用原生js進行封裝函式 實現 選項卡巢狀選項卡 功能

Lurkers.發表於2018-06-06

效果如圖:





程式碼如下:


<!DOCTYPE html>
<html>


<head>
<meta charset="UTF-8">
<title></title>


<body>
<style type="text/css">
body,
ul,
li,
div {
padding: 0;
margin: 0;
list-style: none;
}

.box {
width: 900px;
margin: 0 auto;
border: 1px solid #CCCCCC;
height: 308px;
overflow: hidden;
}

#ul1 {
width: 200px;
float: left;
background: #CCCCCC;
height: 308px;

}

#ul1 li {
height: 77px;
line-height: 76px;
text-align: center;
font-size: 15px;
border-bottom: 1px solid #9ACD32;
}

#ul1 li.on {
background: #fff;
color: #000000;
}

.box div {
width: 700px;
float: right;
position: relative;
height: 308px;
}

.box div img {
width: 100%;
height: 308px;
}

.box div ul {
width: 100%;
height: 30px;
position: absolute;
left: 0px;
bottom: 0px;


}

.box div li {
float: left;
margin-right: 1px;
cursor: pointer;
background: #9ACD32;
line-height: 30px;
font-size: 12px;
text-align: center;
height: 40px;
}

.box div li.on {
background: pink;
}
</style>
</body>
</head>


<body>
<div class="box" id="box">
<ul id='ul1'>
<li class="on">最新團購</li>
<li>商品特惠</li>
<li>名品推薦</li>
<li>繽紛活動</li>
</ul>
<div>
<img src="img/1.jpg">
<img src="img/2.jpg">
<img src="img/3.jpg">
<ul>
<li>商品1</li>
<li>商品2</li>
<li>商品3</li>
</ul>
</div>
<div>
<img src="img/4.jpg">
<img src="img/5.jpg">
<img src="img/6.jpg">
<img src="img/7.jpg">
<ul>
<li>商品1</li>
<li>商品2</li>
<li>商品3</li>
<li>商品4</li>
</ul>
</div>
<div>
<img src="img/8.jpg">
<img src="img/1.jpg">
<img src="img/2.jpg">
<img src="img/3.jpg">
<ul>
<li>商品1</li>
<li>商品2</li>
<li>商品3</li>
<li>商品4</li>
</ul>
</div>
<div>


<img src="img/4.jpg">
<img src="img/5.jpg">
<img src="img/8.jpg">



<ul>
<li>商品1</li>
<li>商品2</li>
<li>商品3</li>


</ul>
</div>
</div>
<script type="text/javascript">
var oBox=document.getElementById("box")
var oUl1=document.getElementById("ul1")
var oLi=oUl1.getElementsByTagName("li")
var oDiv=oBox.getElementsByTagName("div")

//封裝函式begin
for (var i=0;i<oDiv.length;i++) {
fn(oDiv[i])
}

function fn(ojbk){

var oImg=ojbk.getElementsByTagName("img")
var oUl=ojbk.getElementsByTagName("ul")[0]
var aLi=ojbk.getElementsByTagName("li")

//左邊選項卡begin
for (var i=0;i<oLi.length;i++) {
oLi[i].index=i
oLi[i].onclick=function(){
for(var i=0;i<oDiv.length;i++){
oLi[i].className=""
oDiv[i].style.display="none"
}
this.className="on"
oDiv[this.index].style.display="block"
}

}
//左邊選項卡end

//下邊選項卡begin
aLi[0].className="on"
for(var i=0;i<oImg.length;i++){
aLi[i].index=i
aLi[i].onclick=function(){
for(var i=0;i<aLi.length;i++){
aLi[i].className=""
oImg[i].style.display="none"
}

this.className="on"
oImg[this.index].style.display="block"
}
}
//下邊選項卡end

//下邊選項卡寬度區均值begin
for (var i=0;i<aLi.length;i++) {
aLi[i].style.width=(700/aLi.length)-(aLi.length*1)+"px"
}
//下邊選項卡寬度區均值begin

//封裝函式end
}


</script>
</body>


</html>

相關文章