我使用以下代码在向下滚动时固定 div 的位置(因此它会留在窗口中)。它工作得很好,但在 IE7 中我得到了错误:offset().top is null or not an object.
$(document).ready(function(){
var msie6 = $.browser == 'msie' && $.browser.version < 7;
if (!msie6) {
var top = $('#comment').offset().top - parseFloat($('#comment').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
$('#comment').addClass('fixed');
} else {
// otherwise remove it
$('#comment').removeClass('fixed');
}
});
}
});
谷歌搜索我发现了这个(见 Earl Jenkins 的底部帖子)http://api.jquery.com/offset/ 在其中他解决了这个特定的错误。但是,像我这样的 jQuery 和 javascript 初学者,我不知道如何实现这个修复,因为在他的帖子中他使用了一个固定值(100),而在上面的代码中却没有。
我试图通过这样做来修复:
var fix = $('#comment').offset();
var top = fix.top - parseFloat($('#comment').css('margin-top').replace(/auto/, 0));
但这并不能解决问题。谢谢你的帮助!