2012-08-04 148 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