1. 程式人生 > 其它 >springboot+layui學習系列(一):動態水平導航選單

springboot+layui學習系列(一):動態水平導航選單

免責宣告:本博文僅為自己學習記錄,如有參考價值,不甚榮幸。

網頁程式碼:

<html>
<head>
    <meta charset="UTF-8">
    <title>水平導航選單</title>
    <link rel="stylesheet" href="/resources/common-css/reset.css">
    <link rel="stylesheet" href="/resources/common-css/style.css"
>
</head> <body> <header class="header"> <nav class="menu radius" > <ul class="nav" id="navBarId"> </ul> </nav> </header> <script src="/resources/js/jquery-3.3.1.js"></script
>
<script> $(function(){ $.ajax({ type: "GET", url: "/resources/json/menu.json", dataType: "json", success: function (res) { var data =res.data; var liStr = ""
; //載入一級選單 for (var i = 0; i < data.length; i++) { liStr += "<li>"; liStr += "<a href=\"javascript:;\" data-src=\"" + data[i].href + "\" data-menuid=\"" + data[i].icon + "\" data-title=\"" + data[i].title + "\">" + data[i].title + "</a>"; if (data[i].children != null && data[i].children.length > 0) { //獲得二級選單資料 var child1 = data[i].children; liStr += "<ul>"; //載入二級選單 for (var j = 0; j < child1.length; j++) { liStr += "<li><a href=\"javascript:;\" data-src=\"" + child1[j].href + "\" data-menuid=\"" + child1[j].icon + "\" data-title=\"" + child1[j].title + "\">" + child1[j].title + "</a>"; if (child1[j].children != null && child1[j].children.length > 0) { //獲得三級選單資料 var child2 = child1[j].children; liStr += "<ul>"; //載入三級選單 for (var k = 0; k < child2.length; k++) { liStr += "<li><a href=\"javascript:;\" data-src=\"" + child2[j].href + "\" data-menuid=\"" + child2[j].icon + "\" data-title=\"" + child2[j].title + "\">" + child2[j].title + "</a></li>"; } liStr += "</ul>"; } liStr += "</li>"; } liStr +="</ul></li>"; } $("#navBarId").html(liStr); } } }); });
</script> </body> </html>

style.css

.header{
    width:98%;
    margin-left: auto;
    margin-right: auto;
    margin-top:30px;
    margin-bottom:30px;
}

.menu{
    background-color:#333333;
    position: relative;
    font-size: 18px;

}
.nav{
     margin-left: 5cm;
 }
.nav > li{
    display: inline-block;
    position: relative;

}
.nav > li > a{
    padding-top:0.5em;
    padding-bottom:0.5em;
    padding-left: 2em;
    padding-right: 3em;
    text-decoration:none;
}
.nav > li:hover > a{
    background-color:rgb(255, 255,0);
    color:rgb(0, 0, 0);
}
.nav a{
    color:#f5f5f5;
    display: block;
    text-decoration: none;
}
.nav  ul{
    display: none;
    position:absolute;
    background-color:transparent;
}
.nav  ul li{
    position:relative;
}
.nav  ul li + li{
    border-top: 1px solid rgb(70, 70, 70);
}

.nav > li:hover > ul{
    display: block;
    padding-top: 0.4em;
}
.nav > li > ul > li > a{
    padding-top:0.5em;
    padding-bottom:0.5em;
    width: 7em;
    padding-left: 1em;
    padding-right: 1em;
    background-color: rgb(50, 50, 50);
}
.nav > li > ul > li:hover > a{
    background-color:rgb(255, 255,0);
    color:rgb(0, 0, 0);
}
.nav > li > ul > li > ul{
    left:100%;
    top:0;
    padding-top: 1em;
}
.nav > li:last-child > ul > li > ul{
    left:-100%;
    top:0;
}
.nav > li > ul > li:hover >ul {
    display: block;
}
.nav > li > ul > li > ul > li > a{
    padding-top:0.4em;
    padding-bottom:0.4em;
    width: 7em;
    padding-left: 1em;
    padding-right: 1em;
    background-color: rgb(50, 50, 50);
}
.nav > li > ul > li > ul > li:hover > a{
    background-color:rgb(255, 255,0);
    color:rgb(0, 0, 0);
}

reset.css

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
main,
nav,
section,
summary {
    display: block;
}

audio,
canvas,
video {
    display: inline-block;
}

audio:not([controls]) {
    display: none;
    height: 0;
}

[hidden],
template {
    display: none;
}

html {
    -ms-text-size-adjust: 100%;
    -webkit-text-size-adjust: 100%;
}

a {
    background: transparent;
}

a:focus {
    outline: thin dotted;
}

a:active,
a:hover {
    outline: 0;
}

abbr[title] {
    border-bottom: 1px dotted;
}

b,
strong {
    font-weight: bold;
}

dfn {
    font-style: italic;
}

hr {
    -moz-box-sizing: content-box;
    box-sizing: content-box;
    height: 0;
}

mark {
    background: #ff0;
    color: #000;
}

code,
kbd,
pre,
samp {
    font-family: monospace, serif;
    font-size: 1em;
}

pre {
    white-space: pre-wrap;
}

q {
    quotes: "\201C" "\201D" "\2018" "\2019";
}

small {
    font-size: 80%;
}

sub,
sup {
    font-size: 75%;
    line-height: 0;
    position: relative;
    vertical-align: baseline;
}

sup {
    top: -0.5em;
}

sub {
    bottom: -0.25em;
}

img {
    border: 0;
}

svg:not(:root) {
    overflow: hidden;
}

figure {
    margin: 0;
}

fieldset {
    border: 1px solid #c0c0c0;
    margin: 0 2px;
    padding: 0.35em 0.625em 0.75em;
}

legend {
    border: 0;
    padding: 0;
}

button,
input,
select,
textarea {
    font-family: inherit;
    font-size: 100%;
    margin: 0;
}

button,
input {
    line-height: normal;
}

button,
select {
    text-transform: none;
}

button,
html input[type="button"],
input[type="reset"],
input[type="submit"] {
    -webkit-appearance: button;
    cursor: pointer;
}

button[disabled],
html input[disabled] {
    cursor: default;
}

input[type="text"]:hover,
input[type="text"]:focus,
input[type="text"]:active {
    border: 1px solid #CCC;
}

input[type="checkbox"],
input[type="radio"] {
    box-sizing: border-box;
    padding: 0;
}

input[type="search"] {
    -webkit-appearance: textfield;
    -moz-box-sizing: content-box;
    -webkit-box-sizing: content-box;
    box-sizing: content-box;
}

input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-decoration {
    -webkit-appearance: none;
}

button::-moz-focus-inner,
input::-moz-focus-inner {
    border: 0;
    padding: 0;
}

textarea {
    overflow: auto;
    vertical-align: top;
}

table {
    border-collapse: collapse;
    border-spacing: 0;
}

body,
div,
ol,
ul,
li,
h1,
h2,
h3,
h4,
h5,
h6,
p,
span,
th,
td,
dl,
dd,
form,
fieldset,
legend,
input,
textarea,
select {
    margin: 0 auto;
    padding: 0;
    border: 0;
}


/* Global */

body {
    color: #444;
    background: #FFF;
    font: 12px/24px "Microsoft Yahei", Arial, Verdana, Tahoma, Sans-Serif;
}

ul,
ol,
li {
    list-style: none;
}

table,
td,
th,
input {
    font-size: 12px;
}

h1,
h2,
h3,
h4,
h5,
h6 {
    font-weight: normal;
}

h1 {
    font-size: 28px;
}

h2 {
    font-size: 18px;
}

h3 {
    font-size: 16px;
}

h4 {
    font-size: 14px;
}

h5,
h6 {
    font-size: 12px;
}

.inner {
    overflow: hidden;
}

.clearfix {
    clear: both;
    font-size: 1px;
    width: 1px;
    height: 0;
    visibility: hidden;
    margin-top: 0px!important;
    *margin-top: -1px;
    line-height: 0
}

.radius {
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
}

.opacity {
    opacity: 0.5;
    filter: "alpha(opacity=50)";
    filter: alpha(opacity=50);
}

.folio-thumb img {
    width: 100%;
    -webkit-backface-visibility: hidden;
    -moz-backface-visibility: hidden;
    -ms-backface-visibility: hidden;
    backface-visibility: hidden;
    -webkit-transition-duration: 0.7s;
    -moz-transition-duration: 0.7s;
    -ms-transition-duration: 0.7s;
    -o-transition-duration: 0.7s;
}

.folio-thumb:hover img {
    -webkit-transform: scale(1.3);
    -moz-transform: scale(1.3);
    -o-transform: scale(1.3);
    -ms-transform: scale(1.3);
}

.mediaholder {
    overflow: hidden;
}

a:link,
a:visited {
    color: #444;
    outline: 0;
    text-decoration: none;
}

a:hover {
    color: #005BB5;
    text-decoration: underline;
    /* -webkit-transition:color 0.4s ease; -moz-transition:color 0.4s ease; -o-transition:color 0.4s ease; -ms-transition:color 0.4s ease; transition: color 0.4s ease;*/
}

返回json資料

{
  "code":0,
  "msg":"",
  "count":0,
  "data":
    [
      {
        "id":2,
        "pid":1,
        "title":"基礎管理",
        "icon":"&#xe857;",
        "href":"",
        "spread":true,
        "children":[
          {"id":7,
            "pid":2,
            "title":"客戶管理",
            "icon":"&#xe651;",
            "href":"/bus/toCustomerManager",
            "spread":false,
            "children":[]},
          {"id":8,
            "pid":2,
            "title":"供應商管理",
            "icon":"&#xe658;",
            "href":"/bus/toProviderManager",
            "spread":false,"children":[]},
          {"id":9,
            "pid":2,
            "title":"商品管理",
            "icon":"&#xe657;",
            "href":"/bus/toGoodsManager",
            "spread":false,
            "children":[]}]},
      {"id":3,
        "pid":1,
        "title":"進貨管理",
        "icon":"&#xe645;",
        "href":"",
        "spread":false,
        "children":[
          {"id":10,
            "pid":3,
            "title":"商品進貨",
            "icon":"&#xe756;",
            "href":"/bus/toInportManager",
            "spread":false,"children":[]},
          {"id":11,
            "pid":3,
            "title":"商品退貨查詢",
            "icon":"&#xe65a;",
            "href":"/bus/toOutportManager",
            "spread":false,
            "children":[]
          }
        ]
      },
      {"id":4,
        "pid":1,
        "title":"銷售管理",
        "icon":"&#xe611;",
        "href":"",
        "spread":false,
        "children":[
          {"id":12,
            "pid":4,
            "title":"商品銷售",
            "icon":"&#xe65b;",
            "href":"",
            "spread":false,
            "children":[]
          },
          {"id":13,
            "pid":4,
            "title":"銷售退貨查詢",
            "icon":"&#xe770;",
            "href":"",
            "spread":false,
            "children":[]
          }
        ]
      },
      {"id":5,
        "pid":1,
        "title":"系統管理",
        "icon":"&#xe614;",
        "href":"",
        "spread":false,
        "children":[
          {"id":14,
            "pid":5,
            "title":"部門管理",
            "icon":"&#xe770;",
            "href":"/sys/toDeptManager",
            "spread":false,
            "children":[]
          },
          {"id":15,
            "pid":5,
            "title":"選單管理",
            "icon":"&#xe857;",
            "href":"/sys/toMenuManager",
            "spread":false,"children":[]
          },
          {"id":16,
            "pid":5,
            "title":"許可權管理",
            "icon":"&#xe857;",
            "href":"/sys/toPermissionManager",
            "spread":false,
            "children":[]
          },
          {"id":17,
            "pid":5,
            "title":"角色管理",
            "icon":"&#xe650;",
            "href":"/sys/toRoleManager",
            "spread":false,
            "children":[]
          },{"id":18,
            "pid":5,
            "title":"使用者管理",
            "icon":"&#xe612;",
            "href":"/sys/toUserManager",
            "spread":false,
            "children":[]
          }
        ]
      },
      {"id":6,
        "pid":1,
        "title":"其它管理",
        "icon":"&#xe628;",
        "href":"",
        "spread":false,
        "children":[
          {"id":21,
            "pid":6,
            "title":"登陸日誌",
            "icon":"&#xe675;",
            "href":"/sys/toLoginfoManager",
            "spread":false,
            "children":[]
          },
          {"id":22,
            "pid":6,
            "title":"系統公告",
            "icon":"&#xe756;",
            "href":"/sys/toNoticeManager",
            "spread":false,"children":[]
          },
          {"id":23,
            "pid":6,
            "title":"圖示管理",
            "icon":"&#xe670;",
            "href":"../resources/page/icon.html",
            "spread":false,
            "children":[]
          },
          {"id":95,
            "pid":6,
            "title":"快取管理",
            "icon":"&#xe681;",
            "href":"/sys/toCacheManager",
            "spread":true,
            "children":[
            ]
          }
        ]
      }
    ]
}

三級導航json(參考)

[
	{
		"title": "一級選單",
		"icon": "icon-text",
		"href": "page/news/newsList.html",
		"spread": false,
		"children": [
			{
				"title": "二級選單",
				"icon": "&#xe61c;",
				"href": "page/404.html",
				"spread": false,
				"children": [
					{
						"title": "三級選單",
						"icon": "&#xe61c;",
						"href": "page/404.html",
						"spread": false
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					}
				]
			},
			{
				"title": "二級選單",
				"icon": "&#xe609;",
				"href": "page/login/login.html",
				"spread": false,
				"children": [
					{
						"title": "三級選單",
						"icon": "&#xe61c;",
						"href": "page/404.html",
						"spread": false
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					}
				]
			},
			{
				"title": "二級選單",
				"icon": "&#xe609;",
				"href": "page/login/login.html",
				"spread": false,
				"children": [
					{
						"title": "三級選單",
						"icon": "&#xe61c;",
						"href": "page/404.html",
						"spread": false
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					}
				]
			},
			{
				"title": "二級選單",
				"icon": "&#xe609;",
				"href": "page/login/login.html",
				"spread": false,
				"children": [
					{
						"title": "三級選單",
						"icon": "&#xe61c;",
						"href": "page/404.html",
						"spread": false
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					}
				]
			}
		]
	},
	{
		"title": "一級選單",
		"icon": "&#xe634;",
		"href": "page/img/images.html",
		"spread": false,
		"children": [
			{
				"title": "二級選單",
				"icon": "&#xe61c;",
				"href": "page/404.html",
				"spread": false,
				"children": [
					{
						"title": "三級選單",
						"icon": "&#xe61c;",
						"href": "page/404.html",
						"spread": false
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					}
				]
			},
			{
				"title": "二級選單",
				"icon": "&#xe609;",
				"href": "page/login/login.html",
				"spread": false,
				"children": [
					{
						"title": "三級選單",
						"icon": "&#xe61c;",
						"href": "page/404.html",
						"spread": false
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					}
				]
			},
			{
				"title": "二級選單",
				"icon": "&#xe609;",
				"href": "page/login/login.html",
				"spread": false,
				"children": [
					{
						"title": "三級選單",
						"icon": "&#xe61c;",
						"href": "page/404.html",
						"spread": false
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					}
				]
			},
			{
				"title": "二級選單",
				"icon": "&#xe609;",
				"href": "page/login/login.html",
				"spread": false,
				"children": [
					{
						"title": "三級選單",
						"icon": "&#xe61c;",
						"href": "page/404.html",
						"spread": false
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					}
				]
			}
		]
	},
	{
		"title": "一級選單",
		"icon": "&#xe630;",
		"href": "",
		"spread": false,
		"children": [
			{
				"title": "二級選單",
				"icon": "&#xe61c;",
				"href": "page/404.html",
				"spread": false,
				"children": [
					{
						"title": "三級選單",
						"icon": "&#xe61c;",
						"href": "page/404.html",
						"spread": false
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					}
				]
			},
			{
				"title": "二級選單",
				"icon": "&#xe609;",
				"href": "page/login/login.html",
				"spread": false,
				"children": [
					{
						"title": "三級選單",
						"icon": "&#xe61c;",
						"href": "page/404.html",
						"spread": false
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					}
				]
			},
			{
				"title": "二級選單",
				"icon": "&#xe609;",
				"href": "page/login/login.html",
				"spread": false,
				"children": [
					{
						"title": "三級選單",
						"icon": "&#xe61c;",
						"href": "page/404.html",
						"spread": false
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					}
				]
			},
			{
				"title": "二級選單",
				"icon": "&#xe609;",
				"href": "page/login/login.html",
				"spread": false,
				"children": [
					{
						"title": "三級選單",
						"icon": "&#xe61c;",
						"href": "page/404.html",
						"spread": false
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					},
					{
						"title": "三級選單",
						"icon": "&#xe609;",
						"href": "page/login/login.html",
						"spread": false,
						"target": "_blank"
					}
				]
			}
		]
	}
]