用鼠标位置改变图像

问题描述:

我在页面顶部有一个图像,并希望它从静止变为左侧,然后右侧,具体取决于您在页面上的鼠标位置。请帮助我用鼠标位置改变图像

+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

最好的办法是利用文档上的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