事件的应用

1. 实现如下图Tab切换的功能

image.png
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
*{
  margin: 0;
  padding: 0;
}
.tab-ct{
  width: 304px;
  margin: auto;
}
.tab-ct li{
  border: 1px solid;
}
.header > li{
  list-style: none;
  border: 1px solid;
  border-left: none;
  border-bottom: none;
}
.header > li:hover{
 cursor: pointer;
}
.header > li:first-child{
  border-left: 1px solid;
}
.clear:after{
  content: '';
  display: block;
  clear: both;
}
.header>li{
  float: left;
  width: 100px;
}
.content>li{
  float: left;
  width: 302px;
  height: 200px;
  display: none;  
}
.header .active {
  background-color: rgba(128, 128, 128, 0.2);
}
.content .active{
  display: block;
}
        
    </style>
</head>
<body>
    <div class="tab-ct">
    <ul class="header clear">
        <li class="active">tab1</li>
        <li>tab2</li>
        <li>tab3</li>
    </ul>
    <ul class="content clear">
        <li class="active">ct1</li>
        <li>ct2</li>
        <li>ct3</li>
    </ul>
    </div>
    <script>
        var tabs = document.querySelectorAll('.tab-ct .header>li')  // 声明
        var panels = document.querySelectorAll('.tab-ct .content>li')   // 声明 
        tabs.forEach(function(tab){ // 遍历
            tab.addEventListener('click',function(){    // 触发事件
                tabs.forEach(function(node){    // 遍历
                    node.classList.remove('active') // 去除所有class='active'
                })
                this.classList.add('active')    // 鼠标点击的加class='active'
                var index = [].indexOf.call(tabs,this)  //i
                panels.forEach(function(node){
                    node.classList.remove('active')
                })
                panels[index].classList.add('active')
            })
        })
    </script>
</body>
</html>

预览- http://js.jirengu.com/taboh/3/edit?output

2.实现下图的模态框功能,点击模态框不隐藏,点击关闭以及模态框以外的区域模态框隐藏

image.png
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        *{
  margin: 0;
  padding: 0;
}
.modal{
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: gray;
}
.active{
  display: block;
}
.modal-ct {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  width: 400px;
  background-color:white;
  border-radius: 3px;
}
.modal-ct .content{
  border-top: 1px solid;
  border-bottom: 1px solid;
  padding: 20px 10px;
}
.modal-ct header{
  margin-top: 10px;
  margin-bottom: 10px;
  padding-left: 10px;
}
.modal-ct header span{
  margin-left: 290px;
}
.modal-ct footer {
  margin: 10px;
  margin-left: 300px; 
}
.modal-ct footer span{
  padding-left: 10px;  
}
.modal-ct span {
 cursor: pointer;
}
    </style>
</head>
<body>
<div class="btn-group">
  <button id="btn">点我</button>
</div>
<div class="modal">
  <div class="modal-ct">
    <header>
      <h3>这是标题<span class="close">X</span></h3>
    </header>
    <div class="content">
      <p>这是内容</p>
      <p>这是内容</p>
    </div>
    <footer>
      <span class="cancel">取消</span><span>确定</span>
    </footer>
  </div>
</div>
<script>
    var btn = document.querySelector('#btn')
    madal = document.querySelector('.modal')
    ct = document.querySelector('.modal-ct')
    close = document.querySelector('.close')
    cancel = document.querySelector('.cancel')

    btn.addEventListener('click',function(){
        madal.classList.add('active')
    })
    close.addEventListener('click',function(){
        madal.classList.remove('active')
    })
    cancel.addEventListener('click',function(){
        madal.classList.remove('active')
    })
    madal.addEventListener('click',function(){
        madal.classList.remove('active')
    })
    ct.addEventListener('click',function(e){
        e.stopPropagation()
    })
</script>
</body>
</html>

预览- http://js.jirengu.com/mexoj/2/edit?html,css,output

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 题目1:实现tabhttp://js.jirengu.com/sinawaxule/1/edit 题目2:实现模态...
    好好顽阅读 153评论 0 0
  • 1、DOM0 事件和DOM2级在事件监听使用方式上有什么区别? Dom0级事件处理程序是将一个函数赋值给一个事件处...
    不是我的简书阅读 395评论 0 0
  • 1. 实现如下图Tab切换的功能 2. 实现下图的模态框功能,点击模态框不隐藏,点击关闭以及模态框以外的区域模态框隐藏
    _李祺阅读 369评论 0 0
  • 爸妈经常来学校看我 次数很频繁 不是因为我恋家了 也不是爸妈粘我了 爸妈陪你度过了咿呀学语 蹒跚学步 后来 老师同...
    没有诗欲的诗人阅读 180评论 0 3
  • (诗文原创:羽裳) 曾渴望 一份纯粹的拥有 却害怕 灼伤你 痛彻我 于是 默默注视 悄悄远走 不让你看见 早已含泪...
    羽裳_阅读 263评论 0 7