jQuery实现上下滑动的tab
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.mod-tab {
border: 1px solid #ccc;
margin-top: 30px;
}
.mod-tab * {
box-sizing: border-box;
}
.mod-tab ul, .mod-tab li {
margin: 0;
padding: 0;
list-style: none;
}
.mod-tab .tabs:after {
content: '';
height: 0;
display: table;
clear: both;
}
.mod-tab .tabs li{
float: left;
width: 33.3%;
height: 30px;
line-height: 30px;
text-align: center;
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
background-color: #fff;
cursor: pointer;
}
.mod-tab .tabs li:last-child{
border-right: none;
}
.mod-tab .tabs .active {
background: #eee;
}
.mod-tab .wrap {
position: relative;
height: 180px;
overflow: hidden;
}
.mod-tab .panels {
position: absolute;
top: -40px;
width: 100%;
}
.mod-tab .panels>li {
height: 180px;
padding: 20px;
}
.mod-tab .panels>li:nth-child(1){
background: red;
}
.mod-tab .panels>li:nth-child(2){
background: green;
}
.mod-tab .panels>li:last-child{
background: yellow;
}
.mod-tab .active {
display: block;
}
</style>
</head>
<body>
<div class = "mod-tab">
<ul class = "tabs">
<li class = "active">tab1</li>
<li>tab2</li>
<li>tab3</li>
</ul>
<div class = "wrap">
<ul class = "panels">
<li>内容 1</li>
<li>内容 2</li>
<li>内容 3</li>
</ul>
</div>
</div>
<script src = "http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script>
<script>
$('.tabs>li').on('click',function(){
$('.tabs>li').on('click',function(){
//防止函数的重复调用
var $this = $(this)
//获取当前点击元素的下表,index()获取的是当前元素在兄弟元素排行中的顺序
var index = $this.index()
//使触发点击事件的元素添加active,其兄弟元素移除active
var panelHeight = $('.mod-tab .wrap').height()
$this.addClass('active').siblings().removeClass('active')
$this.parents('.mod-tab').find('.panels')
.animate({top: -panelHeight*index})
})
})
</script>
</body>
</html>
网友评论