JS實(shí)現(xiàn)選項(xiàng)卡插件的兩種寫法(jQuery和class)
本文實(shí)例為大家分享了JS實(shí)現(xiàn)選項(xiàng)卡插件的2種寫法,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)插件的幾個(gè)注意點(diǎn):
(1)定義一個(gè)固定的html結(jié)構(gòu),比如選項(xiàng)卡的標(biāo)題的標(biāo)簽為為li,每個(gè)選項(xiàng)卡的內(nèi)容的標(biāo)簽為div等等;
(2)選中時(shí)的樣式提前確定;
(3)最好先用簡單的JS實(shí)現(xiàn)選項(xiàng)卡的功能,再改為插件的模式。
html結(jié)構(gòu)如下:
<style>
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
#tabBox {
box-sizing: border-box;
margin: 20px auto;
width: 500px;
}
.navBox {
display: flex;
position: relative;
top: 1px;
}
.navBox li {
box-sizing: border-box;
margin-right: 10px;
padding: 0 10px;
line-height: 35px;
border: 1px solid #999;
cursor: pointer;
}
.navBox li.active {
border-bottom-color: #FFF;
}
#tabBox>div {
display: none;
box-sizing: border-box;
padding: 10px;
height: 150px;
border: 1px solid #999;
}
#tabBox>div.active {
display: block;
}
</style>
<div id="tabBox">
<ul class="navBox">
<li class="active">編程</li>
<li>讀書</li>
<li>運(yùn)動</li>
</ul>
<div class="active">編程使我快樂</div>
<div>讀書使我幸福</div>
<div>運(yùn)動使我健康</div>
</div>
先用JS實(shí)現(xiàn)選項(xiàng)卡的功能:
let len = liList.length;
for(let i = 0; i < len; i++) {
liList[i].index = i;
liList[i].onclick = function() {
for(let j = 0; j < len; j++) {
if(j === this.index) {
liList[j].className = 'active';
contentList[j].className = 'active';
}
else{
liList[j].className = '';
contentList[j].className = '';
}
}
};
}
實(shí)現(xiàn)插件的第一種方法:jQuery
利用$.fn.extend方法,在jQuery上擴(kuò)展一個(gè)選項(xiàng)卡功能的方法:
(function($){
function clickLi() {
let $this = this,
$navBox = $this.find('.navBox'),
$liList = $navBox.find('li'),
$contentList = $this.find('div');
$liList.click(function(){
let $this = $(this),
index = $this.index();
$this.addClass('active').siblings().removeClass('active');
$contentList.eq(index).addClass('active').siblings().removeClass('active');
});
}
$.fn.extend({
tabClick: clickLi
});
})(jQuery);
使用方法:
let $tabBox = $('#tabBox');
$tabBox.tabClick();
實(shí)現(xiàn)插件的第二種方法:class
利用ES6中的class類,創(chuàng)建一個(gè)選項(xiàng)卡類Tab,并添加屬性和方法,并且可以多參數(shù)傳遞實(shí)現(xiàn)選項(xiàng)卡:
(function(){
class Tab {
constructor(selector, options) {
// 處理第一個(gè)參數(shù)
if(!selector)
throw new ReferenceError('The first selector parameter must be passed~~');
if(typeof selector === 'string')
this.container = document.querySelector(selector);
else if(selector.nodeType)
this.container = selector;
this.initialParams(options);
this.navBox = this.container.querySelector('.navBox'),
this.liList = this.navBox.querySelectorAll('li'),
this.contentList = this.container.querySelectorAll('div'),
this.count = this.liList.length;
this.change();
this.handleLi();
}
// 初始化參數(shù)
initialParams(options) {
let _default = {
showIndex: 0,
triggerEvent: 'click'
};
for(let key in options) {
if (!options.hasOwnProperty(key)) break;
_default[key] = options[key];
}
// 把信息掛載到實(shí)例上
for (let key in _default) {
if (!_default.hasOwnProperty(key)) break;
this[key] = _default[key];
}
}
// 切換標(biāo)題
change() {
[].forEach.call(this.liList, (item, index) => {
if(index === this.showIndex) {
this.liList[index].className = 'active';
this.contentList[index].className = 'active';
return;
}
this.liList[index].className = '';
this.contentList[index].className = '';
});
}
// 綁定標(biāo)題對應(yīng)的事件
handleLi() {
[].forEach.call(this.liList, (item, index) => {
item.addEventListener(this.triggerEvent, () => {
this.showIndex = index;
this.change();
});
});
}
}
window.Tab = Tab;
})();
使用方法:
new Tab('#tabBox', {
showIndex: 2,
triggerEvent: 'mouseenter'
});
第二種方法是現(xiàn)在常用的方法,因?yàn)樗梢詡鬟f很多參數(shù)。可以根據(jù)需求,設(shè)置默認(rèn)要傳遞的參數(shù),將這個(gè)選項(xiàng)卡插件做的更完善。
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊兩個(gè)精彩的專題:javascript選項(xiàng)卡操作方法匯總 jquery選項(xiàng)卡操作方法匯總
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序授權(quán)登錄實(shí)現(xiàn)方案wx.getUserProfile(2022年最新版)
微信在最近開始要求使用wx.getUserProfile()來獲取用戶的昵稱,頭像等信息,所以下面這篇文章主要給大家介紹了關(guān)于2022年最新版微信小程序授權(quán)登錄實(shí)現(xiàn)方案wx.getUserProfile的相關(guān)資料,需要的朋友可以參考下2022-11-11
javascript 用記憶函數(shù)快速計(jì)算遞歸函數(shù)
摘自《JavaScript: The Good Parts》,作為讀書筆記備用。對于追求執(zhí)行效率的朋友可以參考下。2010-03-03
JavaScript在IE和Firefox瀏覽器下的7個(gè)差異兼容寫法小結(jié)
盡管那需要用長串的、沉悶的不同分支代碼來應(yīng)付不同瀏覽器的日子已經(jīng)過去,偶爾還是有必要做一些簡單的區(qū)分和目標(biāo)檢測來確保某塊代碼能在用戶的機(jī)器上正常運(yùn)行。2010-06-06
按下回車鍵指向下一個(gè)位置的一個(gè)函數(shù)代碼
本篇文章主要是對按下回車鍵指向下一個(gè)位置的一個(gè)函數(shù)代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-03-03
Bootstrap 模態(tài)框(Modal)插件代碼解析
Bootstrap 模態(tài)框(Modal)插件 模態(tài)框(Modal)是覆蓋在父窗體上的子窗體。這篇文章主要介紹了Bootstrap 模態(tài)框(Modal)插件代碼解析的相關(guān)資料,需要的朋友可以參考下2016-12-12
showModalDialog在谷歌瀏覽器下會返回Null的解決方法
showModalDialog的返回值在IE、火狐下面都能夠獲取返回值,但是在谷歌瀏覽器下面會返回Null,下面有個(gè)不錯(cuò)的解決方法,感興趣的朋友可以參考下2013-11-11
ES6 proxy和reflect的使用方法與應(yīng)用實(shí)例分析
這篇文章主要介紹了ES6 proxy和reflect的使用方法,結(jié)合具體實(shí)例形式分析了ES6 proxy和reflect基本功能、原理、使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-02-02
vue2.0實(shí)戰(zhàn)之基礎(chǔ)入門(1)
這篇文章主要為大家詳細(xì)介紹了vue2.0實(shí)戰(zhàn)第一篇基礎(chǔ)入門的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

