scroll display along with scroll on textarea

后端 未结 2 1456
感动是毒
感动是毒 2021-02-04 10:30

I have a text area element whose content is dynamically displayed on the right. As I scroll down the textarea, I want the display to be scrolled down as well corresponding to th

相关标签:
2条回答
  • 2021-02-04 10:49

    If I understand you correctly you want to distribute the scroll position evenly across the two elements. Here's a take on that:

    var input = document.querySelector('.input');
    var result = document.querySelector('.result');
    
    input.addEventListener('scroll', function () {
      var factor = input.scrollTop / (input.scrollHeight - input.offsetHeight);
      var scrollPosition = factor * (result.scrollHeight - result.offsetHeight);
      
      result.scrollTop = scrollPosition;
    });
    .container {
      display: flex;
    }
    
    .input,
    .result {
      flex-basis: 50%;
      height: 16rem;
    }
    
    .input {
      background: lightgray;
    }
    
    .result {
      background: papayawhip;
      overflow-y: auto;
    }
    <div class="container">
      <textarea class="input">Biscuit chupa chups sweet toffee croissant. Pastry muffin tiramisu gingerbread dessert. Cookie liquorice liquorice gingerbread tootsie roll fruitcake caramels pudding tootsie roll. Gingerbread dragée oat cake soufflé. Candy dragée candy bear claw pie cheesecake bonbon toffee. Caramels sweet roll marshmallow tiramisu powder. Marzipan bonbon tart jelly jujubes. Candy brownie marzipan chocolate biscuit dessert. Pudding lemon drops croissant ice cream bonbon danish croissant bonbon. Danish cake fruitcake chocolate bar gingerbread gummi bears sesame snaps halvah. Marzipan candy canes gingerbread chocolate cake. Jelly-o caramels tiramisu powder pudding pie biscuit halvah. Toffee wafer pastry danish powder candy chocolate bar brownie tiramisu.
    Lollipop chocolate cake pastry jujubes icing sesame snaps brownie cupcake cotton candy. Sesame snaps wafer sweet roll tiramisu cake topping chocolate cake fruitcake. Jelly beans sugar plum marshmallow cake chocolate bar liquorice chupa chups. Oat cake jelly fruitcake pudding gummies caramels icing. Chocolate tiramisu donut donut carrot cake jelly-o. Gingerbread marzipan chupa chups powder soufflé toffee. Chocolate bar jelly pie. Marshmallow jelly-o chocolate. Candy canes biscuit lollipop pudding pie biscuit caramels. Icing pudding muffin. Topping biscuit cotton candy sweet wafer sweet gummies cheesecake. Croissant soufflé pudding. Cookie jelly beans cotton candy powder sesame snaps tootsie roll pudding jelly-o bear claw. Pastry macaroon gummies chupa chups powder chocolate bonbon lemon drops.
    </textarea>
      <div class="result">Biscuit chupa chups sweet toffee croissant. Pastry muffin tiramisu gingerbread dessert. Cookie liquorice liquorice gingerbread tootsie roll fruitcake caramels pudding tootsie roll. Gingerbread dragée oat cake soufflé. Candy dragée candy bear claw pie cheesecake bonbon toffee. Caramels sweet roll marshmallow tiramisu powder. Marzipan bonbon tart jelly jujubes. Candy brownie marzipan chocolate biscuit dessert. Pudding lemon drops croissant ice cream bonbon danish croissant bonbon. Danish cake fruitcake chocolate bar gingerbread gummi bears sesame snaps halvah. Marzipan candy canes gingerbread chocolate cake. Jelly-o caramels tiramisu powder pudding pie biscuit halvah. Toffee wafer pastry danish powder candy chocolate bar brownie tiramisu.
    Lollipop chocolate cake pastry jujubes icing sesame snaps brownie cupcake cotton candy. Sesame snaps wafer sweet roll tiramisu cake topping chocolate cake fruitcake. Jelly beans sugar plum marshmallow cake chocolate bar liquorice chupa chups. Oat cake jelly fruitcake pudding gummies caramels icing. Chocolate tiramisu donut donut carrot cake jelly-o. Gingerbread marzipan chupa chups powder soufflé toffee. Chocolate bar jelly pie. Marshmallow jelly-o chocolate. Candy canes biscuit lollipop pudding pie biscuit caramels. Icing pudding muffin. Topping biscuit cotton candy sweet wafer sweet gummies cheesecake. Croissant soufflé pudding. Cookie jelly beans cotton candy powder sesame snaps tootsie roll pudding jelly-o bear claw. Pastry macaroon gummies chupa chups powder chocolate bonbon lemon drops.
    Caramels tootsie roll marshmallow cookie. Chocolate fruitcake cake chocolate jelly beans candy canes chocolate bar carrot cake sesame snaps. Cotton candy tart topping bonbon fruitcake sweet tiramisu sweet. Halvah cake liquorice dessert jujubes dessert jelly-o bonbon powder. Lemon drops croissant soufflé icing tart pie dragée ice cream. Chocolate bar sesame snaps soufflé. Chocolate carrot cake marzipan tiramisu chupa chups. Jujubes chocolate apple pie jujubes oat cake topping. Lemon drops candy powder sweet dessert wafer chupa chups macaroon. Pudding gummi bears sugar plum donut. Cookie liquorice wafer dessert. Topping jujubes gingerbread.
    Cake donut tootsie roll wafer brownie. Soufflé cotton candy topping. Cake halvah ice cream macaroon jujubes sesame snaps. Apple pie biscuit chocolate chocolate cake tart marzipan apple pie. Chocolate bar cake bear claw cookie cake fruitcake carrot cake halvah lollipop. Sesame snaps chupa chups pudding. Sesame snaps danish tart cotton candy cheesecake liquorice. Dragée sweet jelly beans carrot cake cake chocolate bar cake sweet roll chocolate bar. Tiramisu cookie chocolate cake powder gingerbread pudding chupa chups tiramisu carrot cake. Apple pie cake jujubes. Lollipop icing cupcake carrot cake liquorice marzipan. Sesame snaps gummi bears jelly-o ice cream caramels wafer. Wafer donut tart jujubes chocolate chupa chups. Macaroon tart tootsie roll ice cream muffin croissant soufflé gingerbread.
    Dessert marzipan tart cheesecake donut. Wafer sweet jujubes gummi bears bear claw. Jelly beans candy canes gummi bears tootsie roll. Macaroon chocolate bar carrot cake fruitcake sweet roll danish sugar plum croissant candy canes. Tootsie roll icing candy oat cake chocolate bar dessert marzipan. Donut lemon drops gingerbread cake. Cotton candy fruitcake jujubes biscuit jelly donut pastry lollipop. Candy canes cheesecake liquorice toffee jelly. Marzipan tootsie roll lollipop. Chupa chups donut candy canes cotton candy icing tart muffin caramels. Tootsie roll toffee chocolate cake gummi bears. Cake macaroon candy canes muffin tootsie roll cupcake muffin halvah cake. Chocolate toffee gummi bears brownie soufflé jujubes marshmallow tart apple pie.</div>
    </div>

    0 讨论(0)
  • 2021-02-04 10:52

    To sync scroll between 2 (or multi) div you can use (native javascript):

      var columns = document.querySelectorAll('.column');
      for (var i = 0; i < columns.length; i++) {
          columns[i].addEventListener('scroll', function() {
              var factor = this.scrollTop / (this.scrollHeight - this.offsetHeight);
              for (var j = 0; j < columns.length; j++) {
                  columns[j].scrollTop = factor * (columns[j].scrollHeight - columns[j].offsetHeight);
              }
          });
      }
    

    http://codepen.io/NourSammour/pen/ONLoxZ?editors=1011

    Or you can simply use JQuery

    $(document).ready(function() {
        $('.column').each(function() {
            var obj = $(this);
            obj.scroll(function() {
                $('.column').scrollTop(obj.scrollTop());
            });
        });
    });
    

    http://codepen.io/NourSammour/pen/wGwEmY?editors=1111

    0 讨论(0)
提交回复
热议问题