「点击其他地方关闭浮层」

布局

HTML
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div id="wrapper" class="wrapper">
    <button id="clickMe">点我</button>
    <div id="popover" class="popover">
      <input type="checkbox">浮层
    </div>
  </div>
</body>
</html>
CSS
body{
  border: 1px solid red;
}
.wrapper{
  position: relative;
  display: inline-block;
}
.popover{
  border: 1px solid red;
  position: absolute;
  left: 100%;
  top: 0;
  white-space: nowrap;
  padding: 10px;
  margin-left: 10px;
  background: white;
  display: none;
}
.popover::before{
  position: absolute;
  right: 100%;
  top: 5px;
  border: 10px solid transparent;
  border-right-color: red;
  content: '';
}
.popover::after{
  position: absolute;
  right: 100%;
  top: 5px;
  border: 10px solid transparent;
  border-right-color: white;
  content: '';
  margin-right: -1px;
}

方案一

JS
clickMe.addEventListener('click', function(e){
  popover.style.display = 'block'
})
wrapper.addEventListener('click', function(e){
  e.stopPropagation()
})

document.addEventListener('click', function(){
  popover.style.display = 'none'
})

方案二

JS
$(clickMe).on('click', function() {
  $(popover).show()
  console.log('show')
  setTimeout(function() {
    console.log('添加 one click')
    $(document).one('click', function() {
      console.log('我觉得这里不会执行')
      console.log('hide')
      $(popover).hide()
    })
  }, 0)

})

冒泡动画

HTML
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<div class="red">
  <div class="blue">
    <div class="green">
      <div class="yellow">
        <div class="orange">
          <div class="purple">
            
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>
CSS
*{margin:0;padding:0;box-sizing:border-box;}
.red.active {
  background: red;
}
.blue.active {
  background: blue;
}
.green.active {
  background: green;
}
.yellow.active {
  background: yellow;
}
.orange.active {
  background: orange;
}
.purple.active {
  background: purple;
}
div {
  border: 1px solid black;
  padding: 10px;
  transition: all 0.5s;
  display: flex;
  flex:1;
  border-radius: 50%;
  background: white;
}

.red{
  width: 100vw;
  height: 100vw;
}
JS
let divs = $('div').get()
let n = 0
for (let i = 0; i < divs.length; i++) {
  divs[i].addEventListener('click', () => {
    setTimeout(() => {
      divs[i].classList.add('active')
    }, n * 500)
    n += 1
  }, true)
}
for (let i = 0; i < divs.length; i++) {
  divs[i].addEventListener('click', () => {
    setTimeout(() => {
      divs[i].classList.remove('active')
    }, n * 500)
    n += 1
  })
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 需求:点击button出现浮层,点击别处关闭浮层。我们有一个按钮和一个浮层,浮层内为一个checkbox,浮层默认...
    是刘快啊阅读 1,215评论 0 1
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,749评论 1 45
  • 《初中多文本读写结合策略》 主讲:晁忠强 主持:李利敏 策划:贾红亚 二零一七年四月六日 晁忠强老师简介 晁忠强,...
    冉冉语文阅读 1,950评论 0 7
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,814评论 1 92
  • 打卡2019年5月7日 姓名:万杨 公司:北京海特科技有限公司 【日精进打卡第43天】 【知~学习】 《六项精进》...
    万杨_827b阅读 85评论 0 0