用鼠标位置更改图像
我的页面顶部有一个图像,希望它根据您在页面上的鼠标位置从静止变为左,然后向右变化。 请帮我
最好的办法是在文档上使用mousemove
function,然后使用event参数跟踪鼠标位置。
这是一个JSFiddle示例 。
$(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 });
这是一个关于鼠标跟踪的教程 。
这是一大堆可用的事件。
特别是这里的pageX
和pageY
。