Vertical Scrolling Sync For 2 Divs

2019-07-29 04:21发布

I am trying to sync the vertical scrolls of 2 divs; One is TinyMCE editor's body, and the other one is dynamically created preview div.

The preview looks like this: enter image description here

For the preview div, I managed to get the % of the scroll by using:

setup : function(ed) {
   ed.on('init', function() {
      $('.editorContainer > .mce-tinymce > .mce-container-body').children().eq(2).on('scroll', function () {
         var tmceBody = ed.getBody();

         console.log("this offsetHeight: " + this.offsetHeight);   // 501
         console.log("this scrollTop: " + this.scrollTop);         // 160
         console.log("this scrollHeight: " + this.scrollHeight);   //  806

         var percentage = this.scrollTop / (this.scrollHeight - this.offsetHeight);
         console.log("this percentage: " + percentage);    // 0.5 -- Prev div works

         console.log("TMCE offsetHeight: " + tmceBody.offsetHeight);    // 1009
         console.log("TMCE scrollTop: " + tmceBody.scrollTop);          // 14
         console.log("TMCE scrollHeight: " + tmceBody.scrollHeight);    // 1037

         var x = percentage * (tmceBody.scrollHeight - tmceBody.offsetHeight);

         console.log("TMCE x: " + x);       // 14.2

         (tmceBody).scrollTop(x);
       })
     }
   }
 }

So when I move preview, editor scrolls but not right value.

Fiddle: https://jsfiddle.net/gm7j0e9u/3/

0条回答
登录 后发表回答