Fe-12 dom&轮播图

2018-01-07  本文已影响20人  吕阳

Fe-12-1

Fe-12-2 toggle

Toggle

<!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>toggle</title>
  <style>
      .gua-hide {
          display: none;
      }
      .gua-menu-content {
          height: 100px;
          border: 1px solid red;
      }
      .gua-item {
          /*display: inline-block;*/
          /*opacity: 0.6;*/
          filter: grayscale(100%)
      }
      .gua-item.gua-highlight {
          filter: initial;
          /*opacity: 1;*/
      }


  </style>
</head>
<body>
  <!-- 1, 点击显示的菜单 -->
  <div class="gua-1">
      <div class="gua-menu">
          <button class="gua-menu-toggle">点击展开详情</button>
          <div class="gua-menu-content">
              详情
          </div>
      </div>
      <div class="gua-menu">
          <button class="gua-menu-toggle">点击展开详情</button>
          <div class="gua-menu-content">
              详情
          </div>
      </div>
  </div>
  <script src=toggle.js></script>
</body>
</html>


var log = function() {
    console.log.apply(console, arguments)
}


var e = function(selector) {
    return document.querySelector(selector)
}


var bindEvent = function(element, eventName, callback) {
    element.addEventListener(eventName, callback)
}


var toggleClass = function(element, className) {
    if (element.classList.contains(className)) {
        element.classList.remove(className)
    } else {
        element.classList.add(className)
    }
}


var bindAll = function(selector, eventName, callback) {
    var elements = document.querySelectorAll(selector)
    for(var i = 0; i < elements.length; i++) {
        var e = elements[i]
        bindEvent(e, eventName, callback)
    }
}


// find 函数可以查找 element 的所有子元素
var find = function(element, selector) {
    return element.querySelector(selector)
}


var bindEventToggles = function() {
    var selector = '.gua-menu-toggle'
    bindAll(selector, 'click', function(event){
        log('click button')
        // 找到自己这个菜单的 gua-menu-content
        var p = event.target.parentElement
        var c = find(p, '.gua-menu-content')
        // 开关 gua-hide class
        toggleClass(c, 'gua-hide')
    })
}


bindEventToggles()


上一篇 下一篇

猜你喜欢

热点阅读