需求

需要做一个顶部的水平导航条,有三级,展开的时候二级和三级一起展开,结果如图:

效果

代码

下面贴上代码:

HTML代码

<div class="layui-header">
  <ul id="moudleMenu" class="layui-nav layui-layout-left kit-nav">
    <li class="layui-nav-item nav-custom"><a href="javascript:;">一级菜单</a>
      <div class="layui-nav-child layui-anim layui-anim-upbit">
        <ul style="float: left; text-align: center; color: black; margin: 0;">
          <li>
            <span>二级标题</span>
            <!-- 标题下面的线 -->
            <div style="height: 1px; background-color: #1e9fff; width: 90%; margin: 0 auto;">&nbsp;</div>
          </li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>三级菜单</span></a></li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>三级菜单</span></a></li>
        </ul>
        <ul style="float: left; text-align: center; color: black; margin: 0;">
          <li>
            <span>二级标题</span>
            <div style="height: 1px; background-color: #1e9fff; width: 90%; margin: 0 auto;">&nbsp;</div>
          </li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>三级菜单</span></a></li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>三级菜单</span></a></li>
        </ul>
      </div>
    </li>
    <li class="layui-nav-item nav-custom"><a href="javascript:;">一级菜单</a>
      <div class="layui-nav-child layui-anim layui-anim-upbit">
        <ul style="float: left; text-align: center; color: black; margin: 0;">
          <li>
            <span>二级标题</span>
            <div style="height: 1px; background-color: #1e9fff; width: 90%; margin: 0 auto;">&nbsp;</div>
          </li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>三级菜单</span></a></li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>三级菜单</span></a></li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>三级菜单</span></a></li>
        </ul>
        <ul style="float: left; text-align: center; color: black; margin: 0;">
          <li>
            <span>二级标题</span>
            <div style="height: 1px; background-color: #1e9fff; width: 90%; margin: 0 auto;">&nbsp;</div>
          </li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>三级菜单</span></a></li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>我是独特的三级菜单</span></a></li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>三级菜单</span></a></li>
        </ul>
        <ul style="float: left; text-align: center; color: black; margin: 0;">
          <li>
            <span>二级标题</span>
            <div style="height: 1px; background-color: #1e9fff; width: 90%; margin: 0 auto;">&nbsp;</div>
          </li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>三级菜单</span></a></li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>三级菜单</span></a></li>
        </ul>
      </div>
    </li>
    <li class="layui-nav-item nav-custom"><a href="javascript:;">一级菜单</a>
      <div class="layui-nav-child layui-anim layui-anim-upbit">
        <ul style="float: left; text-align: center; color: black; margin: 0;">
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>我上面的二级标题呢</span></a></li>
          <li><a style="padding: 0;" href="'javascript:void(0);"><span>我是三级</span></a></li>
        </ul>
      </div>
    </li>
  </ul>
</div>

需要加上一个css  .layui-nav-child{top:60px !important;}

JS代码:

引用jQuery、layui.js、layui.css和element.js文件

layui.use('element', function(){
var element = layui.element;
element.init();
//如果不把layui自带的滑动线移除,会导致子菜单隐藏
  $(".layui-nav-bar").css("display","none");
  
//鼠标悬浮
navMouseOver();
//鼠标移出
navMouseOut();
});
//当鼠标经过时 设置展开菜单的宽度及位置的CSS样式
function navMouseOver() {
$(".nav-custom").each(function() {
$(this).mouseover(function () {
//当鼠标放上时显示子菜单
$(this).children("div").css("visibility", "visible");
$(this).children("div").children("ul").children("li").children("a").children("span").css("visibility", "visible");
//ul的个数
var ulCount = $(this).children("div").children("ul").length;
//li的个数
var liCount = $(this).children("div").children("ul").children("li").length;
//定义字符最大长度
var maxLength = 0;
//遍历当前展开菜单的li
$(this).children("div").children("ul").children("li").each(function() {
//获取展开菜单的标题字符数
if ($(this).children("span").text().length > maxLength) {
maxLength = $(this).children("span").text().length; }
//获取展开菜单的选择项的字符数
if ($(this).children("span").text() == "") {
if ($(this).children("a").children("span").text().length > maxLength) {
maxLength = $(this).children("a").children("span").text().length;
}
}
});
//每个子菜单的ul长度 为最长字符数*字符像素px+左右空白间隔
var width = maxLength * 13 + 40;
//设置展开菜单的每个子菜单的ul长度 为最长字符数*字符像素px
$(".nav-custom").children("div").children("ul").css("width", width + "px");
//设置展开菜单的总div宽度 为子菜单的ul长度*子菜单ul个数
$(".nav-custom").children("div").css("width", width * ulCount + "px");
//设置展开菜单的总div偏移居中
$(".nav-custom").children("div").css("left", (-1 * width * ulCount) / 2 + ($(".nav-custom").width()) / 2 + "px");
});
});
}
//当鼠标移出时,立即隐藏子菜单(因为不设置隐藏的话,来回切会有延迟导致未隐藏的子菜单变形)
function navMouseOut() {
$(".nav-custom").each(function () {
$(this).mouseout(function () {
//设置div不可见
$(this).children("div").css("visibility", "hidden");
//设置div中的字不可见
$(this).children("div").children("ul").children("li").children("a").children("span").css("visibility", "hidden");
});
});
}

