jQuery $(window).resize()在减少窗口高度时不起作用 [英] jQuery $(window).resize() not working when decreasing window's height
问题描述
我有一个ID为main的div,我想使用jQuery来设置其高度,并在 window.resize
上设置其高度,使用函数修改其高度但由于某种原因,只有在增加窗口的高度时才能正常工作,而在减少窗口高度时不起作用。
这是JS:
function setMainH(){
var docH = $(document).height();
$(#main)。height(docH - 40);
$(function(){
setMainH();
});
$ b $(window).resize(function(){
setMainH();
});
编辑:问题出现增加或减少
编辑2: resize()
事件似乎被调用正确,我尝试过使用
的console.log( 调整大小);
调整大小并正确记录,所以问题不在于此。
$(window).height(); //返回浏览器视口的高度
$(document).height(); //返回HTML文档的高度
所以改变
var docH = $(document).height();
至
var docH = $(window).height();
将解决此问题。
I have a div with an id "main", I want to set its height using jQuery and making so that on window.resize
its height is modified using a function but for some reason it works well only when increasing window's height, when decreasing it it doesn't work.
This is the JS:
function setMainH() {
var docH = $(document).height();
$("#main").height(docH - 40);
}
$(function() {
setMainH();
});
$(window).resize(function() {
setMainH();
});
EDIT: the problem appears either increasing or decreasing
EDIT 2: the resize()
event seem to be called correctly, I've tried with
console.log("resizing");
on resizing and it logs correctly so the problem isn't that.
$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document
So changing
var docH = $(document).height();
to
var docH = $(window).height();
will solve the problem.
这篇关于jQuery $(window).resize()在减少窗口高度时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!