2012-08-04 159 views
-1

我要滚动的页面,根据鼠标的位置任何想法如何使用jQuery做到这一点? 要添加我有鼠标的坐标。滚动根据鼠标位置--jQuery

增加的实际功能。你能帮我滚动吗?

function createDraggables(){ 
     $j(".card").draggable({ 
      revert: "invalid", 
      containment: "#cardwall", 
      drag: function(event,ui) { 
        var viewportHeight = $j(window).height(); 
        var documentHeight = $j(document).height(); 
        var y = event.pageY - $j('html, body').scrollTop(); 
        console.log('==>',this); 
        $j('html, body').scrollTop ((y/viewportHeight) * documentHeight); 
       } 
     }); 
    } 
+1

左右...上下...?任何代码? WHYTSF(你到目前为止尝试过什么?) – 2012-08-04 17:04:12

+0

我只是想要顶部和底部。 – user1083596 2012-08-04 17:40:06

回答

1

看起来像Vanga Sasidhar有你在找什么。然而,正如你所说的,你可以使用这个坐标来平滑地滚动到顶部的给定位置。

$(document).ready(function(){ 
    $('html, body').animate({ 
     scrollTop: 200 // Replace this value with your coordinates 
    }, 1000); 
}); 
0

是的您可以根据当前的鼠标位置来做到这一点。

var viewportHeight = $(window).height(); 
var documentHeight = $(document).height(); 

//Lets listen for mousemove event on body tag 
$('body').mousemove (function(e) { 
    //Get current y position 
    var y = e.pageY - $(this).scrollTop(); 

    //Based on current proportion, update the document's scrollTop. 
    $(this).scrollTop ((y/viewportHeight) * documentHeight); 
});​ 

工作演示可以在这里找到:http://jsfiddle.net/codebombs/GfX3L/

+0

那么我无法在这个函数中复制上面的代码。你能帮我吗? – user1083596 2012-08-04 17:53:18