你可以嘗試t他下面的代碼。請記住,如果您不希望瀏覽器窗口調整大小,則可以將$(window).width()/2
分配給mousemove
綁定之外的變量,以避免在每次更新時查找窗口寬度。需要使用Math.floor
或Math.ceil
來向上/向下舍入計算的水平中心以避免十進制數。
實施例1(水平中心是動態它總是會在鼠標移動重新計算。):
$(document).on('mousemove',function(e){
if((e.pageY==0) && (e.pageX==Math.floor($(window).width()/2))){
//run function
}
});
實施例2(水平中心保持靜止,在執行時即計算值):
var hCenter = Math.floor($(window).width()/2);
$(document).on('mousemove',function(e){
if((e.pageY==0) && (e.pageX==hCenter)){
//run function
}
});
實施例3(對窗口調整大小更新hCenter):
// calculate horizontal center at page load
var hCenter = Math.floor($(window).width()/2);
// update hCenter every time the window is resized
$(window).resize(function(){
hCenter = Math.floor($(window).width()/2);
});
$(document).on('mousemove',function(e){
if((e.pageY==0) && (e.pageX==hCenter)){
//run function
}
});
使用'mousemove'事件?但請記住,這是一項非常密集的任務,儘管它是速率有限的 – Alexander 2013-03-09 17:59:41
你有沒有嘗試過任何東西?請發佈您的代碼 – darshanags 2013-03-09 18:00:56
@darshanags我編輯了這個問題,並補充了我會怎麼想的。 – IMUXIxD 2013-03-09 18:11:45