我正在构建一个脚本,当窗口大小小于783px时,该脚本需要更改某个脚本的全部功能,因此我找到了解决方案
if(jQuery(document).width() < "783" && myvar=="1"){
//do things for screens under 783px
}
但是它仅在第一次加载站点时屏幕小于783px时才有效,现在我也想检查调整大小,因为CSS媒体查询有效并且混乱了。我需要这样的东西:
if((document).width()<783 && myvar=="1" || onresize(document.width<783) && myvar=="1"){
//do things for screens under 783px
}
谁能帮我这个?我试着用
jQuery(window).on("resize", function () {
//do my things for under 783px
}).resize();
但这似乎行不通,如果有人有其他建议,请分享。
使用:
function check() {
if ($(document).width() < 783 && myVar === "1") { // use `===` and no quote around 783
console.log("y");
}
}
check(); // first-time check
$(window).resize(function () { // no `on` here
check();
}); // no `.resize()` needed here
注意:将整个包裹 $(window).load()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句