事件的应用

2017-07-05  本文已影响10人  好好顽

题目1:实现tabhttp://js.jirengu.com/sinawaxule/1/edit

<!doctype html>
<html>

<head>
  <style>
    ul,
    li {
      margin: 0;
      padding: 0;
    }

    li {
      list-style: none;
    }

    .clearfix:after {
      content: '';
      display: block;
      clear: both;
    }

    .tab {
      width: 600px;
      margin: 20px auto;
      border: 1px solid #ccc;
      padding: 20px 10px;
      border-radius: 4px;
    }

    .tab-header {
      border-bottom: 1px solid #ccc;
    }

    .tab-header>li {
      float: left;
      color: brown;
      border-top: 1px solid #fff;
      border-left: 1px solid #fff;
      border-right: 1px solid #fff;
      padding: 10px 20px;
      cursor: pointer;
    }

    .tab-header .active {
      border: 1px solid #ccc;
      border-bottom-color: #fff;
      border-radius: 4px 4px 0 0;
      color: #333;
      margin-bottom: -1px;
    }

    .tab-container {
      padding: 20px 10px;
    }

    .tab-container>li {
      display: none;
    }

    .tab-container>.active {
      display: block;
    }



    .box {
      height: 1000px;
    }
  </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
        <ul>
          <li></li>
        </ul>
      </li>
      <li>内容2</li>
      <li>内容3</li>
    </ul>
  </div>
  <div class="box"></div>
  <script>
    var tabs = document.querySelectorAll('.tab .tab-header>li')
    var panels = document.querySelectorAll('.tab .tab-container>li')

    tabs.forEach(function (tab) {
      tab.addEventListener('click', function () {

        tabs.forEach(function (node) {
          node.classList.remove('active')
        })

        this.classList.add('active')
        var index = [].indexOf.call(tabs, this)

        panels.forEach(function (node) {
          node.classList.remove('active')
        })
        panels[index].classList.add('active')
      })
    })
  </script>
</body>

</html>

题目2:实现模态框 http://js.jirengu.com/mayujilipa/1/edit

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>modal</title>
  <style>
    .modal-dialog {
      display: none;
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: raba(0, 0, 0, 0.8);
    }

    .modal-dialog .bt {
      display: inline-block;
      padding: 3px 6px;
      border: 1px solid #ccc;
      border-radius: 3px;
      font-size: 14px;
    }

    .modal-dialog a {
      text-decoration: none;
      color: #333;
    }

     .cover {
      /*display: none;*/
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #000;
      opacity: 0.4;
      z-index: 99;
    }

    .modal-dialog .modal-ct {
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      -webkit-transform: translate(-50%, -50%);
      width: 400px;
      border-radius: 3px;
      border: 1px solid #ccc;
      background: #fff;
      z-index: 100;
    }

    .modal-dialog .modal-ct .header {
      position: relative;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
    }

    .modal-dialog .modal-ct .header h3 {
      margin: 0;
      padding-left: 10px;
      font-size: 16px;
    }

    .modal-dialog .modal-ct .header .close {
      position: absolute;
      right: 10px;
      top: 10px;
      line-height: 1;
    }

    .modal-dialog .modal-ct .content {
      padding: 10px;
    }

    .open {
      display: block;
    }

    
  </style>
</head>

<body>
  <div class="btn-group">
    <button id="btn-modal">点我1</button>
  </div>

  <div id="modal-1" class="modal-dialog">
    <div class="cover"></div>
    <div class="modal-ct">
      <div class="header">
        <h3>标题</h3>
        <a href="#" class="close">x</a>
      </div>
      <div class="content">
        <p>内容</p>
        <p>内容</p>
      </div>
    </div>

  </div>
  <script>
    var btn = document.querySelector('#btn-modal')
    modal = document.querySelector('#modal-1')
    modalCt = document.querySelector('#modal-1 .modal-ct')
    close = document.querySelector('#modal-1 .close')

    btn.addEventListener('click', function () {
      modal.classList.add('open')
    })

    close.addEventListener('click', function () {
      modal.classList.remove('open')
    })

    modal.addEventListener('click', function () {
      modal.classList.remove('open')
    })

    modalCt.addEventListener('click', function () {
      e.stopPropagation()
    })
  </script>
</body>

</html>
上一篇下一篇

猜你喜欢

热点阅读