Handle click and drag movement to scroll horizontally using mootools or jquery

Is there an easy way to handle a motion event consisting of clicking and dragging left or right on a div to make a classic slider.
The idea is to do something similar to scrolling iphone applications, but by left-clicking.

+5
source share
2 answers

Do you mean something like this?

var x,y,top,left,down;

$("#stuff").mousedown(function(e){
    e.preventDefault();
    down = true;
    x = e.pageX;
    y = e.pageY;
    top = $(this).scrollTop();
    left = $(this).scrollLeft();
});

$("body").mousemove(function(e){
    if(down){
        var newX = e.pageX;
        var newY = e.pageY;

        $("#stuff").scrollTop(top - newY + y);    
        $("#stuff").scrollLeft(left - newX + x);    
    }
});

$("body").mouseup(function(e){down = false;});

http://jsfiddle.net/AhC87/2/

, div. , , , . .

+7

All Articles