我试图让用户使用鼠标垂直和水平移动页面。为此,我使用 jQuery 的scrollTop() 和scrollLeft() 函数在用户按下并移动鼠标时滚动文档。我的代码工作正常,但页面在移动时剧烈晃动。
我已经能够用更小的代码片段生成相同的问题。
这是live demo .
有什么想法导致这种不稳定的行为吗?
最佳答案
为您的 jQuery 尝试类似的操作。这是一个工作JSFiddle
var down=false;
var downX=0;
var downY=0;
$(document).ready(function(){
$(document).mousedown(function(e){
e.preventDefault();
down=true;
downX=e.clientX;
downY=e.clientY;
return false;
}).mouseup(function(e){
e.preventDefault();
down=false;
return false;
}).mousemove(function(e){
e.preventDefault();
if(down){
$(document).scrollTop($(document).scrollTop()-e.clientY+downY)
.scrollLeft($(document).scrollLeft()-e.clientX+downX);
downY = e.clientY; //Reset the cursor's last Y position
downX = e.clientX; //Reset the cursor's last X position
}
return false;
});
});
此内容改编自 draggable.js jQuery 插件。我强烈推荐你使用它。它为您完成所有这些工作。这是它的演示 http://hitconsultants.com/dragscroll_scrollsync/scrollpane.html .
关于jquery - 滚动顶部和滚动左摇晃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24623209/