Scrolling a jScrollPane element while dragging an element

I'm trying to scroll by highlighting text and dragging it down. Now, as you probably know, this is the default default behavior for a standard item overflow: auto, however I am trying to do this with some fancy scrollbars kindly provided by jQuery jScrollPane from Kelvin Luck.

I created a fiddle here: DEMO

Basically, as you can see, highlighting and scrolling work at the top of the window (by default overflow: auto), but not in the second, and to make matters worse, as soon as you reach the bottom, it DOES NOT WRONG your choice!

So my question is (is) this (these): is there a way to fix this? If so, how?

UPDATE

I worked on this quite a bit and found a small solution using setTimeout()

however, it does not work as intended, and if someone wants to help, I will split it into a new fiddle here: jsFiddle

code itself:

pane = $('#scrolldiv2');
pane.jScrollPane({animateEase: 'linear'});
api = pane.data('jsp');

$('#scrolldiv2').on('mousedown', function() {
    $(this).off().on('mousemove', function(e) {
        rel = $(this).relativePosition();
        py = e.pageY - rel.y;
        $t = $(this);
        if (py >= $(this).height() - 20) {
            scroll = setTimeout(scrollBy, 400, 20);
        }
        else if (py < 20) {
            scroll = setTimeout(scrollBy, 400, -20);
        }
        else {
            clearTimeout(scroll);
        }
    })
}).on('mouseup', function() {
    $(this).off('mousemove');
    clearTimeout(scroll);
})

var scrollBy = function(v) {
    if (api.getContentPositionY < 20 & v == -20) {
        api.scrollByY(v + api.getContentPositionY);
        clearTimeout(scroll);
    } else if (((api.getContentHeight - $t.height()) - api.getContentPositionY) < 20 & v == 20) {
        api.scrollByY((api.getContentHeight - $t.height()) - api.getContentPositionY);
        clearTimeout(scroll);
    } else {
        api.scrollByY(v, true)
        scroll = setTimeout(scrollBy, 400, v)
    }
}

$.fn.extend({
    relativePosition: function() {
        var t = this.get(0),
            x, y;
        if (t.offsetParent) {
            x = t.offsetLeft;
            y = t.offsetTop;
            while ((t = t.offsetParent)) {
                x += t.offsetLeft;
                y += t.offsetTop;
            }
        }
        return {
            x: x,
            y: y
        }
    },
})​
+5
source share
2 answers

You just need to scroll up / down depending on how close the mouse is to the end of the div; not as good as your own solution, but it does its job ( http://jsfiddle.net/PWYpu/25/ )

$('#scrolldiv2').jScrollPane();

var topScroll = $('#scrolldiv2').offset().top,
    endScroll = topScroll + $('#scrolldiv2').height(),
    f = ($('#scrolldiv2').height() / $('#scrolldiv2 .jspPane').height())*5 ,
    selection = false,
    _prevY;

$(document).mousemove(function(e){
    var mY;
    var delta = _prevY - e.pageY;
    if((e.pageY < endScroll && (mY = ((e.pageY - endScroll + 80)/f)) > 0) || 
       (e.pageY > topScroll && (mY = (e.pageY - (topScroll + 80))/f) < 0)){
          if(selection && (delta > 10 || delta < -10) )
          $('#scrolldiv2').data('jsp').scrollByY(mY, false) ;
    } 
})   

$('#scrolldiv2').mousedown(function(e){_prevY = e.pageY; selection = true ;})
$(window).mouseup(function(){selection = false ;})​

By the way, the reason why he inverts the choice is because he reached the end of the document, just put some empty space there and the problem was solved.

+3
source

, , , , ( ), . .

0

All Articles