jQuery Grab Content and Scroll page horizontally

感情迁移 提交于 2020-01-13 19:11:07

问题


My goal is to grab content x and scroll page horizontally while mouse is moving until mouse stops (mouseup event), similar to a tablet swipe action.

Seems easy enough.. Get clientX on mousedown, scrollLeft by ClientX while moving, Turn off mousemove function when done.

I've been playing with it for awhile and can't get the scroll effect I'm looking for..

What am I doing wrong here?

$('#thediv').on('mousedown', function(event) {  
    var e = event;   

    $('#thediv').on('mousemove',function(event){ 
        new_e = event; 
        $('html, body').stop().animate({
            scrollLeft: new_e.clientX  
        }, 300);   
        return false;  
     }); 

    $('#thediv').on('mouseup', function() { 
        $('#thediv').off('mousemove');  
    }); 
}); 

http://jsfiddle.net/mD7mu/


回答1:


$('#greendiv').on('mousedown', function(e) {
    $('#greendiv').on('mousemove', function(evt) {
        $('html,body').stop(false, true).animate({
            scrollLeft: e.pageX - evt.clientX
        });
    });
});

really close. simply subtracting the clientX from the pageX , and using false,true in the stop function will give you the desired effect, I think.

here's the fiddle!



来源:https://stackoverflow.com/questions/13926640/jquery-grab-content-and-scroll-page-horizontally

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