2012-11-14 36 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'); 
} 
相关问题