javascript - Scrolling with mouse movements - Stack Overflow

var down=false;var scrollLeft=0;var x=0;$('#test').mousedown(function(e) {down = true;scrol

var down=false;
var scrollLeft=0;
var x=0;

$('#test').mousedown(function(e) {
    down = true;
    scrollLeft = this.scrollLeft;
    x = e.clientX;
}).mouseup(function() {
    down = false;
}).mousemove(function(e) {
    if (down) {
       this.scrollLeft = scrollLeft + x - e.clientX;
    }
}).mouseleave(function() {
    down = false;
});

Here's a demo: /

I want to be able to scroll the div by using the mouse. It works fine, though a bit weird with text since you can accidentally select things, but it has a problem that shows up when you move the mouse outside of the test div. It continues scrolling even though I am setting down=false. How can I stop this and why is it happening?

var down=false;
var scrollLeft=0;
var x=0;

$('#test').mousedown(function(e) {
    down = true;
    scrollLeft = this.scrollLeft;
    x = e.clientX;
}).mouseup(function() {
    down = false;
}).mousemove(function(e) {
    if (down) {
       this.scrollLeft = scrollLeft + x - e.clientX;
    }
}).mouseleave(function() {
    down = false;
});

Here's a demo: http://jsfiddle/STVqe/3/

I want to be able to scroll the div by using the mouse. It works fine, though a bit weird with text since you can accidentally select things, but it has a problem that shows up when you move the mouse outside of the test div. It continues scrolling even though I am setting down=false. How can I stop this and why is it happening?

Share Improve this question asked Jun 6, 2011 at 18:30 RaduRadu 8,6998 gold badges56 silver badges93 bronze badges 8
  • What do you mean by "scroll the div by using the mouse?" I don't actually see anything different happen when I move the mouse, whether or not it's clicked. – Matt Ball Commented Jun 6, 2011 at 18:35
  • Click the div and hold the mouse down, then move it around horizontally. – Radu Commented Jun 6, 2011 at 18:37
  • Here is perhaps a clearer example of the desired effect: jqueryfordesigners./demo/scrollable-timelines.html – Radu Commented Jun 6, 2011 at 18:38
  • 1 @Radu, if you want that affect, then remove the scrollbar. – Naftali Commented Jun 6, 2011 at 18:39
  • 1 BTW, if you don't want the text to be selectable: jsfiddle/mattball/MP2cg via *user-select – Matt Ball Commented Jun 6, 2011 at 18:48
 |  Show 3 more ments

2 Answers 2

Reset to default 5

This works for me in Chrome: http://jsfiddle/mattball/MP2cg/

It looks like the problem is actually caused because of the text being selected. If you add the non selectable text css rules and go with your original javascript it works (I believe) as you want, with scrollbars. Tested in FF 3.6

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742419294a4440359.html

相关推荐

  • javascript - Scrolling with mouse movements - Stack Overflow

    var down=false;var scrollLeft=0;var x=0;$('#test').mousedown(function(e) {down = true;scrol

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信