Mousedown on body and drag the page up or down, like on a pdf

前端 未结 4 1039
隐瞒了意图╮
隐瞒了意图╮ 2021-02-11 07:12

How would I make it so I can mousedown on the body on my page, then drag the page up or down, like you can in a PDF?

Basically I want to be able to drag the whole page u

4条回答
  •  醉话见心
    2021-02-11 07:54

    Quick fix for the scrollbar problem: use $(document.body) instead of $(document).

    Still, the animation behave strangely sometimes, especially with repetitive scrolls. (eg. scrolling up and the following animation scrolls down).

    Since the OP asked for a PDF viewer-like scrolling (and not an iPhone-like scrolling) that don't involve any kind of animation, I guess we could put Sohnee's code on a diet and write this:

        var gesturesY = 0;
        var startPosition = 0;
        var isMouseDown = false;
    
        $(document.body).mousemove( function (e) {
            gesturesY = parseInt(e.pageY, 10);
            if (isMouseDown) {
                window.scrollBy(0, startPosition - gesturesY);
                return false;
            }
        });
    
        $(document.body).mousedown( function() {
            startPosition = gesturesY;
            isMouseDown = true;
        });
    
        $(document.body).mouseup( function() {
            isMouseDown = false;
            return false;
        });
    

提交回复
热议问题