iframe子元素相對於父頁面滾動條固定(iframe無滾動條,iframe固定高度有滾動條,兩種情況)


一、當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); });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM