原生tab

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');
            }
        });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 180,358评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 16,697评论 4 61
  • 吃货地图产品需求文档 V1.0-2015/03/30 1概述 1.1产品概述及目标 概述:“吃货地图”是一款基于i...
    michaelshan阅读 6,015评论 1 46
  • 上次有同学问我你写的九型人格怎么才四个,还用到了算法,可见好奇心爆棚,那么我继续上次的话题说下去,九型其实是九种人...
    超人在进化阅读 876评论 0 1
  • 中午老板娘发飙,起因由于老板娘外出结果中午我们没有轮班吃饭,导致有一个客户的单子没处理,一个快递没接,一个技术问题...
    一桑阅读 486评论 0 1

友情链接更多精彩内容