zoukankan      html  css  js  c++  java
  • JavaScript实现多个菜单的显示隐藏(JavaScript实现二级/三级菜单的显示隐藏)

    (如有错敬请指点,以下是我工作中遇到并且解决的问题)

    效果图 :

    可以通过 https://littlehiuman.github.io/07-menus/ 查看效果。

    https://github.com/littleHiuman/littleHiuman.github.io 求点star~~~

    点击各个菜单(自助餐、中山二三路、智能排序)会显示/隐藏二级菜单(二级菜单可能既有左侧内容也有右侧内容(三级菜单),也可能只有左侧内容)

    CSS:

    * {
      margin: 0;
      padding: 0;
    }
    li {
      list-style: none;
    }
    
    .menu {
      width: 100%;
      height: 20px;
      background: #fff;
      border-bottom: 1px solid #ddd;
      overflow: hidden;
      padding: 8px 0;
    }
    .menu_con {
      text-align: center;
      border-right: 1px solid #ddd;
      width: 33%;
      height: 20px;
      line-height: 20px;
      float: left;
      cursor: pointer;
    }
    .menu_fon:last-child {
      border-right: none;
    }
    .menu_title {
      font-size: 14px;
    }
    
    .pop_wrap {
      display: none;
      position: fixed;
      top: 37px;
      left: 0;
      background: rgba(0, 0, 0, 0.5);
      width: 100%;
      height: 100%;
      clear: both;
    }
    
    .pop_left,
    .pop_right {
      position: absolute;
      height: 150px;
      overflow: auto;
    }
    .pop_left {
      background: #fff;
      left: 0;
      width: 40%;
    }
    .pop_right {
      background: #ddd;
      left: 40%;
      width: 60%;
    }
    .pop_wrap li {
      line-height: 30px;
      padding-left: 20px;
      border-bottom: 1px solid #eee;
    }
    .pop_wrap li.active {
      color: #6ab494;
    }
    .pop_right li.active {
      background: #eee;
    }

    HTML:

    <div class="menu">
      <div class="menu_con menu_type" onclick="showhide(0)">
        <span class="menu_title">自助餐</span>
      </div>
      <div class="menu_con menu_position" onclick="showhide(1)">
        <span class="menu_title">中山二三路</span>
      </div>
      <div class="menu_con menu_sort" onclick="showhide(2)">
        <span class="menu_title">智能排序</span>
      </div>
    </div>
    <div class="pop_wrap">
      <ul class="pop_left"></ul>
      <ul class="pop_right"></ul>
    </div>

    JAVASCRIPT:

    var current = ''
    function showhide(sth) {
      var data = [
        {
          left: ['全部分类', '美食', '电影', '美食', '摄影', '摄影'],
          right: ['全部', '经济型', '经济型', '经济型', '经济型', '经济型']
        },
        {
          left: ['全部分类', '番禺区', '电影', '美食', '摄影', '摄影'],
          right: ['全部', '经济型', '经济型', '经济型', '经济型', '经济型']
        },
        {
          left: ['智能排序', '好评优先', '离我最近', '人均最低', '摄影', '摄影'],
          right: []
        }
      ]
      var popWrap = document.querySelector('.pop_wrap')
      var popLeft = document.querySelector('.pop_left')
      var popRight = document.querySelector('.pop_right')
      popLeft.innerHTML = setData(data[sth].left)
      popRight.innerHTML = setData(data[sth].right)
      if (popWrap.style.display == 'block') {
        if (sth == current) {
          popWrap.style.display = 'none'
        } else {
          current = sth
        }
      } else {
        current = sth
        popWrap.style.display = 'block'
      }
    }
    function setData(data) {
      var str = ''
      if (data.length) {
        for (var i = 0; i < data.length; i++) {
          str += `<li ${i == 0 ? "class='active'" : ''}>${data[i]}</li>`
        }
      }
      return str
    }
     
  • 相关阅读:
    html中label及加上属性for之后的用法
    Django中利用filter与simple_tag为前端自定义函数的实现方法
    关于自动编译iOS工程,生成app及ipa文件的方法-备
    ios打包ipa的四种实用方法(.app转.ipa)-备
    为GCD队列绑定NSObject类型上下文数据-利用__bridge_retained(transfer)转移内存管理权-备
    GCD使用经验与技巧浅谈--备
    h5与iOS的wkwebview不兼容问题
    cocoaPods 安装和应用
    .a静态库的注意事项
    UIApplication详解再解-备
  • 原文地址:https://www.cnblogs.com/hiuman/p/7347402.html
Copyright © 2011-2022 走看看