DIV CSS 佈局教程網

JS 實現導航欄懸停效果(續二):用 left 50% 讓 fixed 元素水平居中
編輯:關於JavaScript     

在用 jQuery 重寫導航欄懸停頁面後,發現原本的方法還有幾個可以再改進的地方,於是有了這篇續作。本文記錄了重構後的程式碼,並釐清一個關鍵的 CSS 定位問題。

原本存在的問題

第一,原本的 JS 程式碼較冗餘,導航列上的切換是用 JS 做跳轉而非真正的超連結;第二,導航列本身用了 fixed 定位,卻沒有設為水平居中,而是在 JS 裡動態改 left 值來勉強居中。這第二個問題會導致:當瀏覽器視窗尺寸改變時,頁面中其他 div 的位置都變了,就再也無法用頁面中的元素動態地為 fixed 定位的導航列定位。

test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript" src="test.js"></script>
<link rel="stylesheet" type="text/css" href="test.css"></link>
<title>Test</title>
</head>
<body>
<div id="main_div" class="main">
<div id="content_div1" class="content">1</div>
<div id="nav" class="navigation">
<a href="#content_div3"><div id="tab_div1" class="tab">tab1</div></a>
<a href="#content_div4"><div id="tab_div2" class="tab">tab2</div></a>
<a href="#content_div5"><div id="tab_div3" class="tab">tab3</div></a>
<a href="#content_div6"><div id="tab_div4" class="tab">tab4</div></a>
</div>
<div id="content_div2" class="content">2</div>
<div id="content_div3" class="content">3</div>
<div id="content_div4" class="content">4</div>
<div id="content_div5" class="content">5</div>
<div id="content_div6" class="content">6</div>
<div id="content_div7" class="content">7</div>
</div>
</body>
</html>

test.js

//記錄導航條原來在頁面上的位置
var naviga_offsetTop = 0;

//使導航條,懸停在頂部
function naviga_stay_top(){
//獲取滾動距離
var scrollTop = $(document).scrollTop();
//如果向下滾動的距離大於原來導航欄離頂部的距離
//直接將導航欄固定到可視區頂部
if( scrollTop > naviga_offsetTop ){
$("#nav").css({"top": "0px"});
} else {
//如果向下滾動的距離小原來導航欄離頂部的距離,則重新計算導航欄的位置
$("#nav").css({"top": naviga_offsetTop - scrollTop + "px"});
}
}

function onload_function(){
//記錄初始狀態導航欄的高度
naviga_offsetTop = $("#nav").attr("offsetTop");

//綁定滾動和鼠標事件
$(window).bind("scroll", naviga_stay_top);
$(window).bind("mousewheel",naviga_stay_top);
$(document).bind("scroll", naviga_stay_top);
$(document).bind("mousewheel",naviga_stay_top);
}

$(document).ready( onload_function );

test.css(注意 navigation 類的樣式)

div.main{
width: 800px;
background: #CCC;
margin: 10px auto 0;
position: relative;
}

div.content{
width: 800px;
height: 400px;
background: yellow;
margin: 10px auto 0;
}

div.navigation{
width: 800px;
height: 40px;
background: red;
margin: 0 auto;
top: 400px;
left:50%;
position: fixed;
margin-left:-400px;
}

div.tab{
width: 195px;
height: 40px;
background: blue;
float: left;
margin-left: 5px;
}

總結:fixed 元素如何水平居中

這個問題的根因還是在對 CSS 定位不夠熟悉。fixed 定位的元素無法用常見的 margin: 0 auto 來水平居中,因為這種方式要求元素處於正常文檔流中、且父元素要有固定寬度,而 fixed 已經脫離了文檔流。

正確做法是:left: 50% 先把元素的最左邊對齊到父元素寬度的中點,再用 margin-left: -[元素寬度的一半]px 把元素往左推半個自身寬度,這樣就能達到完美的水平居中效果。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved