问题
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