gpt4 book ai didi

javascript - 实时浏览器大小检测

转载 作者:行者123 更新时间:2023-11-28 13:23:49 27 4
gpt4 key购买 nike

此代码仅在加载时基于浏览器的大小工作,只是想知道我可以为它实现什么来获取当前浏览器大小并根据当前信息工作。

我曾尝试将它包装在 resize() 中,但它会导致它表现得很奇怪,即切换不断地打开和关闭,或者在缩小的浏览器中加载时它根本不起作用。

这是一个响应式网站,页脚菜单在大屏幕上只是静态链接,但在小屏幕上会变成下拉菜单。

    var myWidth = 0, myHeight = 0;

if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
}

if(myWidth < 980) {
$("#footer h3").click(function () {
$(this).toggleClass("active");
$(this).parent().find("ul").slideToggle('medium');
});
}

最佳答案

var myWidth = 0, myHeight = 0;


function getSize(){

if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
}

}

getSize(); // run first time

$(window).resize(function(){
getSize(); // do it on resize
});


$("#footer h3").click(function () {

getSize(); // not needed but good to have

if(myWidth < 980) {
$(this).toggleClass("active");
$(this).parent().find("ul").slideToggle('medium');
}

});

关于javascript - 实时浏览器大小检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14691412/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com