gpt4 book ai didi

javascript - Bootstrap 将 div 元素固定在顶部并防止调整大小

转载 作者:行者123 更新时间:2023-11-28 04:46:57 26 4
gpt4 key购买 nike

我想通过滚动在顶部固定一个 div 元素。我用以下代码实现了这一点:

Javascript:

$(function () {
var msie6 = $.browser == 'msie' && $.browser.version < 7;
if (!msie6) {
var top = $('#betslip').offset().top - parseFloat($('#betslip').css('margin-top').replace(/auto/, 0));
$(window).scroll(function (event) {
// what the y position of the scroll is
var y = $(this).scrollTop();
// whether that's below the form
if (y >= top) {
// if so, ad the fixed class
$('#betslip').addClass('fixed');
} else {
// otherwise remove it
$('#betslip').removeClass('fixed');
}
});
}

CSS:

#betslip.fixed {
position: fixed;
top: 0;
}

HTML:

<div class="col-md-12" id="betslip">
...
</div>

问题是,滚动时 div 元素变大了。我该如何解决/防止这种情况?

这是滚动前后的截图:

enter image description here

最佳答案

通过添加 position: fixed;#betslip 它将忽略容器的宽度。尝试将 width: inherit; 添加到 #betslip.fixed

关于javascript - Bootstrap 将 div 元素固定在顶部并防止调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40898762/

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