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

前端 未结 4 1041
隐瞒了意图╮
隐瞒了意图╮ 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 08:01

    Variation of Francesco's answer that includes scrolling along the X-axis:

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

提交回复
热议问题