一、當iframe自適應高度,無滾動條時候:
例如這樣:
//隨着父頁面滾動條滾動定位“#qn-quc”他的位置固定在頂部 $(parent.window).scroll(function() { var scrollheight = parseInt($(parent.window).scrollTop()); console.log(parseInt($(parent.window).scrollTop())) if (scrollheight >= 300) { $("#qn-quc").css({ "position": "absolute", "top": parseInt($(parent.window).scrollTop()) - 120 + "px" }); } else { document.getElementById("qn-quc").style = " "; } });
或者這樣:
//題目按鈕固定頂部 $(window.parent.document).scroll(function() { var scrollheight = parseInt($(window.parent.document).scrollTop()); if (scrollheight >= 300) { $("#qn-quc").css({ "position": "absolute", "top": parseInt($(window.parent.document).scrollTop()) - 120 + "px" }); } else { document.getElementById("qn-quc").style = " "; } });
二、當iframe頁面存在滾動條就簡單很多了
例如這樣:
var ie6 = document.all; var dv = $('#qn-quc'), st; dv.attr('otop', dv.offset().top); //存儲原來的距離頂部的距離 $(window).scroll(function () { st = Math.max(document.body.scrollTop || document.documentElement.scrollTop); if (st > parseInt(dv.attr('otop'))) { if (ie6) {//IE6不支持fixed屬性,所以只能靠設置position為absolute和top實現此效果 dv.css({ position: 'absolute', top: st }); } else if (dv.css('position') != 'fixed') dv.css({ 'position': 'fixed', top: 0 }); } else if (dv.css('position') != 'static') dv.css({ 'position': 'static' }); });
//監聽window滾動條位置
$(window).scroll(function () { var st = $(this).scrollTop(); console.log(st); });