样式的话,按照自己喜欢的调吧(`・ω・´)

(function navMouseOver() {$(".layui-nav-bar").css("display","none"); $(".nav-custom").each(function() { $(this).mouseover(function() { $(this).children("div").css("visibility", "visible"); $(this).children("div").children("ul").children("li").children("a").children("span").css("visibility", "visible"); var ulCount = $(this).children("div").children("ul").length; var liCount = $(this).children("div").children("ul").children("li").length; var maxLength = 0; $(this).children("div").children("ul").children("li").each(function() { if ($(this).children("span").text().length > maxLength) { maxLength = $(this).children("span").text().length; } if ($(this).children("span").text() == "") { if ($(this).children("a").children("span").text().length > maxLength) { maxLength = $(this).children("a").children("span").text().length; } } }); var width = maxLength * 13 + 40; $(".nav-custom").children("div").children("ul").css("width", width + "px"); $(".nav-custom").children("div").css("width", width * ulCount + 10 +"px"); $(".nav-custom").children("div").css("left", (-1 * width * ulCount) / 2 + ($(".nav-custom").width()) / 2 + "px"); }); }); })(); (function navMouseOut() { $(".nav-custom").each(function() { $(this).mouseout(function() { $(this).children("div").css("visibility", "hidden"); $(this).children("div").children("ul").children("li").children("a").children("span").css("visibility", "hidden"); }); }); })()

layui水平导航条三级的更多相关文章

  1. CSS水平导航条和纵向导航条

    问题描述:         使用CSS制作水平导航条和纵向导航条   问题解决:        (1)水平导航条            1.1 效果预览:                   1.2 ...

  2. HTML布局水平导航条2制作

    前两个博文导航条都不是铺满水平的浏览器的,很多导航条样式都是随着浏览器的移动,是100%.此外前两个博文导航条都是块状点击的,也就是给a标签加宽高,设置成块状显示,点击的时候不一定要点文字,只要点击该 ...

  3. HTML布局水平导航条1制作

    该文是用css制作个导航条,用竖线分隔,导航条是点击的多个区块.步骤:ul里设置需要数量的li,li中加上a链接给ul加样式,去掉默认的前面的点给li设置左浮动,让ul里的li横向排列a链接设置成块状 ...

  4. div自定义的滚动条 (水平导航条)

    <!DOCTYPE html> <html> <head> <title></title> <style> div{ /* wi ...

  5. simple水平导航条

    话不多说,看代码: html部分 <body> <ul> <li><a href="#">Home</a></li ...

  6. css -- 导航条

    1.垂直导航条 HTML: <ul class="nav"> <li><a href="">Home</a>&l ...

  7. CSS列表及导航条

    大多数网页中都包含某种形式的列表,今天我们就来联系几个基本的导航条.   垂直导航条 注意要点: 去掉默认的项目符号(list-style-type:none),将外边距和内边距都设为0. 以em设置 ...

  8. [前端]如何写一个水平导航栏?(浮动、inline-block+消除间距)

    在看W3school时,看到一个很好的例子,如何制作一个水平的导航栏?没有任何要求,只需要达到下面的效果: 我认为这个例子包含了很多css布局需要了解的知识,因此单独写一下. W3school上面的方 ...

  9. ch6 列表和导航条

    为列表添加定制的项目符号 可使用list-style-image属性:缺点是对项目符号图像的位置的控制能力不强. 常用的方法:使用list-style-type来关闭项目符号,将定制的项目符号作为背景 ...

随机推荐

  1. 【Android】RxJava的使用(二)Action

    回顾 在上一节Android RxJava的使用(一)基本用法中,介绍了RxJava的基本用法.下面来回顾下实现一次RxJava的基本使用.例:分别打印"Hellow"." ...

  2. Spring 集成 Ehcache 开启缓存

    1.jar包 1.1.slf4j-api-1.6.1.jar 1.2.ehcache-2.7.0.jar 1.3.spring-core-3.2.0.RELEASE.jar 1.4.spring-co ...

  3. [翻译] AnchoredFloatView

    AnchoredFloatView Float View over TableView to indicate selected row direction Float View 可以用来指示 Tab ...

  4. 使用NSClassFromString

    使用NSClassFromString 使用NSClassFromString可以直接从字符串初始化出对象出来,即使不引用头文件也没关系,以下是源码: AppDelegate.m // // AppD ...

  5. 《C++ Primer Plus》读书笔记之七—内存模型和名称空间

    第九章 内存模型和名称空间 1.不要将函数定义或者变量声明放到头文件中. 2.头文件常包含的内容:函数原型.使用#define或者const定义的常量.结构声明.类声明.模板声明.内联函数. 3.避免 ...

  6. ASP.NET在IIS的启动优化设置

    ASP.NET在IIS的启动优化设置 约定: IIS=Internet Information Services Manager 概要 因为ASP.NET 程序在第一次启动的时候需要等待太长时间,至少 ...

  7. 寒假短期学习计划 - C++

    寒假短期学习计划 - C++ 一.所选课程 && 相关 0.选以下课的理由: 选课理由0: 只是短期的计划,先选些短视频感受:之后再视情况选其他课: 选课理由1: 难度低,以前自学过一 ...

  8. Fix for: Permission denied to access property 'toString'

    Originally posted by rwolffgang here. Hi guys,when developing a game that runs in an iframe (Faceboo ...

  9. php操作redis的两个个小脚本

    redis这东西,查询起来没有mysql那么方便,只能自己写脚本了.下面是工作中写的两个小脚本 第一个脚本,查找有lottery|的键,将他们全部删除|打印出来 <?php $redis = n ...

  10. rabbitmq的万能安装和外网访问(NC版)

    先去这个http://www.rabbitmq.com/releases/下载erlang环境和rpm(erlang的尽量高点,rabbitmq版本差不多就可以了,) erlang-19.0.4-1. ...