<html>
<head>
<title>伸縮的菜單</title>
<style>
<!--
body{
background-color:#ffdee0;
}
#navigation {
width:200px;
font-family:Arial;
}
#navigation > ul {
list-style-type:none; /* 不顯示項目符號 */
margin:0px;
padding:0px;
}
#navigation > ul > li {
border-bottom:1px solid #ED9F9F; /* 添加下劃線 */
}
#navigation > ul > li > a{
display:block; /* 區塊顯示 */
padding:5px 5px 5px 0.5em;
text-decoration:none;
border-left:12px solid #711515; /* 左邊的粗紅邊 */
border-right:1px solid #711515; /* 右側陰影 */
}
#navigation > ul > li > a:link, #navigation > ul > li > a:visited{
background-color:#c11136;
color:#FFFFFF;
}
#navigation > ul > li > a:hover{ /* 鼠標經過時 */
background-color:#990020; /* 改變背景色 */
color:#ffff00; /* 改變文字顏色 */
}
/* 子菜單的CSS樣式 */
#navigation ul li ul{
list-style-type:none;
margin:0px;
padding:0px 0px 0px 0px;
}
#navigation ul li ul li{
border-top:1px solid #ED9F9F;
}
#navigation ul li ul li a{
display:block;
padding:3px 3px 3px 0.5em;
text-decoration:none;
border-left:28px solid #a71f1f;
border-right:1px solid #711515;
}
#navigation ul li ul li a:link, #navigation ul li ul li a:visited{
background-color:#e85070;
color:#FFFFFF;
}
#navigation ul li ul li a:hover{
background-color:#c2425d;
color:#ffff00;
}
#navigation ul li ul.myHide{ /* 隱藏子菜單 */
display:none;
}
#navigation ul li ul.myShow{ /* 顯示子菜單 */
display:block;
}
-->
</style>
<script language="javascript">
function change(){
//通過父元素li,找到兄弟元素ul
var oSecondDiv = this.parentNode.getElementsByTagName("ul")[0];
//CSS交替更換來實現顯、隱
if(oSecondDiv.className == "myHide")
oSecondDiv.className = "myShow";
else
oSecondDiv.className = "myHide";
}
window.onload = function(){
var oUl = document.getElementById("listUL"); //一級菜單的ul標簽
var aLi = oUl.childNodes; //子元素
var oA;
for(var i=0;i<aLi.length;i++){
//如果子元素為li,且這個li有子菜單ul
if(aLi[i].tagName == "LI" && aLi[i].getElementsByTagName("ul").length){
oA = aLi[i].firstChild; //找到超鏈接
oA.onclick = change; //動態添加點擊函數
}
}
}
</script>
</head>
<body>
<div id="navigation">
<ul id="listUL">
<li><a href="#">Home</a></li>
<li><a href="#">News</a>
<ul class="myHide">
<li><a href="#">Lastest News</a></li>
<li><a href="#">All News</a></li>
</ul>
</li>
<li><a href="#">Sports</a>
<ul class="myHide">
<li><a href="#">Basketball</a></li>
<li><a href="#">Football</a></li>
<li><a href="#">Volleyball</a></li>
</ul>
</li>
<li><a href="#">Weather</a>
<ul class="myHide">
<li><a href="#">Today's Weather</a></li>
<li><a href="#">Forecast</a></li>
</ul>
</li>
<li><a href="#">Contact Me</a></li>
</ul>
</div>
</body>
</html>
上菜單為二級菜單,伸縮菜單是在一級菜單<li>下點擊實現的
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>伸縮菜單</title>
<meta name="keywords" content="關鍵字列表" />
<meta name="description" content="網頁描述" />
<link rel="stylesheet" type="text/css" href="" />
<style type="text/css">
body,p,ul,li{padding:0px;margin:0px;}
ul li{list-style:none;}
body{font-size:13px;}
.menu{
width:210px;
margin:50px auto;
border:1px solid #ccc;
}
.menu p{
height:25px;
line-height:25px;
font-weight:bold;
background:#eee;
border-bottom:1px solid #ccc;
padding-left:5px;
cursor:pointer;
}
.menu ul li{
height:24px;
line-height:24px;
padding-left:5px;
}
</style>
<script type="text/javascript">
//分析思路
//當頁面加載完成后
//獲取到所有的p元素 獲取到所有的ul元素
//給每一個p元素綁定一個onclick事件
//判斷每一個p元素對應的ul是否是隱藏或者是顯示
window.onload = function(){
//獲取id=menu對象
var div_obj = document.getElementById("menu");
//獲取所有的p元素
var ps_obj = div_obj.getElementsByTagName("p");
var ps_length = ps_obj.length;
//獲取到所有的ul元素
var uls_obj = div_obj.getElementsByTagName("ul");
//給每一個p元素綁定一個onclick事件
for(var i=0;i<ps_length;i++){
ps_obj[i].id = i; //給每一個p元素加上一個標識
ps_obj[i].onclick = function(){
//判斷對應的ul是否是顯示或者隱藏
if(uls_obj[this.id].style.display == "none"){
uls_obj[this.id].style.display = "block";
}else{
uls_obj[this.id].style.display = "none";
}
}
}
}
</script>
</head>
<body>
<div id="menu" class="menu">
<div>
<p>web前端</p>
<ul style="display:none;">
<li>HTML</li>
<li>DIV+CSS</li>
<li>JAVASCRIPT</li>
<li>jQuery</li>
<li>Bootstrap</li>
</ul>
</div>
<div>
<p>PHP+MYSQL核心編程</p>
<ul style="display:none;">
<li>PHP</li>
<li>MYSQL</li>
<li>HTTP協議</li>
<li>PHP繪圖技術</li>
</ul>
</div>
<div>
<p>PHP高級</p>
<ul style="display:none;">
<li>XML編程</li>
<li>AJAX</li>
<li>MVC</li>
</ul>
</div>
</div>
</body>
</html>
類似QQ伸縮菜單:
今天的教程中,我們將創建一個完整的頁面列表,該菜單具有兩個良好的特性:當在菜單項上方停留時,我們將移動一個適用于當前項的寬度的hover狀態項,并且我們將從頁面的左側滑動出一個描述條,到達當前菜單項。
我們將使用jQuery實現該風格的效果和一些CSS3屬性。我們不打算使用任何圖像。
那么,讓我們開始吧!
標記
HTML結構將包含一個無序列表,它表示我們的菜單和一個用于描述元素的div:
<div id="slidingMenuDesc" class="slidingMenuDesc"> <div><span>Description for "About"</span></div> ... </div> <ul id="slidingMenu" class="slidingMenu"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Portfolio</a></li> <li><a href="#">Work</a></li> <li><a href="#">Contact</a></li> <li><a href="#">Get a quote</a></li> </ul>
我們省略了“家”的描述,因為沒有什么可描述的。當我們在其他項目上懸停時,滑動的div應該出現。
CSS
首先,我們將對菜單及其導航項目進行樣式化,然后我們將對這些描述元素進行樣式化。
讓我們重新設置一些樣式:
body, ul, li, h1, h2, span{ margin:0; padding:0; } ul{ list-style:none; }
背景是暗灰色的:
body{ background:#292929;}
菜單上的東西的列表會完全放在屏幕的右邊:
.slidingMenu { position: absolute; height:410px; width: 410px; top:40px; overflow:hidden; right:1px; font-family: Arial, Helvetica, sans-serif; }
菜單項用于右浮動:
.slidingMenu li { display:block; float:right; clear:both; position:relative; overflow:hidden;}
" mover "元素的位置是絕對的,我們會將它動態地放置在頂部和寬度上:
.slidingMenu li.move { width: 9px; height: 68px; right:0px; padding-right:10px; margin-top:2px; z-index: 8; position: absolute; background: #2183c4; background: -webkit-gradient( linear, left top, left bottom, from(#0771b8), to(#2183c4) ); background: -moz-linear-gradient( top, #0771b8, #2183c4 ); -moz-border-radius: 8px 0px 0px 8px; -webkit-border-top-left-radius: 8px; -webkit-border-bottom-left-radius: 8px; border-top-left-radius: 8px; border-bottom-left-radius: 8px; -moz-box-shadow:1px 1px 5px #000; -webkit-box-shadow:1px 1px 5px #000; box-shadow:1px 1px 5px #000; }
我們將以非常微妙的背景梯度和一些盒式陰影來提供這種移動的懸停元素。
鏈接元素的樣式如下:
.slidingMenu li a { font-size:66px; text-transform:uppercase; text-decoration: none; color: #ddd; outline: none; text-indent:5px; z-index: 10; display: block; float: right; height: 66px; line-height: 66px; position: relative; overflow: hidden; padding-right:10px;}
這些描述將在一個相對定位的容器中。我們將margin-top設置為相同的值的頂部菜單列表:
/* Descriptions */ .slidingMenuDesc{ margin-top:40px; position:relative; }
內部具有描述跨度的div將具有與移動器相同的背地梯度以及相同的盒式陰影。圓形的邊界將會在相反的角落:
.slidingMenuDesc div{background: #2183c4;background:-webkit-gradient(linear,left top,left bottom,from(#0771b8),to(#2183c4));background:-moz-linear-gradient(top,#0771b8,#2183c4);height: 68px;padding-right:5px;left:-5px;width:0px;margin-top:2px;overflow:hidden;position:absolute;-moz-box-shadow:1px 1px 5px #000;-webkit-box-shadow:1px 1px 5px #000;box-shadow:1px 1px 5px #000;-moz-border-radius: 0px 8px 8px 0px;-webkit-border-top-right-radius: 8px;-webkit-border-bottom-right-radius: 8px;border-top-right-radius: 8px;border-bottom-right-radius: 8px;}
我們需要將這些元素設置為絕對的,因為我們將把頂部調整為當前列表元素,我們將在這里懸停。
描述張成的空間也將是絕對的。這不是必需的,但它給你更多的選擇如果你想申請其他動畫效果:
.slidingMenuDesc div span { font-size:36px; color: #f0f0f0; text-indent:5px; z-index: 10; display: block; height: 66px; line-height: 66px; position:absolute; right:10px; margin-left:5px; top:-3px; }
現在,讓我們來看一下JavaScript !
JavaScript
首先,我們將把下列腳本添加到我們的HTML頭部:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script src="cufon-yui.js" type="text/javascript"></script><script src="BabelSans_500.font.js" type="text/javascript"></script><script src="jquery.easing.1.3.js" type="text/javascript"></script>
我們將添加以下腳本:
$(function() { Cufon.replace('a, span').CSS.ready(function() { var $menu = $("#slidingMenu"); /** * the first item in the menu, * which is selected by default */ var $selected = $menu.find('li:first'); /** * this is the absolute element, * that is going to move across the menu items * It has the width of the selected item * and the top is the distance from the item to the top */ var $moving = $('<li />',{ className : 'move', top : $selected[0].offsetTop + 'px', width : $selected[0].offsetWidth + 'px' }); /** * each sliding div (descriptions) will have the same top * as the corresponding item in the menu */ $('#slidingMenuDesc > div').each(function(i){ var $this = $(this); $this.css('top',$menu.find('li:nth-child('+parseInt(i+2)+')')[0].offsetTop + 'px'); }); /** * append the absolute div to the menu; * when we mouse out from the menu * the absolute div moves to the top (like initially); * when hovering the items of the menu, we move it to its position */ $menu.bind('mouseleave',function(){ moveTo($selected,400); }) .append($moving) .find('li') .not('.move') .bind('mouseenter',function(){ var $this = $(this); var offsetLeft = $this.offset().left - 20; //slide in the description $('#slidingMenuDesc > div:nth-child('+ parseInt($this.index()) +')').stop(true).animate({'width':offsetLeft+'px'},400, 'easeOutExpo'); //move the absolute div to this item moveTo($this,400); }) .bind('mouseleave',function(){ var $this = $(this); var offsetLeft = $this.offset().left - 20; //slide out the description $('#slidingMenuDesc > div:nth-child('+ parseInt($this.index()) +')').stop(true).animate({'width':'0px'},400, 'easeOutExpo'); });; /** * moves the absolute div, * with a certain speed, * to the position of $elem */ function moveTo($elem,speed){ $moving.stop(true).animate({ top : $elem[0].offsetTop + 'px', width : $elem[0].offsetWidth + 'px' }, speed, 'easeOutExpo'); } }) ; });
在我們將字體(所有的“a”元素和所有“span”元素)化之后,主函數就會被執行。我們在默認情況下選擇了第一項,那就是我們的“Home”。當我們懸停在菜單項上時,我們將移動li。移動到正確的位置,并從描述項中滑出。
這是它!我們希望你喜歡它并且覺得它很有用!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。