2012-11-14 95 views
0

我試圖阻止通過CSS媒體查詢更改CSS值後觸發調整大小功能。這不起作用......我想「有一個切換」只出現一次。我如何殺死它?如何在css值更改後停止調整大小功能?

$(window).bind('resize', function(event) { 
    if ($('#toggle').css('display') == 'none') 
     { 
      console.log("there's no toggle!"); 
     } 
     else { 
      console.log("there IS a toggle!"); 
      return false; // trying to stop the function 
     } 
}); 

回答

1

應該這樣寫,使用。對()和.off()。

$(window).on('resize', function() { 

if ($('#toggle').css('display') == 'none') 
    { 
     console.log("there's no toggle!"); 
    } 
    else { 
     console.log("there IS a toggle!"); 
     $(window).off('resize'); 
    } 
}); 
+0

使用on和off綁定和解除綁定的優點是什麼? – Lauren

+0

從jQuery 1.7開始,首選的是.on()處理程序...最終.bind()將會像對待.live()一樣被棄用(踢到了路邊)。.on()處理程序更加靈活, .live()和.bind()....你可以在這裏閱讀更多:) http://api.jquery.com/bind/ – VIDesignz

1

使用unbind()

$(window).unbind('resize'); 

/

else { 
     console.log("there IS a toggle!"); 
     $(window).unbind('resize'); 
} 
相關問題