window.JVMegaMenu = window.JVMegaMenu || (function ($) { var isTouch = true, menu = function (_selector, _options) { var options = $.extend({}, menu.options, _options), selector = $(_selector).removeClass("cssmenu"), isRTL = selector.css("direction") == "rtl", type = (function () { var t; $.each("vertical horizontal accordion dropline".split(" "), function (i, v) { if (selector.is("." + v)) { t = v; return false } }); return t })(); if (type === "accordion") { var initAccordion = function (parent) { parent.accordion({ autoHeight: false, header: "> li.hasChild > :first-child", active: (function () { var index = false; parent.children("li").each(function (i) { if ($(this).find(".active:first").length > 0) { index = i; return false } }); return index })() }).find("ul").each(function () { initAccordion($(this)) }) }; initAccordion(selector); return } var allMenu = selector.find("li"), customs = {}, setPosition = function (el) { if (type == "dropline") { return } el.css({ display: "block", visibility: "hidden", top: "", left: "" }); var windowWidth = $(window).width(), elWidth = el.outerWidth(), offset = el.offset(), position = el.position(), parent = el.parent(), pWidth = parent.outerWidth(), pOffset = parent.offset(); offset.right = offset.left + elWidth; pOffset.right = pOffset.left + pWidth; if (isRTL) { if (elWidth > windowWidth) { el.css({ right: pOffset.right + 20 - windowWidth, display: "none", visibility: "", top: "100%" }); return } if (offset.left < 0) { el.css("right", - elWidth); if (el.offset().left + elWidth > windowWidth) { el.css({ right: -(elWidth - pOffset.right), top: parent.outerHeight() }) } } } else { if (elWidth > windowWidth) { el.css({ left: -pOffset.left + 20, display: "none", visibility: "", top: "100%" }); return } if (offset.right > windowWidth) { el.css("left", - elWidth); if (el.offset().left < 0) { el.css({ left: position.left - (offset.right - windowWidth), top: "100%" }) } } } el.css({ display: "none", visibility: "" }) }, getLevel = function (node) { return node.parents(".fx-subitem").length + 1 }, getConfig = function (i) { if (!i) { return options.defaultEff } return customs[i] || getConfig(i - 1) }, getTouches = function (e) { if (e.originalEvent) { if (e.originalEvent.touches && e.originalEvent.touches.length) { return e.originalEvent.touches } else { if (e.originalEvent.changedTouches && e.originalEvent.changedTouches.length) { return e.originalEvent.changedTouches } } } else { if (e.touches.length) { return e.touches } else { if (e.changedTouches.length) { return e.changedTouches } } } return null }; $.each(options.customEff, function (i, v) { customs[v.level] = v }); var isShowing, isHiding, showMenu = function (el) { var This = el, ops, $This = $(el).addClass("hover"), menu = $This.children(".fx-subitem"); if (!menu.length) { return } ops = getConfig(getLevel(menu)).showEff; clearTimeout($This.data().timeout); if (!menu.is(":visible")) { $This.data().timeout = setTimeout(function () { setPosition(menu); menu.show(ops.effect["@selected"], $.extend({ easing: ops.easing }, ops.effect[ops.effect["@selected"]]), ops.duration, function () { $This.is(".hover") || hideMenu(el) }) }, ops.delay) } }, hideMenu = function (el) { var This = el, ops, $This = $(el).removeClass("hover"), menu = $This.children(".fx-subitem"); if (!menu.length) { return } ops = getConfig(getLevel(menu)).hideEff; clearTimeout($This.data().timeout); if (menu.is(":visible")) { $This.data().timeout = setTimeout(function () { menu.hide(ops.effect["@selected"], $.extend({ easing: ops.easing }, ops.effect[ops.effect["@selected"]]), ops.duration, function () { $This.is(".hover") && showMenu(el) }) }, ops.delay) } }; allMenu.hover(function () { showMenu(this) }, function () { hideMenu(this) }); isTouch && (function () { selector.bind("touchstart", function (e) { var touchs = getTouches(e); if (touchs && touchs.length > 1 || selector.outerWidth() < selector.parent().innerWidth()) { return } var $document = $(document), $parent = selector.parent(), touch = touchs[0], bgTouch = { x: touch.screenX, y: touch.screenY }, bgScrollTop = $document.scrollTop(), bgPos = selector.position(), moveTouch = { x: 0, y: 0 }, moveHandle = function (e) { var touchs = getTouches(e); if (touchs.length > 1) { return } var touch = touchs[0]; moveTouch = { x: touch.screenX - bgTouch.x, y: touch.screenY - bgTouch.y }; if (isRTL) { selector.css("right", bgPos.right - moveTouch.x); $document.scrollTop(bgScrollTop - moveTouch.y) } else { selector.css("left", bgPos.left + moveTouch.x); $document.scrollTop(bgScrollTop - moveTouch.y) } return false }, endHandle = function (e) { $document.unbind("touchmove", moveHandle).unbind("touchend", endHandle); var enPos = selector.position(); if (isRTL) { if (enPos.left + selector.outerWidth() < $parent.innerWidth()) { selector.animate({ right: 0 }) } else { var rightMin = selector.parent().innerWidth() - selector.outerWidth(); if (enPos.left > 0) { selector.animate({ right: rightMin }) } } } else { if (enPos.left > 0) { selector.animate({ left: 0 }) } else { var lefMin = selector.parent().innerWidth() - selector.outerWidth(); if (enPos.left < lefMin) { selector.animate({ left: lefMin }) } } } }; bgPos.right = parseInt(selector.css("right")) || 0; console.log(bgPos.right); $document.bind("touchmove", moveHandle).bind("touchend", endHandle) }); allMenu.filter(".hasChild").each(function () { var This = this, $This = $(this), label = $(""), btn = $("", { "class": "fx-btnExpand", href: "javascript:void(0)" }).append(label); if ($This.children(".group-content").length) { return } $This.children(".fx-subitem").before(btn); if ($This.children(".separator").length) { btn.push($This.children(".separator")[0]) } }); var $document = $(document).bind("touchstart", function (e) { var touchs = getTouches(e); if (touchs.length > 1) { return } var moveHandle = function (e) { $document.unbind("touchend", endHandle).unbind("touchmove", moveHandle) }, endHandle = function () { $document.unbind("touchmove", moveHandle); if (!$(e.target).parents(selector.selector).length) { var actives = allMenu.filter(".hover").trigger("mouseleave"); if (actives.length) { return false } } }; $document.one("touchend", endHandle).one("touchmove", moveHandle) }) })(); var _width = function(){ if (selector.width() < $(window).width() && selector.hasClass("dropline")) { return "100%" } else { var width = 0; selector.children("li").each(function () { width += $(this).outerWidth() + (parseInt($(this).css('margin-left'))||0) + (parseInt($(this).css('margin-right'))||0) }); return width + 5 } } type == "vertical" || (function () { selector.css({ position: "absolute", top: 0, width: _width() }).parent().css({ position: "relative" }); $(window).resize(function () { selector.css({ position: "absolute", top: 0, width: _width() }).parent().css({ position: "relative" }) }); $(document).bind("jvframework_changedfontsize", function () { selector.css("width", _width()) }); $(window).load(function () { selector.css("width", _width()) }); var css = isRTL ? "right" : "left", at = 0, first = function () { at += 40; at = at > 0 ? 0 : at; var obj = {}; obj[css] = at; selector.stop().animate(obj, 300) }, last = function () { at -= 40; var min = selector.parent().innerWidth() - selector.outerWidth(); at = at < min ? min : at; var obj = {}; obj[css] = at; selector.stop().animate(obj, 300) }, reset = function () { windowWidth = $(window).width(); at = selector.position().left; if (selector.width() > windowWidth) { pnLast.show(); pnFirst.show() } else { pnLast.hide(); pnFirst.hide() } }, btnfirst = $("", { href: "javascript:void(0)" }).append($("").text("First")).click(first), btnLast = $("", { href: "javascript:void(0)" }).append($("").text("Last")).click(last), pnFirst = $("
", { "class": "flex-btnFirst" }).append(btnfirst), pnLast = $("
", { "class": "flex-btnLast" }).append(btnLast); selector.before(pnFirst).after(pnLast); $(window).resize(reset); reset() })() }; menu.options = {}; return menu })(jQuery);