jQuery制作網(wǎng)頁版選項(xiàng)卡
網(wǎng)頁選項(xiàng)卡可以較好的利用有限的頁面來展示更多的元素,而使用JQuery來制作網(wǎng)頁選項(xiàng)卡也是一件非常簡(jiǎn)單的事情。今天就來分享一個(gè)網(wǎng)頁選項(xiàng)卡的制作小技巧。
◦引入所需庫
◦選項(xiàng)卡原理
◦業(yè)務(wù)核心
◦完整小例子
引入所需庫
這個(gè)知識(shí)點(diǎn)很基礎(chǔ),但是為了照顧fresh man ,我還是寫一下吧。
選項(xiàng)卡原理
選項(xiàng)卡出現(xiàn)其實(shí)只是某一個(gè)div獲取到了顯示的權(quán)限,其他的沒有顯示而已。
•網(wǎng)頁代碼
<div class="tab"> <div class="tab_menu"> <ul> <li class="selected">選項(xiàng)卡1</li> <li>選項(xiàng)卡2</li> <li>選項(xiàng)卡3</li> </ul> </div><br><br><br> <div class="tab_box"> <div>選項(xiàng)卡1:這里是1號(hào)內(nèi)容區(qū)域</div> <div class="hide">選項(xiàng)卡2:這里是2號(hào)內(nèi)容區(qū)域</div> <div class="hide">選項(xiàng)卡3:這里是3號(hào)內(nèi)容區(qū)域</div> </div> </div>
•添加點(diǎn)樣式元素
<style> // 使得UL中的li標(biāo)簽水平排列 ul { display:inline; white-space: nowrap; } li { margin:3px; float:left; background:red; // 這樣可以防止li標(biāo)簽出現(xiàn)換行的顯示 display:inline-block; } .tab_menu { list-style:none; /* 去掉ul前面的符號(hào) */ margin: 0px; /* 與外界元素的距離為0 */ padding: 0px; /* 與內(nèi)部元素的距離為0 */ width: auto; /* 寬度根據(jù)元素內(nèi)容調(diào)整 */ } .tab_box { background-color: #465c71; /* 背景色 */ border: 1px #4e667d solid; /* 邊框 */ color: #dde4ec; /* 文字顏色 */ display: block; /* 此元素將顯示為塊級(jí)元素,此元素前后會(huì)帶有換行符 */ line-height: 1.35em; /* 行高 */ padding: 4px 20px; /* 內(nèi)部填充的距離 */ text-decoration: none; /* 不顯示超鏈接下劃線 */ white-space: nowrap; /* 對(duì)于文本內(nèi)的空白處,不會(huì)換行,文本會(huì)在在同一行上繼續(xù),直到遇到 <br> 標(biāo)簽為止。 */ } .selected { background-color: green; display: block; } .hide { display: none; } </style>
•JQuery控制
<script> // 加上鼠標(biāo)的點(diǎn)擊效果 $(function(){ $("ul li").click(function(){ $(this).addClass("selected").siblings().removeClass("selected"); var index = $("ul li").index(this); $("div.tab_box > div").eq(index).show().siblings().hide(); }) }) // 加上鼠標(biāo)懸浮效果 $(function(){ $("div.tab_menu ul li").hover(function(){ $(this).addClass("selected").show(); // 下面的這個(gè)可以實(shí)現(xiàn)選項(xiàng)卡的聯(lián)動(dòng)效果 var index = $("ul li").index(this); $("div.tab_box > div").eq(index).show().siblings().hide(); },function(){ $(this).removeClass("selected").show(); // 下面的這個(gè)可以實(shí)現(xiàn)選項(xiàng)卡的聯(lián)動(dòng)效果 var index = $("ul li").index(this); $("div.tab_box > div").eq(index).show().siblings().hide(); }) }) </script>
業(yè)務(wù)核心
里面最關(guān)鍵的其實(shí)還是最為基礎(chǔ)的JQuery選擇器的使用,然后我們就可以動(dòng)態(tài)的改變頁面上的元素,從而實(shí)現(xiàn)我們想要的效果。這也是JQuery的強(qiáng)大之處。
這里比較有技巧性的就是通過對(duì)ul li樣式的變換,實(shí)現(xiàn)了類似于導(dǎo)航欄的效果。我們可以借鑒到今后的開發(fā)之中。這是一個(gè)非常實(shí)用的小技巧。
// 使得UL中的li標(biāo)簽水平排列 ul { display:inline; white-space: nowrap; } li { margin:3px; float:left; background:red; display:inline-block; }
完整小例子
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Tab Host Demo</title> <script type="text/javascript" src="jquery-2.2.4.min.js"></script> <style> // 使得UL中的li標(biāo)簽水平排列 ul { display:inline; white-space: nowrap; } li { margin:3px; float:left; background:red; display:inline-block; } .tab_menu { list-style:none; /* 去掉ul前面的符號(hào) */ margin: 0px; /* 與外界元素的距離為0 */ padding: 0px; /* 與內(nèi)部元素的距離為0 */ width: auto; /* 寬度根據(jù)元素內(nèi)容調(diào)整 */ } .tab_box { background-color: #465c71; /* 背景色 */ border: 1px #4e667d solid; /* 邊框 */ color: #dde4ec; /* 文字顏色 */ display: block; /* 此元素將顯示為塊級(jí)元素,此元素前后會(huì)帶有換行符 */ line-height: 1.35em; /* 行高 */ padding: 4px 20px; /* 內(nèi)部填充的距離 */ text-decoration: none; /* 不顯示超鏈接下劃線 */ white-space: nowrap; /* 對(duì)于文本內(nèi)的空白處,不會(huì)換行,文本會(huì)在在同一行上繼續(xù),直到遇到 <br> 標(biāo)簽為止。 */ } .selected { background-color: green; display: block; } .hide { display: none; } </style> </head> <body> <div class="tab"> <div class="tab_menu"> <ul> <li class="selected">選項(xiàng)卡1</li> <li>選項(xiàng)卡2</li> <li>選項(xiàng)卡3</li> </ul> </div><br><br><br> <div class="tab_box"> <div>選項(xiàng)卡1:這里是1號(hào)內(nèi)容區(qū)域</div> <div class="hide">選項(xiàng)卡2:這里是2號(hào)內(nèi)容區(qū)域</div> <div class="hide">選項(xiàng)卡3:這里是3號(hào)內(nèi)容區(qū)域</div> </div> </div> <script> // 加上鼠標(biāo)的點(diǎn)擊效果 $(function(){ $("ul li").click(function(){ $(this).addClass("selected").siblings().removeClass("selected"); var index = $("ul li").index(this); $("div.tab_box > div").eq(index).show().siblings().hide(); }) }) // 加上鼠標(biāo)懸浮效果 $(function(){ $("div.tab_menu ul li").hover(function(){ $(this).addClass("selected").show(); // 下面的這個(gè)可以實(shí)現(xiàn)選項(xiàng)卡的聯(lián)動(dòng)效果 var index = $("ul li").index(this); $("div.tab_box > div").eq(index).show().siblings().hide(); },function(){ $(this).removeClass("selected").show(); // 下面的這個(gè)可以實(shí)現(xiàn)選項(xiàng)卡的聯(lián)動(dòng)效果 var index = $("ul li").index(this); $("div.tab_box > div").eq(index).show().siblings().hide(); }) }) </script> </body> </html>
實(shí)現(xiàn)的效果如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于jquery實(shí)現(xiàn)最簡(jiǎn)單的選項(xiàng)卡切換效果
- jquery編寫Tab選項(xiàng)卡滾動(dòng)導(dǎo)航切換特效
- jQuery實(shí)現(xiàn)TAB選項(xiàng)卡切換特效簡(jiǎn)單演示
- jquery實(shí)現(xiàn)具有嵌套功能的選項(xiàng)卡
- jQuery實(shí)現(xiàn)選項(xiàng)卡切換效果簡(jiǎn)單演示
- jQuery實(shí)現(xiàn)Tab選項(xiàng)卡切換效果簡(jiǎn)單演示
- 基于JQuery的6個(gè)Tab選項(xiàng)卡插件
- jQuery EasyUI API 中文文檔 - Tabs標(biāo)簽頁/選項(xiàng)卡
- jquery tools之tabs 選項(xiàng)卡/頁簽
- jQuery之選項(xiàng)卡的簡(jiǎn)單實(shí)現(xiàn)
相關(guān)文章
修復(fù)jQuery tablesorter無法正確排序的bug(加千分位數(shù)字后)
這篇文章主要介紹了如何修復(fù)jQuery tablesorter無法正確排序的bug(加千分位數(shù)字后)的相關(guān)資料,需要的朋友可以參考下2016-03-03jQuery+ThinkPHP+Ajax實(shí)現(xiàn)即時(shí)消息提醒功能實(shí)例代碼
這篇文章主要介紹了jQuery+ThinkPHP+Ajax實(shí)現(xiàn)即時(shí)消息提醒功能的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-03-03使用ajaxfileupload.js實(shí)現(xiàn)上傳文件功能
這篇文章主要為大家詳細(xì)介紹了使用ajaxfileupload.js實(shí)現(xiàn)上傳文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08JQuery入門——事件切換之toggle()方法應(yīng)用介紹
在toggle()方法中,可以依次調(diào)用N個(gè)指定的函數(shù),直到最后一個(gè)函數(shù),然后重復(fù)對(duì)這個(gè)函數(shù)輪番調(diào)用,在函數(shù)之間切換調(diào)用的時(shí)候相當(dāng)?shù)姆奖?接下來將會(huì)詳細(xì)介紹toggle()方法的使用,感興趣的你可不要錯(cuò)過了啊2013-02-02jQuery不使用插件及swf實(shí)現(xiàn)無刷新文件上傳
這篇文章主要介紹了jQuery不使用插件及swf實(shí)現(xiàn)無刷新文件上傳,需要的朋友可以參考下2014-12-12jQuery去掉字符串起始和結(jié)尾的空格(多種方法實(shí)現(xiàn))
去掉字符串起始和結(jié)尾的空格在實(shí)際應(yīng)用中時(shí)很常見的的功能,本教程以多種方法為大家介紹下去掉空格的方法,感興趣的朋友可以參考下哈2013-04-04