蝙蝠岛资源网 Design By www.hbtsch.com
本文实例为大家分享了JS实现选项卡插件的2种写法,供大家参考,具体内容如下
实现插件的几个注意点:
(1)定义一个固定的html结构,比如选项卡的标题的标签为为li,每个选项卡的内容的标签为div等等;
(2)选中时的样式提前确定;
(3)最好先用简单的JS实现选项卡的功能,再改为插件的模式。
html结构如下:
<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>运动</li>
</ul>
<div class="active">编程使我快乐</div>
<div>读书使我幸福</div>
<div>运动使我健康</div>
</div>
先用JS实现选项卡的功能:
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 = '';
}
}
};
}
实现插件的第一种方法:jQuery
利用$.fn.extend方法,在jQuery上扩展一个选项卡功能的方法:
(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();
实现插件的第二种方法:class
利用ES6中的class类,创建一个选项卡类Tab,并添加属性和方法,并且可以多参数传递实现选项卡:
(function(){
class Tab {
constructor(selector, options) {
// 处理第一个参数
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();
}
// 初始化参数
initialParams(options) {
let _default = {
showIndex: 0,
triggerEvent: 'click'
};
for(let key in options) {
if (!options.hasOwnProperty(key)) break;
_default[key] = options[key];
}
// 把信息挂载到实例上
for (let key in _default) {
if (!_default.hasOwnProperty(key)) break;
this[key] = _default[key];
}
}
// 切换标题
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 = '';
});
}
// 绑定标题对应的事件
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'
});
第二种方法是现在常用的方法,因为它可以传递很多参数。可以根据需求,设置默认要传递的参数,将这个选项卡插件做的更完善。
如果大家还想深入学习,可以点击两个精彩的专题:javascript选项卡操作方法汇总 jquery选项卡操作方法汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
js,选项卡,插件
蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com
暂无JS实现选项卡插件的两种写法(jQuery和class)的评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2025年11月06日
2025年11月06日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]