if ($(window).scrollTop() > 0) {
$("#divContent").css({ "top": "11.2%", "width": "18.4%", "position": "fixed", "z-index": "2" });
$("#divChapters").css({ "position": "relative", "margin-left": "0%" });
} else {
$("#divContent").css({ "position": "relative", "width": "100%", "z-index": "0" });
$("#divChapters").css({ "position": "relative", "margin-left": "0%" });
}
这是我的代码,我在其中添加了一些基于 scrollTop > 0 的 css 但现在我想根据我的要求进行一些修改,在我的 scrollTop 函数中我想检测窗口宽度,如果它检测到窗口宽度大小 = 1920 那么我想给这个 ID 添加宽度 #divContent 设置为 11%。
请帮助我。
最佳答案
你可以使用 -
var width = window.innerWidth;
if(width==1920)
{
$("#divContent").css({"width" : "11%"});
}
跨浏览器解决方案(对 IE8 及更早版本使用 clientWidth 和 clientHeight):
var w = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
关于jquery - 我如何在我的 jquery 代码中指定窗口宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29554154/