JavaScript自定義插件實(shí)現(xiàn)tabs切換功能
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)tabs切換功能的具體代碼,供大家參考,具體內(nèi)容如下
自定義插件實(shí)現(xiàn)tabs切換功能
這是HTML代碼:
<script src='http://www.leifengta.com.cn/bcjs/jquery-3.1.0.js'></script> <script src='http://www.leifengta.com.cn/bcjs/plugs/demo01.js'></script> <style>#tabs>div{ height: 200px; width: 200px; background-color: pink; display: none;}#tabs div.div-active{ display: block;}.btn-active{ background-color: orange;}</style>
這是js代碼:
(function ($) { //tabs插件 $.fn.tabs=function (options) {let defaults = { activeIndex:1, titleActive:'btn-active', contentActive:'div-active', attr:'rel'}/*合并參數(shù)*/$.extend(defaults,options);/*獲取所有按鈕*/let btns=this.find('['+defaults.attr+']');/*獲取rel中的值*/let rels=[];btns.each(function (index,element) { rels.push($(element).attr(defaults.attr));});/*獲取所有div*/let divs=this.find(rels.toString());/*判斷指定下標(biāo)是否越界*/if(defaults.activeIndex > btns.length-1){ defaults.activeIndex = 0;}/*設(shè)置默認(rèn)顯示的內(nèi)容*/btns.eq(defaults.activeIndex).addClass(defaults.titleActive);divs.eq(defaults.activeIndex).addClass(defaults.contentActive);/*給按鈕綁定單擊事件*/btns.click(function () { $(this).addClass(defaults.titleActive).siblings().removeClass(defaults.titleActive); divs.eq($(this).index()).addClass(defaults.contentActive).siblings().removeClass(defaults.contentActive);}); }})(jQuery);
最后的代碼截屏
1.默認(rèn)

2.點(diǎn)擊進(jìn)行切換:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳解Android studio 動(dòng)態(tài)fragment的用法2. 解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題3. 圖文詳解vue中proto文件的函數(shù)調(diào)用4. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)5. php模擬實(shí)現(xiàn)斗地主發(fā)牌6. 什么是python的自省7. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)8. vue 使用localstorage實(shí)現(xiàn)面包屑的操作9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備