2012-02-28 140 views
-1

我在頁面頂部有一個圖像,並希望它從靜止變爲左側,然後右側,具體取決於您在頁面上的鼠標位置。請幫助我用鼠標位置改變圖像

+0

到目前爲止我的代碼只能與點擊 ____________________________________________________________________ $( 「face_one 」)。點擊(函數(){ \t \t \t \t $(「 臉 」)。ATTR(「 SRC」,「 images/faces/face_one.png「); \t \t \t \t }); \t \t \t \t $( 「face_two 」)。點擊(函數(){ \t \t \t \t \t $(「 臉 」)。ATTR(「 SRC」, 「圖像/面/ face_two.png」 ); \t \t \t \t \t}); \t \t \t \t $( 「face_three 」)。點擊(函數(){ \t \t \t \t \t $(「 臉 」)。ATTR(「 SRC」, 「圖像/面/ face_three.png」 ); \t \t \t \t \t}); – 2012-02-28 05:28:50

+0

這裏是一個教程,你可能會發現有幫助: http://docs.jquery.com/Tutorials:Mouse_Position#Tracking_mouse_position – kappamaki 2012-02-28 05:34:07

回答

2

最好的辦法是利用文檔上的mousemove函數,然後使用event參數跟蹤鼠標位置。

這裏是JSFiddle example

$(document).mousemove(function(event){ 
    var mloc = { 
     x: event.pageX, 
     y: event.pageY 
    }; 

    if( 
     (mloc.x >= 0 && mloc.x <= $(document).width()/2) && 
     (mloc.y >= 0 && mloc.y <= $(document).height()/2) 
    ){ 
     //In upper left corner 
     //Do stuff 
    }else if( 
     (mloc.x >= $(document).width()/2 && mloc.x <= $(document).width()) && 
     (mloc.y >= 0 && mloc.y <= $(document).height()/2) 
    ){ 
     //In upper right corner 
     //Do stuff 
    } //etc 
}); 

Here's a tutorial關於鼠標跟蹤。
Here's a whole bunch可用事件的東西。

特別是,這裏的pageXpageY

+0

完美的作品,謝謝sooo多ShadowScripter – 2012-02-28 17:01:56

+0

@JoshKing很高興我可以幫助:) – ShadowScripter 2012-02-28 17:04:12