1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 后台管理页面通过点击左侧导航栏的菜单项实现右边内容的改变

后台管理页面通过点击左侧导航栏的菜单项实现右边内容的改变

时间:2018-07-14 18:36:11

相关推荐

后台管理页面通过点击左侧导航栏的菜单项实现右边内容的改变

目录

前端页面部分js代码部分左侧导航栏组菜单项的打开与关闭url地址跳转地址栏url地址处理ajax实现右边页面内容变换内容面板上Bootstrap 面包屑导航

前端页面部分

<aside id="left-panel"><!-- User info --><div class="login-info"><span> <a href="javascript:void(0);" id="show-shortcut"data-action="toggleShortcut"> <imgsrc="${ctx}/img/avatars/sunny.png" alt="me" class="online" /> <span>john.doe</span> <i class="fa fa-angle-down"></i></a></span></div><!-- 导航栏--><nav><ul><li class=""><a href="${ctx}/manage/businessManag.html" title="商家管理"><i class="fa fa-lg fa-fw fa-home"></i> <span class="menu-item-parent">商家管理</span></a><ul><li class=""><a href="${ctx}/manage/businessManag.html" title="商家管理"><span class="menu-item-parent">商家管理</span></a></li><li class=""><a href="${ctx}/manage/mealsManag.html" title="菜品管理"><span class="menu-item-parent">菜品管理</span></a></li><li class=""><a href="${ctx}/manage/orderManag.html" title="订单管理"><span class="menu-item-parent">订单管理</span></a></li><li class=""><a href="${ctx}/manage/userManag.html" title="用户管理"><span class="menu-item-parent">用户管理</span></a></li><li class=""><a href="${ctx}/manage/testManager.html" title="测试页面"><span class="menu-item-parent">测试页面</span></a></li></ul></li></ul></nav><span class="minifyme" data-action="minifyMenu"> <iclass="fa fa-arrow-circle-left hit"></i></span></aside><!-- #MAIN PANEL 切换内容的主窗口--><div id="main" role="main"><div id="ribbon"><span class="ribbon-button-alignment"> <span id="refresh"class="btn btn-ribbon" data-action="resetWidgets"data-title="refresh" rel="tooltip" data-placement="bottom"data-original-title="<i class='text-warning fa fa-warning'></i> Warning! This will reset all your widget settings."data-html="true"data-reset-msg="Would you like to RESET all your saved widgets and clear LocalStorage?"><iclass="fa fa-refresh"></i></span></span><!-- breadcrumb --><ol class="breadcrumb"><!-- This is auto generated --></ol><!-- end breadcrumb --></div><!-- #MAIN CONTENT --><div id="content"></div><!-- END #MAIN CONTENT --></div><!-- END #MAIN PANEL --><!-- #PAGE FOOTER --><div class="page-footer"><div class="row"><div class="col-xs-12 col-sm-6"></div><div class="col-xs-6 col-sm-6 text-right hidden-xs"><div class="txt-color-white inline-block"></div><!-- end div--></div><!-- end col --></div><!-- end row --></div><!-- END FOOTER -->

js代码部分

变量值如下:

$.root_ = $('body');$.navAsAjax = true; debugState = true,bread_crumb = $('#ribbon ol.breadcrumb'),

左侧导航栏组菜单项的打开与关闭

