jquery - 滚动顶部和滚动左摇晃

标签 jquery scroll scrolltop mousemove

我试图让用户使用鼠标垂直和水平移动页面。为此,我使用 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/

相关文章:

angular - 在单元测试 Angular 时测试 scrollintoview

ios - 带有 ContentInset 的 UICollectionView 没有一直向下滚动

javascript - 如何使用 jQuery 获取页面向下滚动的像素数量?

jquery - 包装器溢出后窗口滚动到顶部 :hidden via jQuery

javascript - 从文本框的列表项中选择部分文本

jquery - 具有高级自动完成功能的 Angular TextEditor

qt - 我们可以让 QTableWidget 平滑滚动其内容吗?

javascript - onclick 向下滚动计算 (100% - 60px)

javascript - JSON 字符串返回 NAN

jquery - 获取触发引导模式隐藏的元素的 id