changing a image with mouse positions [closed]

不羁的心 提交于 2019-12-10 12:33:11

问题


I have a image at the top of my page and want it to change from stationary to left then right depending on your mouse position on the page. please help me


回答1:


Your best bet is to utilize the mousemove function on the document and then track the mouse location by using the event parameter.

Here's a 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 on mouse tracking.
Here's a whole bunch of available event stuff.

In particular, here's pageX and pageY.



来源:https://stackoverflow.com/questions/9476843/changing-a-image-with-mouse-positions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!