javascript ie6相容position:fixed實現思路

來源:互聯網
上載者:User

positon:fixed 讓HTML元素脫離文檔流固定在瀏覽器的某個位置
網頁中經常會有浮動的導航條會用到這種定位元模式,但是ie6下並不相容這種定位
浮動導航條的樣式,重要的是position:fixed;bottom:60px;(浮動導航底部距離視窗底部60px) 複製代碼 代碼如下:.floating_9677{position:fixed; z-index:961; bottom:60px;}

ie6下positon:fixed不起作用,只能靠js來實現了,首先在ie6下需要將position設定為absolute 複製代碼 代碼如下:position:fixed;bottom:60px;_position:abosulte;

給浮動元素加一個屬性標識,js通過這個屬效能找到這些浮動元素。tag="floatNavigator"
工作中浮動導航條主要通過top或者bottom來定位。 複製代碼 代碼如下://ie6相容position:fixed
function fixedPositionCompatibility(){
//判斷是否ie6瀏覽器
if( $.browser.msie || parseInt($.browser.version,10) <= 6){
var vavigators = $("[tag='floatNavigator']");
if(!navigators.length)return;
//判斷每個浮層是靠頂部固定還是底部固定
$.each(navigators, function(){
this.top = $(this).css("top");
this.bottom = $(this).css("bottom");
this.isTop = this.top == "auto" ? false : true;
});
window.attachEvent("onscroll", function(){
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
$.each(navigators, function(){
this.style.top = this.isTop ? scrollTop + parseInt(this.top) + "px" : scrollTop + $(window).height() - $(this).outerHeight() - parseInt(this.bottom) + "px";
});
});
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.