jQuery實(shí)現(xiàn)tab欄切換效果
本文實(shí)例為大家分享了jQuery實(shí)現(xiàn)tab欄切換效果的具體代碼,供大家參考,具體內(nèi)容如下
具體實(shí)現(xiàn)功能
1、切換選項(xiàng)卡
2、添加選項(xiàng)卡
3、刪除選項(xiàng)卡
4、編輯選項(xiàng)卡
html結(jié)構(gòu)
<div class="tabsbox" id="tab"> <!-- tab標(biāo)簽 --> <nav class="firstnav"> <!-- tab欄標(biāo)題 --> <ul> <li class="liactive"> <span>測(cè)試1</span> <span class="close">×</span> </li> <li><span>測(cè)試2</span><span class="close">×</span></li> <li><span>測(cè)試3</span><span class="close">×</span></li> </ul> <!-- 添加按鈕 --> <div class="tabadd"> <span>+</span> </div> </nav> <!-- tab 內(nèi)容 --> <div class="tabscon"> <section class="conactive">內(nèi)容1</section> <section>內(nèi)容2</section> <section>內(nèi)容3</section> </div> </div>
css部分
* { margin: 0; padding: 0; } main { width: 900px; margin: 0px auto; } h4 { text-align: center; line-height: 50px; } .tabsbox { width: 800px; margin: 0 auto; border: 1px solid orange; } .firstnav { position: relative; line-height: 40px; height: 40px; text-align: center; border-bottom: 1px solid #999; } .firstnav li { list-style: none; width: 100px; float: left; border-right: 1px solid #999; position: relative; cursor: pointer; } .firstnav li span { /* 阻止用戶選中文字 */ user-select: none; } .firstnav li.liactive::after { content: ""; width: 100%; height: 2px; position: absolute; z-index: 11; bottom: -2px; left: 0; background: #fff; } .firstnav .close { cursor: pointer; position: absolute; right: 2px; top: 2px; font-size: 14px; color: #666; border: 1px solid #ccc; width: 14px; height: 14px; line-height: 12px; text-align: center; border-radius: 100%; } .tabscon { height: 300px; white-space: normal; } .tabscon input { width: 100%; height: 80px; } .tabscon section { padding: 30px; display: none; } .tabscon section.conactive { display: block; } .tabadd { position: absolute; padding: 0 10px; right: 10px; top: 0px; font-size: 20px; cursor: pointer; user-select: none; } input { width: 50px; line-height: 20px; }
jQuery部分
$(function() { // 點(diǎn)擊切換 $('ul').on('click', 'li', function() { // 切換選項(xiàng)卡 $(this).addClass('liactive').siblings().removeClass('liactive') // 獲取點(diǎn)擊的li的索引值 var index = $(this).index() // 排他思想讓內(nèi)容顯示與隱藏 $('.tabscon section').eq(index).stop().show().siblings().hide() }) // 添加選項(xiàng)卡 $('.tabadd').click(function() { // 只能創(chuàng)建7個(gè) if ($('li').length >= 7) { return } var li = ` <li><span>New Tab</span><span class="close">×</span></li> ` var section = ` <section>新增內(nèi)容</section> ` // 把新增的li添加到ul $('ul').append(li) // 把新增的內(nèi)容添加到tabscon $('.tabscon').append(section) // $('ul li').length - 1這里獲取的是索引從0開(kāi)始 $('ul').children().eq($('ul li').length - 1).click() }) // 刪除選項(xiàng)卡 $('ul').on('click', '.close', function() { // 獲取當(dāng)前l(fā)i的索引 var index = $(this).parent('li').index() // console.log(index) // 要進(jìn)行判斷 // 三個(gè)狀態(tài): // 1.選中的還是未選中的,不選中不管它 // 2.選中的情況刪除的是第一個(gè),點(diǎn)擊后面一個(gè) // 3.只剩一個(gè)不管 // 如果當(dāng)前的li是被點(diǎn)擊的 if ($(this).parent().hasClass('liactive')) { // 如果當(dāng)前被點(diǎn)擊的不是第一個(gè)li if (index != 0) { // 讓它上一個(gè)li被點(diǎn)擊 $(this).parent('li').prev().click() // 如果li不止一個(gè)讓下一個(gè)li點(diǎn)擊 } else if ($('ul li').length != 1) { $(this).parent('li').next().click() } } // 移除當(dāng)前l(fā)i $(this).parent().remove() // 移除當(dāng)前的內(nèi)容 $('.tabscon section').eq(index).remove() }) // 編輯選項(xiàng)卡 $('ul').on('dblclick', 'li', function() { // 選中l(wèi)i子元素的第一個(gè)span $(this).children().eq(0).html('<input type="text" value="' + $(this).children().eq(0).text() + '"/>') // 選中輸入框中的文字 $(this).find('input').select() // 失去焦點(diǎn)讓span的值等于輸入框中的值 $('input').blur(function() { // 讓span的值等于輸入框中的值 $(this).parent().text($(this).val()) }) // 鍵盤(pán)抬起事件 $('input').keyup(function(e) { // 按下回車失去焦點(diǎn) if (e.keyCode == 13) { $(this).blur() } }) }) })
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于jQuery實(shí)現(xiàn)的查看全文功能【實(shí)用】
本文分享了利用jQuery實(shí)現(xiàn)的查看全文功能:文本內(nèi)容少于四行,不顯示查看全文;超過(guò)五行時(shí)才顯示出來(lái)并有此功能;很實(shí)用,下面就跟小編一起來(lái)看看吧2016-12-12jQuery學(xué)習(xí)心得總結(jié)(必看篇)
下面小編就為大家?guī)?lái)一篇jQuery學(xué)習(xí)心得總結(jié)(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06JQuery給元素添加/刪除節(jié)點(diǎn)比如select
本教程詳細(xì)介紹下jQuery添加/刪除Select的Option項(xiàng),感興趣的各位可以參考下哈2013-04-0430個(gè)經(jīng)典的jQuery代碼開(kāi)發(fā)技巧
這篇文章主要介紹了30個(gè)經(jīng)典的jQuery代碼開(kāi)發(fā)技巧,包含了常見(jiàn)屬性、方法、元素等的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12jquery easyui combox一些實(shí)用的小方法
這篇文章主要介紹了jquery easyui combox一些實(shí)用的小方法,有需要的朋友可以參考一下2013-12-12jQuery通用的全局遍歷方法$.each()用法實(shí)例
這篇文章主要介紹了jQuery通用的全局遍歷方法$.each()用法,結(jié)合實(shí)例形式分析了$.each()方法實(shí)現(xiàn)遍歷功能的相關(guān)技巧,需要的朋友可以參考下2016-07-07jQuery插件ImageDrawer.js實(shí)現(xiàn)動(dòng)態(tài)繪制圖片動(dòng)畫(huà)(附源碼下載)
ImageDrawer.js是一款可以實(shí)現(xiàn)動(dòng)態(tài)繪制圖片動(dòng)畫(huà)的jQuery插件,接下來(lái)通過(guò)本文給大家介紹jQuery插件ImageDrawer.js實(shí)現(xiàn)動(dòng)態(tài)繪制圖片動(dòng)畫(huà)(附源碼下載),需要的朋友參考下2016-02-02jQuery打印指定區(qū)域Html頁(yè)面并自動(dòng)分頁(yè)
項(xiàng)目中需要用到打印HTML頁(yè)面,需要指定區(qū)域打印,使用jquery.PrintArea.js 插件實(shí)現(xiàn)分頁(yè),需要的朋友可以參考下2014-07-07