JavaScript自定義插件實(shí)現(xiàn)tabs切換功能
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)tabs切換功能的具體代碼,供大家參考,具體內(nèi)容如下
自定義插件實(shí)現(xiàn)tabs切換功能
這是HTML代碼:
<script src='http://www.gepszalag.com/bcjs/jquery-3.1.0.js'></script> <script src='http://www.gepszalag.com/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. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息2. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)3. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))4. 刪除docker里建立容器的操作方法5. SpringMVC+Jquery實(shí)現(xiàn)Ajax功能6. JavaScript實(shí)現(xiàn)組件化和模塊化方法詳解7. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法8. 關(guān)于Ajax跨域問題及解決方案詳析9. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲10. ASP中if語句、select 、while循環(huán)的使用方法
