需求

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

效果

代码

下面贴上代码:

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 studio使用openssl

    前言 逆向的基础是开发, 逆向分析时很多时候会使用一些公开的加密函数来对数据进行加密,通过使用 openssl 熟悉下. 正文 首先得先编译出来 openssl,然后把它们复制到你的工程目录下. in ...

  2. dev gridview指定单元格cell获取坐标

    DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo Info2 = gvQueryResult.GetViewInfo() as DevExpre ...

  3. maven 远程仓库、私服及镜像配置

    maven仓库分类 本地仓库.远程仓库.远程仓库又有私服.中央仓库.其它公共库.中央仓库是maven自带的核心仓库. 仓库配置远程仓库可以配置多个,超级pom中定义的中央仓库 <reposito ...

  4. 带你从零学ReactNative开发跨平台App开发[expo 打包发布](八)

    ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...

  5. mac os maverick下安装nginx+php-fpm via homebrew

    自己虽然平时爱折腾,却很少有记下来的习惯,除非碰到特别多问题的部署才会有冲动.今天看同事折腾git,在旁边看着他mac上的evernote满满的记了好几篇,全是技术相关的记录,忽然很感慨.过去解决了很 ...

  6. 山寨今日头条的标题title效果

    山寨今日头条的标题title效果 效果: 源码: // // ViewController.m // 今日头条 // // Created by YouXianMing on 14/11/26. // ...

  7. 如何添加设备UDID到开发者中心

    如何添加设备UDID到开发者中心 1. 登录开发者中心 2. 选择证书那一项 3. 选择Devices 4. 点选+按钮 5. 填上设备的UUID以及设备名字然后添加上 大功告成:) 附录: 如何获取 ...

  8. Linux 系统的/etc目录

    /etc目录下的重要文件 /etc/sysconfig/network 指定服务器上的网络配置信息 /etc/rc.d/init.d/network 网络配置脚本信息 网络配置脚本,开机通过脚本文件来 ...

  9. 一、Linux中的常用命令2 二、Vim编辑器的使用

    一.Linux的常用命令###<1>文件目录操作 13. echo:用于输出字符串,shell编程,echo 1. 输出字符串 : echo str ,shell编程会使用(类似java中 ...

  10. HTML5 JS 实现浏览器全屏(F11的效果)

    项目中有需要使用JS来控制浏览器全屏的方法 DEMO地址: http://zhongxia245.github.io/demo/js2fullpanel.html function fullScree ...