如果条件始终在$(window).resize函数中起作用 [英] if condition always work inside $(window).resize function
本文介绍了如果条件始终在$(window).resize函数中起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个脚本,我希望当窗口仅小于768px
console.log('less than 768px');
时,但是当窗口大于768px
时,我的脚本始终有效,请帮助我:(
I have this script and I want when window is only less than 768px
console.log('less than 768px');
but my script works always when window is more then 768px
please help me someone :(
var width = $(window).width();
$(window).on('resize',function(){
if (width < 768){
console.log('less than 768px');
}else{
console.log('nothing');
}
});
推荐答案
您需要获取resize事件回调中的宽度以获取更新后的值:
You need to get the width inside the resize event callback to get the updated value:
$(window).on('resize',function(){
var width = $(window).width();
if (width < 768){
console.log('less than 768px');
}else{
console.log('nothing');
}
});
这篇关于如果条件始终在$(window).resize函数中起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文