JS實(shí)現(xiàn)滑動(dòng)菜單效果代碼(包括Tab,選項(xiàng)卡,橫向等效果)
本文實(shí)例講述了JS實(shí)現(xiàn)滑動(dòng)菜單效果代碼。分享給大家供大家參考。具體如下:
這里實(shí)現(xiàn)一個(gè)特效將網(wǎng)頁(yè)中的選項(xiàng)卡滑動(dòng)門都集中到一個(gè)網(wǎng)頁(yè)中來(lái),有些同志曾經(jīng)為同一個(gè)頁(yè)面布置兩個(gè)滑動(dòng)門而煩惱,參考一下本例子,相信你會(huì)找到答案,而且有各種排列方式的選項(xiàng)卡,總有一款會(huì)滿足你。
運(yùn)行效果截圖如下:
在線演示地址如下:
http://demo.jb51.net/js/2015/js-move-tab-nav-menu-demo-style-codes/
具體代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN" xml:lang="zh-CN"> <head> <title>選項(xiàng)卡</title> <style type="text/css"> <!-- * { margin:0; padding:0; font-size:12px; font-weight:normal; } .jj { font-weight:bolder!important; } .box { border-top-color:#c00!important; } .pr { color:#060!important; } #tab01 { position:relative; width:336px; height:88px; padding-top:15px; margin:50px; overflow:hidden; } #tab01 h3 { position:relative; z-index:2; float:left; height:14px; padding:0 7px 0 8px; margin-left:-1px; border-left:solid 1px #ccc; border-right:solid 1px #fff; text-align:center; background:#fff; cursor:pointer; } #tab01 h3.up { height:18px; padding:5px 7px 0 7px; margin:-6px 0 0 0; border:solid #ccc; border-width:1px 1px 0; color:#c00; } #tab01 div { display:none; position:absolute; left:0; top:32px; z-index:1; width:324px; height:54px; padding:5px; border:solid 1px #ccc; color:#666; } #tab01 div.up { display:block; } #tab02 { position:relative; width:200px; margin:50px; border:solid #ccc; border-width:0 1px 1px; } #tab02 h4 { height:18px; line-height:18px; border:solid #ccc; border-width:1px 0; margin-bottom:-1px; text-align:center; background:#f6f6f6; cursor:pointer; } #tab02 h4.up { color:#c00; } #tab02 ol { display:none; height:54px; padding:5px; color:#666; } #tab02 ol.up { display:block; } #tab03 { position:relative; width:100px; margin:50px; } #tab03 h3 { position:relative; z-index:1; height:16px; padding-top:4px; margin-bottom:-1px; border:solid #ccc; border-width:1px 0 1px 1px; text-align:center; font-family:宋體; background:#eee; cursor:pointer; } #tab03 h3.up { z-index:3; color:#c00; background:#fff; } #tab03 div.tab { display:none; position:absolute; left:99px; top:0; z-index:2; width:300px; height:200px; padding:5px; border:solid 1px #ccc; color:#666; } #tab03 div.tab.up { display:block; } --> </style> </head> <body> <div id="tab01"> <h3>首頁(yè)</h3> <div class="jj"><p>嘿嘿,無(wú)視div原始class值。</p></div> <h3 class="pr">測(cè)試</h3> <div><p>繼續(xù)無(wú)視h3原始class值。</p></div> <h3>無(wú)聊</h3> <div><p>h3沒(méi)有值也可以~</p></div> <h3 class="box">傻蛋</h3> <div><p>div沒(méi)有值一樣可以~</p></div> </div> <div id="tab02"> <h4>首頁(yè)</h4> <ol class="pr"><li>嘿嘿,無(wú)視容器原始class值。</li></ol> <h4 class="box">測(cè)試</h4> <ol><li>繼續(xù)無(wú)視h3原始class值。</li></ol> <h4>無(wú)聊</h4> <ol><li>h3沒(méi)有值也可以~</li></ol> <h4 class="bb">傻蛋</h4> <ol><li>div沒(méi)有值一樣可以~</li></ol> </div> <div id="tab03"> <h3>首頁(yè)</h3> <div class="tab"><p>嘿嘿,無(wú)視h3原始class值。</p></div> <h3>測(cè)試</h3> <div class="tab wushi"><p>繼續(xù)無(wú)視div原始class值。</p></div> <h3>無(wú)聊</h3> <div class="tab"><p>h3沒(méi)有值也可以~</p></div> <h3 class="box">傻蛋</h3> <div class="tab tab123"><p>class值相似一樣也可以~</p><div><p>指定class后,即時(shí)再多一個(gè)div也行。</p></div></div> </div> <script type="text/javascript"> <!-- function Pid(id,tag){ if(!tag){ return document.getElementById(id); } else{ return document.getElementById(id).getElementsByTagName(tag); } } function tab(id,hx,box,iClass,s,pr){ var hxs=Pid(id,hx); var boxs=Pid(id,box); if(!iClass){ // 如果不指定class,則: boxsClass=boxs; // 直接使用box作為容器 } else{ // 如果指定class,則: var boxsClass = []; for(i=0;i<boxs.length;i++){ if(boxs[i].className.match(/\btab\b/)){// 判斷容器的class匹配 boxsClass.push(boxs[i]); } } } if(!pr){ // 如果不指定預(yù)展開(kāi)容器,則: go_to(0); // 默認(rèn)展開(kāi)序列 yy(); } else { go_to(pr); yy(); } function yy(){ for(var i=0;i<hxs.length;i++){ hxs[i].temp=i; if(!s){// 如果不指定事件,則: s="onmouseover"; // 使用默認(rèn)事件 hxs[i][s]=function(){ go_to(this.temp); } } else{ hxs[i][s]=function(){ go_to(this.temp); } } } } function go_to(pr){ for(var i=0;i<hxs.length;i++){ if(!hxs[i].tmpClass){ hxs[i].tmpClass=hxs[i].className+=" "; boxsClass[i].tmpClass=boxsClass[i].className+=" "; } if(pr==i){ hxs[i].className+=" up"; // 展開(kāi)狀態(tài):標(biāo)題 boxsClass[i].className+=" up"; // 展開(kāi)狀態(tài):容器 } else { hxs[i].className=hxs[i].tmpClass; boxsClass[i].className=boxsClass[i].tmpClass; } } } } tab("tab01","h3","div","","onclick",2); tab("tab02","h4","ol"); tab("tab03","h3","div","tab"); //--> </script> </body> </html>
希望本文所述對(duì)大家的JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
js?通過(guò)Object.defineProperty()?定義和控制對(duì)象屬性
這篇文章主要介紹了js?通過(guò)Object.defineProperty()?定義和控制對(duì)象屬性,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08微信小程序?qū)崿F(xiàn)簡(jiǎn)易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)簡(jiǎn)易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09可以自動(dòng)輪換的頁(yè)簽 tabs with auto play fucntion
HTML、CSS方面改寫了一下結(jié)構(gòu),用了一個(gè)DL javascript方面可以選擇不斷自動(dòng)循環(huán),或者只循環(huán)一次的,點(diǎn)擊以后繼續(xù)循環(huán),或者停止循環(huán)2008-02-02微信小程序?qū)崿F(xiàn)點(diǎn)擊按鈕移動(dòng)view標(biāo)簽的位置功能示例【附demo源碼下載】
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)點(diǎn)擊按鈕移動(dòng)view標(biāo)簽的位置功能,涉及微信小程序事件綁定與this.setData動(dòng)態(tài)修改data數(shù)值進(jìn)而改變view標(biāo)簽樣式的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12純javascript移動(dòng)優(yōu)先的幻燈片效果
這篇文章主要介紹了純javascript實(shí)現(xiàn)移動(dòng)優(yōu)先的幻燈片效果,感興趣的小伙伴們可以參考一下2015-11-11js 頁(yè)面刷新location.reload和location.replace的區(qū)別小結(jié)
在實(shí)際應(yīng)用的時(shí)候,重新刷新頁(yè)面的時(shí)候,我們通常使用: location.reload() 或者是 history.go(0) 來(lái)做。下面有一些相關(guān)的內(nèi)容,大家看完了就會(huì)有更多的收獲。2009-12-12