您现在的位置:首页 >> 前端 >> 内容

京东轮播:选项卡里套选项卡

时间:2017/1/12 10:31:29 点击:

  核心提示:HTML!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN https://www.w3.org/TR/xhtml1/DTD/xht...

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<p id="tab">
    <ul class="title">
    </ul>
    <p class="content">
        <p class="main">
            <img>
            <ul>
            </ul>
        </p>
    </p>
</p>
</body>
</html>

CSS

* {
	margin: 0;
	padding: 0;
}

ul {
	list-style: none;
}

img {
	border: 0;
}

#tab {
	width: 1000px;
	height: 500px;
	margin: 50px auto;
}

#tab .title {
	width: 200px;
	height: 500px;
	display: inline-block;
}

#tab .title li {
	width: 200px;
	height: 125px;
	float: left;
	line-height: 125px;
	text-align: center;
	font-size: 30px;
	background: #f1f1f1;
	border-bottom: 1px dotted #000;
}

#tab .title li.active {
	background: #fff;
}

#tab .title li:hover {
	background: #fff;
}

#tab .content {
	width: 799px;
	height: 500px;
	float: right;
	display: inline-block;
	position: relative;
	border: none;
	border: none;
}

#tab .content img {
	position: absolute;
	top: 0;
	left: 0;
	width: 799px;
	height: 500px;
}

#tab .content ul {
	position: absolute;
	width: 799px;
	height: 45px;
	bottom: 0;
	left: 0;
	filter(alpha: 80);
	opacity: 0.8;
	background: #000;
}

#tab .content ul li {
	float: left;
	height: 45px;
	line-height: 45px;
	background: #d5d5d5;
	border-right: 1px solid #fff;
	cursor: pointer;
	text-align: center;
}

#tab .content ul li.active {
	background: red;
}

#tab .content ul li:hover {
	background: red;
}

JavaScript

window.onload = function() {
    var tab = document.getElementById('tab');
    var title = tab.getElementsByClassName('title')[0];
    var titleLi = title.getElementsByTagName('li');
    var main = tab.getElementsByClassName('main')[0];
    var img = main.getElementsByTagName('img')[0];
    var ul = main.getElementsByTagName('ul')[0];
    var subtitleLi = ul.getElementsByTagName('li');
    var arr = [{
        title: '最热团购',
        subtitle: ['最热团购1', '最热团购2', '最热团购3'],
        pic: ['img/tab/1-1.png', 'img/tab/1-2.png', 'img/tab/1-3.png']
    },
    {
        title: '商城特惠',
        subtitle: ['商城特惠1', '商城特惠2', '商城特惠3', '商城特惠4'],
        pic: ['img/tab/2-1.png', 'img/tab/2-2.png', 'img/tab/2-3.png', 'img/tab/2-4.png']
    },
    {
        title: '名品推荐',
        subtitle: ['名品推荐1', '名品推荐2', '名品推荐3', '名品推荐4', '名品推荐5'],
        pic: ['img/tab/3-1.png', 'img/tab/3-2.png', 'img/tab/3-3.png', 'img/tab/3-4.png', 'img/tab/3-5.png']
    },
    {
        title: '缤纷活动',
        subtitle: ['缤纷活动1', '缤纷活动2', '缤纷活动3', '缤纷活动4'],
        pic: ['img/tab/4-1.png', 'img/tab/4-2.png', 'img/tab/4-3.png', 'img/tab/4-3.png', ]
    }];

    // 初始化
    for (var i = 0; i < arr.length; i++) {
        // 大选项卡的标题
        var oLi = document.createElement('li');
        oLi.innerHTML = arr[i].title;
        oLi.index = i;
        oLi.onclick = function() {
            highlight(this);
            ul.innerHTML = '';
            changeTab(this.index);
        }
        title.appendChild(oLi);
    }
    changeTab(0);
    function changeTab(num) {
        // 大选项卡的内容
        img.src = arr[num].pic[1];
        // 小标题
        for (var j = 0; j < arr[num].subtitle.length; j++) {
            oLi = document.createElement('li');
            oLi.innerHTML = arr[num].subtitle[j];
            oLi.style.width = Math.floor(parseInt(800 / arr[num].subtitle.length) - 2) + 'px';
            oLi.index = j;
            oLi.onclick = function() {
                highlight(this);
                this.parentNode.previousElementSibling.src = arr[num].pic[this.index];
            }
            ul.appendChild(oLi);
        }
    }
    function highlight(ele) {
        var aLi = ele.parentNode.children;
        for (var i = 0; i < aLi.length; i++) {
            aLi[i].className = '';
        }
        ele.className = 'active';
    }
}
京东轮播:选项卡里套选项卡

作者:网络 来源:光明大神棍的博客