gpt4 book ai didi

javascript - 在调整大小刷新时清除 Jquery css 更改

转载 作者:行者123 更新时间:2023-11-29 22:00:39 25 4
gpt4 key购买 nike

从我的示例中可以看出 here .我试图做到这一点,以便在将窗口调整为小于 1000px 时; CSS 被清除,因此我的 .css 文件可以接管。

如果窗口大小开始低于 1000px 则有效,但反之则无效

$(window).resize(function(){

if ($(window).width() >= 1000) {

var length = $('#left').height() - $('#sidebar').height() + $('#left').offset().top;

$(window).scroll(function () {

var scroll = $(this).scrollTop();
var height = $('#sidebar').height() + 'px';

if (scroll < $('#left').offset().top) {

$('#sidebar').css({
'position': 'absolute',
'top': '0'
});

} else if (scroll > length) {

$('#sidebar').css({
'position': 'absolute',
'bottom': '0',
'top': 'auto'
});

} else {

$('#sidebar').css({
'position': 'fixed',
'top': '0',
'height': height
});
}
});

}
else if ($(window).width() <= 1000) {
$('#sidebar').css({
'position': '',
'bottom': '',
'top': ''
});
$('#left').css({
'position': '',
'bottom': '',
'top': ''
});
}

});

最佳答案

我认为代码无法正常工作的主要问题是滚动功能在窗口调整大小功能内;我认为您只需要稍微调整一下内容:

$(window).scroll(function () {

if ($(window).width() >= 1000) {

var length = $('#left').height() - $('#sidebar').height() + $('#left').offset().top;

var scroll = $(this).scrollTop();
var height = $('#sidebar').height() + 'px';

if (scroll < $('#left').offset().top) {

$('#sidebar').css({
'position': 'absolute',
'top': '0'
});

} else if (scroll > length) {

$('#sidebar').css({
'position': 'absolute',
'bottom': '0',
'top': 'auto'
});

} else {

$('#sidebar').css({
'position': 'fixed',
'top': '0',
'height': height
});
}
}
});

$(window).resize(function(){

if ($(window).width() < 1000) {
$('#sidebar').css({
'position': '',
'bottom': '',
'top': ''
});
$('#left').css({
'position': '',
'bottom': '',
'top': ''
});
}

});

关于javascript - 在调整大小刷新时清除 Jquery css 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23945389/

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