$.fn.extend({//pass the options variable to the functionjarvismenu : function(options) {var defaults = {accordion : 'true',speed : 200,closedSign : '[+]',openedSign : '[-]'},// Extend our default options with those provided.opts = $.extend(defaults, options),//Assign current element to variable, in this case is UL element$this = $(this);//add a mark [+] to a multilevel menu$this.find("li").each(function() {if ($(this).find("ul").size() !== 0) {//add the multilevel sign next to the link$(this).find("a:first").append("<b class='collapse-sign'>" + opts.closedSign + "</b>");//avoid jumping to the top of the page when the href is an #if ($(this).find("a:first").attr('href') == "#") {$(this).find("a:first").click(function() {return false;});}}});//open active level$this.find("li.active").each(function() {$(this).parents("ul").slideDown(opts.speed);$(this).parents("ul").parent("li").find("b:first").html(opts.openedSign);$(this).parents("ul").parent("li").addClass("open");});$this.find("li a").click(function() {if ($(this).parent().find("ul").size() !== 0) {if (opts.accordion) {//Do nothing when the list is openif (!$(this).parent().find("ul").is(':visible')) {parents = $(this).parent().parents("ul");visible = $this.find("ul:visible");visible.each(function(visibleIndex) {var close = true;parents.each(function(parentIndex) {if (parents[parentIndex] == visible[visibleIndex]) {close = false;return false;}});if (close) {if ($(this).parent().find("ul") != visible[visibleIndex]) {$(visible[visibleIndex]).slideUp(opts.speed, function() {$(this).parent("li").find("b:first").html(opts.closedSign);$(this).parent("li").removeClass("open");});}}});}}// end ifif ($(this).parent().find("ul:first").is(":visible") && !$(this).parent().find("ul:first").hasClass("active")) {$(this).parent().find("ul:first").slideUp(opts.speed, function() {$(this).parent("li").removeClass("open");$(this).parent("li").find("b:first").delay(opts.speed).html(opts.closedSign);});} else {$(this).parent().find("ul:first").slideDown(opts.speed, function() {/*$(this).effect("highlight", {color : '#616161'}, 500); - disabled due to CPU clocking on phones*/$(this).parent("li").addClass("open");$(this).parent("li").find("b:first").delay(opts.speed).html(opts.openedSign);});} // end else} // end if});} // end function});

url地址跳转

首先我们需要了解window.location对象所包含的属性

由以上知识可以写出url跳转部分的代码:

//先判断导航栏里是否有内容if ($('nav').length) {checkURL();}//菜单项点击事件$(document).on('click', 'nav a[href!="#"]', function(e) {e.preventDefault();var $this = $(e.currentTarget);//判断a的父标签li标签是否被选中以及a标签是否有target属性if (!$this.parent().hasClass("active") && !$this.attr('target')) {// update window with hash// you could also do here: thisDevice === "mobile" - and save a little more memoryif ($.root_.hasClass('mobile-view-activated')) {$.root_.removeClass('hidden-menu');$('html').removeClass("hidden-menu-mobile-lock");window.setTimeout(function() {//忽略地址栏的请求参数if (window.location.search) {window.location.href =window.location.href.replace(window.location.search, '').replace(window.location.hash, '') + '#' + $this.attr('href');} else {window.location.hash = $this.attr('href');}}, 150);// it may not need this delay...} else {if (window.location.search) {window.location.href =window.location.href.replace(window.location.search, '').replace(window.location.hash, '') + '#' + $this.attr('href');} else {window.location.hash = $this.attr('href');}}}});// fire links with targets on different window$(document).on('click', 'nav a[target="_blank"]', function(e) {e.preventDefault();var $this = $(e.currentTarget);window.open($this.attr('href'));});// fire links with targets on same window$(document).on('click', 'nav a[target="_top"]', function(e) {e.preventDefault();var $this = $(e.currentTarget);window.location = ($this.attr('href'));});// all links with hash tags are ignored$(document).on('click', 'nav a[href="#"]', function(e) {e.preventDefault();});// DO on hash change$(window).on('hashchange', function() {checkURL();});

地址栏url地址处理

function checkURL() {//取出从#开始的url(锚),不包含#var url = location.href.split('#').splice(1).join('');//BEGIN: IE11解决方案if (!url) {try {var documentUrl = window.document.URL;if (documentUrl) {if (documentUrl.indexOf('#', 0) > 0 && documentUrl.indexOf('#', 0) < (documentUrl.length + 1)) {url = documentUrl.substring(documentUrl.indexOf('#', 0) + 1);}}} catch (err) {}}//END: IE11解决方案container = $('#content');// Do this if url exists (for page refresh, etc...)if (url) {// remove all active class$('nav li.active').removeClass("active");// match the url and add the active class$('nav li:has(a[href="' + url + '"])').addClass("active");var title = ($('nav a[href="' + url + '"]').attr('title'));// change page title from global vardocument.title = (title || document.title);// debugStateif (debugState){root.console.log("Page title: %c " + document.title, debugStyle_green);}// parse url to jqueryloadURL(url + location.search, container);} else {// grab the first URL from navvar $this = $('nav > ul > li:first-child > a[href!="#"]');//update hashwindow.location.hash = $this.attr('href');//clear dom reference$this = null;}}

ajax实现右边页面内容变换

function loadURL(url, container) {// debugStateif (debugState){root.root.console.log("Loading URL: %c" + url, debugStyle);}$.ajax({type : "GET",url : url,dataType : 'html',cache : true, // (warning: setting it to false will cause a timestamp and will call the request twice)beforeSend : function() {//IE11 bug fix for googlemaps (delete all google map instances)//check if the page is ajax = true, has google map class and the container is #contentif ($.navAsAjax && $(".google_maps")[0] && (container[0] == $("#content")[0]) ) {// target gmaps if any on pagevar collection = $(".google_maps"),i = 0;// run for eachmapcollection.each(function() {i ++;// get map id from class elementsvar divDealerMap = document.getElementById(this.id);if(i == collection.length + 1) {// "callback"} else {// destroy every map foundif (divDealerMap) divDealerMap.parentNode.removeChild(divDealerMap);// debugStateif (debugState){root.console.log("Destroying maps.........%c" + this.id, debugStyle_warning);}}});// debugStateif (debugState){root.console.log("✔ Google map instances nuked!!!");}} //end fix// destroy all datatable instancesif ( $.navAsAjax && $('.dataTables_wrapper')[0] && (container[0] == $("#content")[0]) ) {var tables = $.fn.dataTable.fnTables(true);$(tables).each(function () {if($(this).find('.details-control').length != 0) {$(this).find('*').addBack().off().remove();$(this).dataTable().fnDestroy();} else {$(this).dataTable().fnDestroy();}});// debugStateif (debugState){root.console.log("✔ Datatable instances nuked!!!");}}// end destroy// pop intervals (destroys jarviswidget related intervals)if ( $.navAsAjax && $.intervalArr.length > 0 && (container[0] == $("#content")[0]) && enableJarvisWidgets ) {while($.intervalArr.length > 0)clearInterval($.intervalArr.pop());// debugStateif (debugState){root.console.log("✔ All JarvisWidget intervals cleared");}}// end pop intervals// destroy all widget instancesif ( $.navAsAjax && (container[0] == $("#content")[0]) && enableJarvisWidgets && $("#widget-grid")[0] ) {$("#widget-grid").jarvisWidgets('destroy');// debugStateif (debugState){root.console.log("✔ JarvisWidgets destroyed");} }// end destroy all widgets // cluster destroy: destroy other instances that could be on the page // this runs a script in the current loaded page before fetching the new pageif ( $.navAsAjax && (container[0] == $("#content")[0]) ) {/** The following elements should be removed, if they have been created:**colorList*icon*picker*inline*And unbind events from elements:**icon*picker*inline*especially $(document).on('mousedown')*It will be much easier to add namespace to plugin events and then unbind using selected namespace.**See also:**/journal//05/06/a-jquery-plugin-boilerplate/*http://keith-wood.name/pluginFramework.html*/// this function is below the pagefunction for all pages that has instancesif (typeof pagedestroy == 'function') { try {pagedestroy(); if (debugState){root.console.log("✔ Pagedestroy()");} }catch(err) {pagedestroy = undefined; if (debugState){root.console.log("! Pagedestroy() Catch Error");} }} // destroy all inline chartsif ( $.fn.sparkline && $("#content .sparkline")[0] ) {$("#content .sparkline").sparkline( 'destroy' );if (debugState){root.console.log("✔ Sparkline Charts destroyed!");} }if ( $.fn.easyPieChart && $("#content .easy-pie-chart")[0] ) {$("#content .easy-pie-chart").easyPieChart( 'destroy' );if (debugState){root.console.log("✔ EasyPieChart Charts destroyed!");} }// end destory all inline charts// destroy form controls: Datepicker, select2, autocomplete, mask, bootstrap sliderif ( $.fn.select2 && $("#content select.select2")[0] ) {$("#content select.select2").select2('destroy');if (debugState){root.console.log("✔ Select2 destroyed!");}}if ( $.fn.mask && $('#content [data-mask]')[0] ) {$('#content [data-mask]').unmask();if (debugState){root.console.log("✔ Input Mask destroyed!");}}if ( $.fn.datepicker && $('#content .datepicker')[0] ) {$('#content .datepicker').off();$('#content .datepicker').remove();if (debugState){root.console.log("✔ Datepicker destroyed!");}}if ( $.fn.slider && $('#content .slider')[0] ) {$('#content .slider').off();$('#content .slider').remove();if (debugState){root.console.log("✔ Bootstrap Slider destroyed!");}}// end destroy form controls}// end cluster destroy// empty container and var to start garbage collection (frees memory)pagefunction = null;container.removeData().html("");// place cogcontainer.html('<h1 class="ajax-loading-animation"><i class="fa fa-cog fa-spin"></i> Loading...</h1>');// Only draw breadcrumb if it is main content materialif (container[0] == $("#content")[0]) {// clear everything else except these key DOM elements// we do this because sometime plugins will leave dynamic elements behind$('body').find('> *').filter(':not(' + ignore_key_elms + ')').empty().remove();// draw breadcrumbdrawBreadCrumb();// scroll up$("html").animate({scrollTop : 0}, "fast");} // end if},success : function(data) {// dump data to containercontainer.css({opacity : '0.0'}).html(data).delay(50).animate({opacity : '1.0'}, 300);// clear data vardata = null;container = null;},error : function(xhr, status, thrownError, error) {container.html('<h4 class="ajax-loading-error"><i class="fa fa-warning txt-color-orangeDark"></i> Error requesting <span class="txt-color-red">' + url + '</span>: ' + xhr.status + ' <span style="text-transform: capitalize;">' + thrownError + '</span></h4>');},async : true });}

内容面板上Bootstrap 面包屑导航

function drawBreadCrumb(opt_breadCrumbs) {var a = $("nav li.active > a"),b = a.length;bread_crumb.empty(), bread_crumb.append($("<li>Home</li>")), a.each(function() {bread_crumb.append($("<li></li>").html($.trim($(this).clone().children(".badge").remove().end().text()))), --b || (document.title = bread_crumb.find("li:last-child").text())});// Push breadcrumb manually -> drawBreadCrumb(["Users", "John Doe"]);// Credits: Philip Whitt | philip.whitt@if (opt_breadCrumbs != undefined) {$.each(opt_breadCrumbs, function(index, value) {bread_crumb.append($("<li></li>").html(value)); document.title = bread_crumb.find("li:last-child").text();});}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。