1.原生切换
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tab</title>
<style>
.clearfix:after{
content: '';
display: block;
height: 0;
width: 0;
visibility: hidden;
clear: both;
}
.layout {
width: 910px;
margin: 0 auto;
}
.tab-wrapper {
border: 1px solid #607d8b;
padding: 20px;
border-radius: 4px;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
.tab-nav {
border-bottom: 1px solid #ccc;
}
.tab-nav>li {
cursor: pointer;
color: blue;
float: left;
width: 100px;
border-top: 1px solid #fff;
border-left: 1px solid #fff;
border-right: 1px solid #fff;
text-align: center;
line-height: 2;
border-radius: 2px;
}
.tab-nav .active {
border: 1px solid #ccc;
border-bottom-color: #fff;
border-radius: 4px 4px 0 0;
margin-bottom: -1px;
color: red;
}
.tab-content>li {
height: 200px;
padding: 50px;
margin-top: 1px;
display: none;
}
.tab-content .active {
display: block;
}
</style>
</head>
<body>
<div class="layout">
<div class="tab-wrapper">
<ul class="tab-nav clearfix">
<li class="active">tab1</li>
<li>tab2</li>
<li>tab3</li>
</ul>
<ul class="tab-content">
<li class="active">内容1</li>
<li>内容2</li>
<li>内容3</li>
</ul>
</div>
</div>
<script>
let oTabNav = document.getElementsByClassName('tab-nav')[0],
oTabBtn = document.querySelectorAll('.tab-nav>li');
oTabCont = document.querySelectorAll('.tab-content>li');
oTabNav.addEventListener('click',function(e){
if(e.target.tagName.toLowerCase() === 'li'){
for(let i=0; i<oTabBtn.length; i++){
oTabBtn[i].classList.remove("active");
}
e.target.classList.add("active");
}
let index = Array.prototype.indexOf.call(oTabBtn, e.target);
for(let i=0; i<oTabCont.length; i++){
oTabCont[i].classList.remove("active");
}
oTabCont[index].classList.add("active");
});
</script>
</body>
</html>
2.点击浮层
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Modal</title>
<style>
html,body {
margin: 0;
padding: 0;
height: 100%;
}
#btn {
margin: 10px;
}
.overlay {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
height: 100%;
width: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 99;
}
.modal {
width: 500px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 1px solid #ccc;
border-radius: 6px;
background-color: #fff;
}
.modal>h2 {
border-bottom: 1px solid #ccc;
margin: 0;
line-height: 2;
padding-left: 20px;
}
.modal>.detail {
padding: 0 20px;
height: 100px;
border-bottom: 1px solid #ccc;
}
.modal>.btn {
line-height: 2.2;
cursor: pointer;
float: right;
margin: 0 10px;
}
</style>
</head>
<body>
<input id="btn" type="button" value="Modal">
<div class="overlay">
<div class="modal">
<h2>我是标题</h2>
<div class="detail">
<p>我是内容</p>
<p>我是内容</p>
</div>
<span class="btn close">确定</span>
<span class="btn cancel">取消</span>
</div>
</div>
</body>
</html>
let btn = document.getElementById('btn'),
overlay = document.getElementsByClassName('overlay')[0],
modal = document.getElementsByClassName('modal')[0];
btn.addEventListener('click',function(e){
e.stopPropagation();
overlay.setAttribute('style','display:block');
});
document.addEventListener('click',function(e){
overlay.setAttribute('style','display:none');
});
modal.addEventListener('click',function(e){
e.stopPropagation();
if(e.target.classList.contains('close') || e.target.classList.contains('cancel')){
overlay.setAttribute('style','display:none');
}
});