$(function() { $(".wrapul>li").each(function() { if ($(this).find(".childnav a").length == '0') { $(this).find(".childnav").remove(); } }) var wrapul = $('.wrapul'); var ulli = $('.wrapul>li'); var solidbar = $('.solidbar'); function init() { var curindex = $('.wrapul>li.cur').index(); var sumliw = 0; for (var i = 0; i < curindex; i++) { sumliw = sumliw + ulli.eq(i).outerwidth(); }; var w = (curindex + 1) * 30; var lw = ulli.eq(i).outerwidth() * 0.5 - 17; solidbar.css('left', w + sumliw + lw + 'px'); }; init(); ulli.hover(function() { var index = $(this).index(); var sumliw = 0; var w1 = (index + 1) * 30; var lw1 = ulli.eq(index).outerwidth() * 0.5 - 17; for (var i = 0; i < index; i++) { sumliw = sumliw + ulli.eq(i).width(); }; solidbar.stop().animate({ left: w1 + sumliw + lw1 + 'px' }, 200); $(this).find('.childnav').stop().slidedown(400); }, function() { var curindex = $('.wrapul>li.cur').index(); var sumliw = 0; var w1 = (curindex + 1) * 30; var lw2 = ulli.eq(curindex).outerwidth() * 0.5 - 17; for (var i = 0; i < curindex; i++) { sumliw = sumliw + ulli.eq(i).width(); }; solidbar.stop().animate({ left: w1 + sumliw + lw2 + 'px', }, 200); $(this).find('.childnav').stop().slideup(400); }); /*返回顶部*/ $(window).scroll(function() { //只要窗口滚动,就触发下面代码 var scrollt = document.documentelement.scrolltop + document.body.scrolltop; //获取滚动后的高度 if (scrollt > 500) { //判断滚动后高度超过200px,就显示 $("#top").fadein(200); //淡出 } else { $("#top").stop().fadeout(200); //如果返回或者没有超过,就淡入.必须加上stop()停止之前动画,否则会出现闪动 } if ($(".ban").length > '0') { var h = $(".header").height() + $(".ban").height(); } else { var h = $(".header").height() + $(".zban").height(); } if (scrollt > h) { //判断滚动后高度超过200px,就显示 $(".dh").addclass("fixed"); //淡出 } else { $(".dh").removeclass("fixed"); //淡出 } }); $("#top").click(function() { //当点击标签的时候,使用animate在200毫秒的时间内,滚到顶部 $("html,body").animate({ scrolltop: "0px" }, 200); }); //手机展开二级 $('.dh .oname>span').hammer().on('tap', function() { $(this).parents(".oname").toggleclass('on'); $(this).parents(".dh").find("ul").slidetoggle(); }); $(".flist:not([id='p3'])").remove(); $('.dh ul>li').each(function() { if ($(this).find('.flist>p').length == '0') { $(this).find('span').remove(); $(this).parent("ul").addclass("none"); } }); //手机展开二级 $('.dh ul li>p>span').hammer().on('tap', function() { $(this).parents("li").toggleclass('active'); $(this).parents("li").find(".flist").slidetoggle(); $(this).parents("li").siblings().removeclass('active'); $(this).parents("li").siblings().find(".flist").slideup(); }); //底部菜单打开 //展开子菜单 $('.footer .lm li>p>span').hammer().on('tap', function() { $(this).parents("li").toggleclass('on'); $(this).parents("li").find(".list").slidetoggle(); $(this).parents("li").siblings().removeclass('on'); $(this).parents("li").siblings().find(".list").slideup(); }); /*搜索按钮*/ $('.h-search-button').hammer().on('tap', function() { $(".h-search .search-input").val(''); $(".search-results-info,.results-item").html(""); $('.results-more').hide(); $("html").toggleclass('disablescroll'); $(".h-search").toggleclass('searchvisible'); $(".navlist").removeclass('navlistlock'); /*关闭导航*/ if ($('html').hasclass('disablescroll')) { $('html').bind("touchmove", function(e) { e.preventdefault(); }); } else { $('html').unbind("touchmove"); } /*关闭导航*/ if ($('.h-search').hasclass('searchvisible')) { $("html").addclass('disablescroll'); } $('.list').slideup(400); $('.navlist').hide(); changeicon(1); $('body').unbind("touchmove"); event.stoppropagation(); //阻止冒泡 }); /*搜索关闭按钮*/ $('.h-search-close-button').hammer().on('tap', function() { $("html").removeclass('disablescroll'); $(".h-search").removeclass('searchvisible'); $(".h-search .search-input").val(''); $(".search-results-info,.results-item").html(""); $('.results-more').hide(); }); /*导航关闭按钮*/ $('.h-n-close-button').hammer().on('tap', function() { $("html").removeclass('disablescroll'); $('.list').slideup(400); $('.navlist').stop().slidetoggle(300).removeclass('navlistlock'); changeicon(1); $('body').unbind("touchmove"); event.stoppropagation(); //阻止冒泡 }); //switch>一级 $('.switch').click(function(event) { $(".h-search .search-input").val(''); $(".search-results-info,.results-item").html(""); $('.results-more').hide(); $("html").toggleclass('disablescroll'); $(".h-search").removeclass('searchvisible'); if ($('html').hasclass('disablescroll')) { $('html').bind("touchmove", function(e) { e.preventdefault(); }); } else { $('html').unbind("touchmove"); } $('body').bind("touchmove", function(event) { event.preventdefault; }) $('.navlist').stop().slidetoggle(300).toggleclass('navlistlock'); /*关闭导航*/ if ($('.navlist').hasclass('navlistlock')) { $("html").removeclass('disablescroll'); } //收起下级 if ($('.navlist').css('display') == 'block') { $('.list').slideup(400); $('html').addclass('disablescroll'); changeicon(1); $('body').unbind("touchmove"); } event.stoppropagation(); //阻止冒泡 }); // 一级>二级 $('.firstlistitem').click(function() { var isshow = $(this).hasclass('showicon1'); var ishide = $(this).hasclass('hideicon1'); if (isshow) { $(this).children('.list').stop().slidedown(400); $(this).removeclass('showicon1').addclass('hideicon1'); $('.navlist').removeclass('navlistlock'); //收起兄弟元素及其子元素 $(this).siblings('.firstlistitem').removeclass('hideicon1').addclass('showicon1').find('.list').slideup(400); changeicon(2); } else if (ishide) { $(this).children('.list').stop().slideup(400); $(this).removeclass('hideicon1').addclass('showicon1'); $('.navlist').addclass('navlistlock'); //收起 $(this).removeclass('hideicon1').addclass('showicon1').find('.list').slideup(400); changeicon(2); } event.stoppropagation(); //阻止冒泡 }); // 二级>三级 $('.secondlistitem').click(function(event) { var isshow = $(this).hasclass('showicon2'); var ishide = $(this).hasclass('hideicon2'); if (isshow) { $(this).children('.list').stop().slidedown(400); $(this).removeclass('showicon2').addclass('hideicon2'); //收起兄弟及其子元素 $(this).siblings('.secondlistitem').removeclass('hideicon2').addclass('showicon2').find('.list').slideup(400); } else if (ishide) { $(this).children('.list').stop().slideup(400); $(this).removeclass('hideicon2').addclass('showicon2'); } event.stoppropagation(); //阻止冒泡 }); }); // 头部 导航 菜单 $('.firstlistitem').each(function() { if ($(this).find('.secondlist>li').length == '0') { $(this).find('span,.secondlist').remove(); } }); $('.secondlistitem').each(function() { if ($(this).find('.thirdlist>li').length == '0') { $(this).find('span,.thirdlist').remove(); } }); function changeicon(n) { switch (n) { case 1: $('.firstlistitem').each(function() { if ($(this).hasclass('hideicon1')) { $(this).removeclass('hideicon1').addclass('showicon1'); } }); $('.secondlistitem').each(function() { if ($(this).hasclass('hideicon2')) { $(this).removeclass('hideicon2').addclass('showicon2'); } }); break; case 2: $('.secondlistitem').each(function() { if ($(this).hasclass('hideicon2')) { $(this).removeclass('hideicon2').addclass('showicon2'); } }); break; } }