(function ($, window) { $.fn.extend({ "menustyle": function (options) { var _config = $.extend({ parent: ".w-nav-in", second: 1000, again: true }, options); var menu_node = $(this); var _tempstyle = menu_node.attr("style") ? menu_node.attr("style").split(';'):[]; var _style = ''; for (var i in _tempstyle) { if (!!_tempstyle[i] && _tempstyle[i].length > 0) { if (_tempstyle[i].tolowercase().indexof('display') < 0 ) { _style = _style + _tempstyle[i] + ";"; } } } // 加入定时器 if (menu_node.children(".active").length < 1 && _config.again === true) { settimeout(function () { _config.again = false; menu_node.menustyle(_config) if (!!_config.callback) { _config.callback(); } }, 100); return ''; } // 判断是否是pc,menu长度是否超出 var menu_node_parent = menu_node.parent(_config.parent); var goback = function () { menu_node.attr("style", _style); } // 剔除所有的样式还原操作 var clearmenu = function (menu_node, menu_node_parent) { menu_node.removeattr("style"); menu_node_parent.removeattr("style"); menu_node.children("li").each(function (key) { $(this).removeattr("style"); }); menu_node.next(".g-prev-next-ul").remove(); goback(); if (!!_config.callback) { _config.callback(); } } menu_node.removeattr("style"); menu_node_parent.removeattr("style"); // 外部控制是否执行菜单分页效果 if (typeof window.document.menustyle === 'boolean' && window.document.menustyle === false) { goback(); if (!!_config.callback) { _config.callback(); } return ''; } // ie7 不执行分页效果 if (window.navigator.appname == "microsoft internet explorer" && window.navigator.appversion.split(";")[1].replace(/[ ]/g, "") == "msie7.0") { goback(); if (!!_config.callback) { _config.callback(); } return ''; } if (menu_node_parent.parent().width() < menu_node_parent.width()) { var menu_node_parent_width = menu_node_parent.parent().width(); } else { var menu_node_parent_width = menu_node_parent.width(); } // 判断上下结构的宽度是否需要写入插件 if (menu_node_parent_width === 0 || ((menu_node.children("li").eq(0).outerwidth(true) + 10) >= menu_node_parent_width)) { clearmenu(menu_node, menu_node_parent); return ''; } // 判断总长度是否超过 var islanger = (function () { var lang = 0; menu_node.children("li").each(function () { lang += $(this).outerwidth(true); }); if (lang > (menu_node_parent_width) + 2) { return true; } return false; })(); if (!islanger) { clearmenu(menu_node, menu_node_parent); return ''; } clearmenu(menu_node, menu_node_parent); menu_node.css("visibility", "hidden"); // 开始执行 $("body").css("overflow-x", "hidden"); // 添加左右翻页标识 menu_node_parent.append(""); // 是否存在 min-width:xx% if (menu_node.children("li").eq(0).css("min-width").indexof("%") > 0) { menu_node.children("li").css("min-width", (parseint(menu_node_parent_width / 100 * parsefloat(menu_node.children("li").eq(0).css("min-width"))) + 1) + "px"); } // 定义 ul 的总长度 var menu_node_width = 2; var borderlw = parseint(menu_node.css('border-left-width')); var borderrw = parseint(menu_node.css('border-right-width')); if (isnan(borderlw)) { borderlw = 0; } if (isnan(borderrw)) { borderrw = 0; } menu_node_width += borderlw + borderrw; menu_node.children("li").each(function (key) { var li_this = $(this); var li_this_width = li_this.outerwidth(true); menu_node_width += li_this_width; }); menu_node_width = menu_node_width + parseint(menu_node.css("padding-left")) + parseint(menu_node.css("padding-right")); // 设置 .w-nav-in overflow:hidden 以及 估计宽度 menu_node_parent.css({"overflow": "hidden", "width": menu_node_parent_width + "px", "position": "relative"}); // 设置 menu 的宽度 var menu_node_height = menu_node.children("li").eq(0).outerheight(true) + parseint(menu_node.css("padding-top")) + parseint(menu_node.css("padding-bottom")); menu_node.css({"height": menu_node_height + "px", "width": menu_node_width + "px", "position": "relative", "left": "0px", "top": "0px"}); // 上一个以及下一个 var menu_node_next = menu_node.next(".g-prev-next-ul"); var _prev = menu_node_next.children(".g-prev-li").eq(0); var _next = menu_node_next.children(".g-next-li").eq(0); if (parseint(_prev.css("min-width")) > 0) { _prev.css("min-width", "0px"); } if (parseint(_prev.children("a").css("min-width")) > 0) { _prev.children("a").css("min-width", "0px"); } if (parseint(_next.css("min-width")) > 0) { _next.css("min-width", "0px"); } if (parseint(_next.children("a").css("min-width")) > 0) { _next.children("a").css("min-width", "0px"); } menu_node_next.css({"width": ((_prev.outerwidth(true) + _next.outerwidth(true)) + 3) + "px", "position": "absolute", "right": "0px", "top": "0px"}); // 设置prev以及next的显示位置 var setnodeprevnextcss = function () { var _left = menu_node_parent_width + menu_node_parent.offset().left - (_prev.outerwidth(true) + _next.outerwidth(true)); menu_node_next.css({"right": "0px", "top": "0px"}); } setnodeprevnextcss(0); menu_node_parent_width = menu_node_parent_width - parseint(menu_node.css("padding-left")) - parseint(menu_node.css("padding-right")); // 设置初始化时 加入左右标识的样式以及各个菜单的样式 var initnodelicss = function (menu_node_parent_width, menu_node, _prev, _next) { var tempwidth = 0; var showwidth = menu_node_parent_width - (_prev.outerwidth(true) + _next.outerwidth(true)) var isshow = true; menu_node.children("li").each(function (key) { if ($(this).hasclass("g-prev-li") || $(this).hasclass("g-next-li")) { return ''; } if (true === isshow) { if (($(this).outerwidth(true) + tempwidth) > showwidth) { isshow = false; $(this).css("visibility", "hidden"); } else { tempwidth += $(this).outerwidth(true); $(this).css("visibility", "visible"); } } else { $(this).css("visibility", "hidden"); } }); menu_node_parent.css("overflow", "visible"); } // 显示当前菜单的标签页 var fromtoshow = function (init) { if (menu_node.children(".active").length < 1) { init(); return ''; } var start = false; var end = false; var istrue = false; var tempwidth = 0; var showwidth = menu_node_parent_width - (_prev.outerwidth(true) + _next.outerwidth(true)) menu_node.children("li").each(function (key) { if ($(this).hasclass("g-prev-li") || $(this).hasclass("g-next-li")) { return ''; } if (($(this).outerwidth(true) + tempwidth) > showwidth) { if (false === end) { if (true === istrue) { end = key - 1; } else { tempwidth = $(this).outerwidth(true); start = key; end = false; } } } else { if (false === start) { start = key; } tempwidth += $(this).outerwidth(true); } if ($(this).hasclass("active")) { istrue = true; } }); if (start === 0) { init(); } else { // 计算左移长度 if (false === end) { end = menu_node.children("li").length - 1; } var left = 0; var isadd = true; menu_node.children("li").each(function (key) { if ($(this).hasclass("g-prev-li") || $(this).hasclass("g-next-li")) { return ''; } if (start <= key && key <= end) { if (true === isadd) { isadd = false; } $(this).css("visibility", "visible"); } else { if (true === isadd) { left += $(this).outerwidth(true); } $(this).css("visibility", "hidden"); } }); menu_node.css({"left": "-" + left + "px"}); menu_node_parent.css("overflow", "visible"); } } fromtoshow(function () { initnodelicss(menu_node_parent_width, menu_node, _prev, _next); }); // 设置是否显示上一页或者下一页 var setisshow = function (menu_node, _prev, _next) { if (menu_node.children("li").eq(0).css("visibility") === "visible") { _prev.css("visibility", "hidden"); } else { _prev.css("visibility", "visible"); } if (menu_node.children("li").eq(menu_node.children("li").length - 1).css("visibility") === "visible") { _next.css("visibility", "hidden"); } else { _next.css("visibility", "visible"); } } setisshow(menu_node, _prev, _next); // 开始滚动,点击下一页 _next.children(".g-next").click(function () { if (menu_node.children("li").eq(menu_node.children("li").length - 1).css("visibility") === "visible") { return ''; } var _this = $(this); if (menu_node_next.data("going") == 1 || menu_node_next.is(":animated")) { return ''; } menu_node_next.data("going", "1"); settimeout(function () { menu_node_next.data("going", "0"); menu_node_parent.css("overflow", "visible"); }, _config.second + 200); menu_node_parent.css("overflow", "hidden"); var _left = 0; var sstart = false; var hstart = false; if (menu_node.children("li").eq(0).css("visibility") === "hidden") { var isadd = true; menu_node.children("li").each(function (key) { if ($(this).hasclass("g-prev-li") || $(this).hasclass("g-next-li")) { return ''; } if ($(this).css("visibility") === "visible") { isadd = false; return ''; } if (true === isadd) { _left += $(this).outerwidth(true); } }); } menu_node.children("li").each(function (key) { if ($(this).hasclass("g-prev-li") || $(this).hasclass("g-next-li")) { return ''; } if ($(this).css("visibility") === "visible") { if (hstart === false) { hstart = key; } sstart = key + 1; _left += $(this).outerwidth(true); } }); var tempwidth = 0; var showwidth = menu_node_parent_width - (_prev.outerwidth(true) + _next.outerwidth(true)) var isshow = true; menu_node.children("li").each(function (key) { if ($(this).hasclass("g-prev-li") || $(this).hasclass("g-next-li")) { return ''; } if (false != sstart) { if (sstart <= key) { if (true === isshow) { if (($(this).outerwidth(true) + tempwidth) > showwidth) { isshow = false; $(this).css("visibility", "hidden"); } else { tempwidth += $(this).outerwidth(true); $(this).css("visibility", "visible"); } } else { $(this).css("visibility", "hidden"); } } } }); menu_node.animate({"left": "-" + _left + "px"}, _config.second, function () { menu_node.children("li").each(function (key) { if ($(this).hasclass("g-prev-li") || $(this).hasclass("g-next-li")) { } else { if (false != sstart) { if (sstart > key) { $(this).css("visibility", "hidden"); } } } }); setisshow(menu_node, _prev, _next); menu_node_parent.css("overflow", "visible"); }) }); _prev.click(function () { if (menu_node.children("li").eq(0).css("visibility") === "visible") { return ''; } var _this = $(this); if (menu_node_next.data("going") == 1 || menu_node_next.is(":animated")) { return ''; } menu_node_next.data("going", "1"); settimeout(function () { menu_node_next.data("going", "0"); menu_node_parent.css("overflow", "visible"); }, _config.second + 200); menu_node_parent.css("overflow", "hidden"); var left = parseint(menu_node.css("left")); var _left = false; var enno = menu_node.children("li").length - 1; var showwidth = menu_node_parent_width - (_prev.outerwidth(true) + _next.outerwidth(true)) // 是否到头 var tmp = true; var tmpleft = 0; menu_node.children("li").each(function (key) { if ($(this).hasclass("g-prev-li") || $(this).hasclass("g-next-li")) { return ''; } if (true === tmp) { if ($(this).css("visibility") === "hidden") { tmpleft += $(this).outerwidth(true); } else { tmp = false; } } }); // tmpleft < showwidth if (tmpleft < showwidth) { menu_node.children("li").each(function (key) { if ($(this).hasclass("g-prev-li") || $(this).hasclass("g-next-li")) { return ''; } $(this).css("visibility", "visible"); }); menu_node.animate({"left": "0px"}, _config.second, function () { initnodelicss(menu_node_parent_width, menu_node, _prev, _next); setisshow(menu_node, _prev, _next); }) return ''; } else { // 需要知道从哪里到哪里 var end = false; var start = false; var tleft = 0; var rs = false; if (menu_node.children("li").eq(enno).css("visibility") === "visible") { for (var i = enno; i >= 0; i--) { if (menu_node.children("li").eq(i).css("visibility") === "hidden") { if (end === false) { end = i; } if ((tleft + menu_node.children("li").eq(i).outerwidth(true)) > showwidth) { if (start === false) { start = i + 1; } } else { tleft += menu_node.children("li").eq(i).outerwidth(true); } } } } else { for (var i = enno - 1; i >= 0; i--) { if (rs === false && menu_node.children("li").eq(i).css("visibility") === "visible") { rs = true; } if (rs === true) { if (menu_node.children("li").eq(i).css("visibility") === "hidden") { if (end === false) { end = i; } if ((tleft + menu_node.children("li").eq(i).outerwidth(true)) > showwidth) { if (start === false) { start = i + 1; } } else { tleft += menu_node.children("li").eq(i).outerwidth(true); } } } } if (start === false) { start = 0; tleft = math.abs(left); } } menu_node.children("li").each(function (key) { if ($(this).hasclass("g-prev-li") || $(this).hasclass("g-next-li")) { return ''; } $(this).css("visibility", "visible"); }); menu_node.animate({"left": (left + tleft) + "px"}, _config.second, function () { menu_node.children("li").each(function (key) { if ($(this).hasclass("g-prev-li") || $(this).hasclass("g-next-li")) { return ''; } if (start <= key && key <= end) { $(this).css("visibility", "visible"); } else { $(this).css("visibility", "hidden"); } }); setisshow(menu_node, _prev, _next); menu_node_parent.css("overflow", "visible"); }); } }); menu_node.css("visibility", "visible"); if (!!_config.callback) { _config.callback(); } } }); })(jquery, window);