原生JS实现Tab切换效果
效果展示
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Modal</title>
<style>
.clearfix:after{
content:'';
display: block;
clear: both;
}
li{
list-style: none;
}
li,ul{
margin:0;
padding:0;
}
.tab{
width:600px;
border:1px solid red;
margin:20px auto;
border:1px solid #ccc;
padding:20px 10px;
border-radius: 4px;
}
.tab-header{
border-bottom:1px solid #ccc;
}
.tab-header .active{
border: 1px solid #ccc;
border-radius: 4px 4px 0 0;
color: #333;
border-bottom-color: #fff;
margin-bottom: -1px;
}
.tab-header>li{
float:left;
color: lightblue;
cursor: pointer;
padding: 10px;
border-top:1px solid #fff;
border-left:1px solid #fff;
border-right:1px solid #fff;
}
.tab-container{
padding: 20px 10px;
}
.tab-container>li{
display: none;
}
.tab-container .active{
display: block;
}
</style>
</head>
<body>
<div class="tab">
<ul class="tab-header clearfix">
<li class="active">选项1</li>
<li>选项2</li>
<li>选项3</li>
</ul>
<ul class="tab-container">
<li class="active">内容1</li>
<li>内容2</li>
<li>内容3</li>
</ul>
</div>
<script>
var tabHeader = document.querySelector('.tab-header');
var tabLis = document.querySelectorAll('.tab-header>li');
var tabPanels = document.querySelectorAll('.tab-container>li');
tabHeader.addEventListener('click',function(e){
var clickNode = e.target;
if(clickNode.tagName.toLowerCase() === 'li'){
for(var i=0; i<tabHeader.children.length;i++){
tabLis[i].classList.remove('active');
}
clickNode.classList.add('active');
var index = [].indexOf.call(tabLis,clickNode);
//tabLis返回的是类数组对象,没有indexOf方法,所以要用到call
for(var i=0; i<tabPanels.length;i++){
tabPanels[i].classList.remove('active');
}
tabPanels[index].classList.add('active');
}
})
</script>
</body>
</html>
原生JS实现模态框效果
效果展示
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Modal</title>
<style>
#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="点我">
<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>
<script>
var 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.body.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');
}
});
</script>
</body>
</html>