- >請轉到這個問題同步兩個div滾動不順暢iOS中
我想同步兩個div的滾動條,這是編輯的部分如何我這樣做
var div1 = document.getElementById('element1'),
div2 = document.getElementById('element2');
div1.addEventListener('touchmove', scrolled, false);
div2.addEventListener('touchmove', scrolled, false);
function getscrollTop(node) {
return node.pageYOffset || node.scrollTop;
}
function scrolled() {
var node = this, scrollTop = getscrollTop(node);
var percentage = scrollTop/(node.scrollHeight - node.clientHeight);
var other = document.getElementById({
"element1": "element2",
"element2": "element1"
}[node.id]);
other.scrollTop = percentage * (other.scrollHeight - other.clientHeight);
};
Fiddle - >使用scroll
改爲touchmove
但問題是它在低端設備中閃爍,並希望在事件低端設備中使其平滑。
編輯
我用下面的代碼來平滑滾動
var children = document.querySelectorAll('.scrolldiv');
var getscrollTop = function(node) {
return node.pageYOffset || node.scrollTop;
}, toInt = function(n) {
return Math.round(Number(n));
};
window.setInterval(function() {
var scrollTop = getscrollTop(children[0]);
var percentage = scrollTop/(children[0].scrollHeight - children[0].clientHeight);
var oscrollTop = percentage * (children[1].scrollHeight - children[1].clientHeight);
// console.log(1);
children[1].scrollTop = toInt(oscrollTop);
}, 2);
設置第二個div時的滾動它催人淚下,在急拉它在桌面瀏覽器,但在iOS的瀏覽器,更順暢一旦滾動完成,感覺設置scrollTop而不是滾動。
也許這會幫助你http://stackoverflow.com/questions/17722497/scroll-smoothly-to-specific-element-on-page – lordkain
我注意到的一件事是在第17行,你正在調用一個函數「getscrollTop (節點)「,你已經有了一個變量scrollTop。此外,你聲明「oscrollTop」作爲變量,然後直接將它分配給函數最後的一個對象......直接將其分配給對象。如果您擔心低端設備的性能,代碼優化非常重要。 – Michael
@邁克爾感謝:) – Exception