gpt4 book ai didi

jquery - css ("position") 在窗口调整大小时返回 undefined

转载 作者:行者123 更新时间:2023-11-28 09:19:27 25 4
gpt4 key购买 nike

我有以下代码来捕获浏览器调整大小事件。

 window.onresize = function(){
var style = $('#button-selection').position();
if(style == "relative"){
$("#button-section").css("position"," ");
}else if(style == "fixed"){
$("#button-section").css("position","relative");
}
};


window.onresize = function(){
var style = $('#button-selection').css('position');
if(style == "relative"){
$("#button-section").css("position"," ");
}else if(style == "fixed"){
$("#button-section").css("position","relative");
}
};

样式返回undefined。当文档准备好时,我正在运行它。我尝试使用 window ready,但结果是一样的。以上两种方法都返回 undefined!

最佳答案

改变这个:

var style = $('#button-selection').position();

为此:

var style = $('#button-selection').css('position');

.position()用于获取元素的坐标。

来自文档:

Get the current coordinates of the first element in the set of matched elements, relative to the offset parent.


更新:

改用$(window).resize()。我在这里有一个测试用例场景:in this fiddle.

$(window).resize(function () {
var style = $('button').css('position');
if (style == "relative") {
alert('relative');
} else if (style == "fixed") {
alert('fixed');
}
});

关于jquery - css ("position") 在窗口调整大小时返回 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15943481/

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