Vertical Scrolling Sync For 2 Divs

自古美人都是妖i 提交于 2019-12-24 12:12:25

问题


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:

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/

来源:https://stackoverflow.com/questions/42489070/vertical-scrolling-sync-for-2-divs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!