次のコードを使用して、下にスクロールしたときに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));
しかし、それはトリックを行いません。ご協力いただきありがとうございます!