前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原生JS实现Tab切换效果和模态框效果

原生JS实现Tab切换效果和模态框效果

作者头像
小胖
发布2018-06-27 16:07:45
9.6K0
发布2018-06-27 16:07:45
举报

原生JS实现Tab切换效果

效果展示

代码语言:javascript
复制
<!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实现模态框效果

效果展示

代码语言:javascript
复制
<!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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.09.28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 原生JS实现Tab切换效果
  • 原生JS实现模态框效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档