2016-11-30 57 views
0

我想通过滚动修复顶部的div元素。 我已经用下面的代码来实现这一点:引导修复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

回答

0

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

+0

我已经添加了width:inherit,现在div不像以前那么明亮,但它仍然不符合原始大小。我试图添加额外的固定类也col md-6类,但没有影响:( –

+0

我很难理解没有提供HTML + CSS代码的情况下发生了什么。 – het0