$(function () { try { initTableStyle(); } catch (error) { console.error(error); } // 移动端导航显示、隐藏 try { $(".mobile-nav-icon").click(function () { if ($(".mobile-header-nav").css("opacity") == "0") { var headerHeight = $(".header-img").height(); var windowHeight = $(window).height(); $(".mobile-header-nav").height(windowHeight - headerHeight); $(".mobile-header-nav").css({ "top": headerHeight + "px", "opacity": "1" }) $(".mobile-header-nav").animate({ "right": "0", }, 100) $(this).addClass("nav-icon-active"); $("body").addClass("not-roll"); } else { $(".mobile-header-nav").animate({ "right": "-100%", "opacity": "0" }, 100); $(this).removeClass("nav-icon-active"); $("body").removeClass("not-roll"); } }) } catch (error) { console.error(error) } try { // 移动端导航子栏目显示与隐藏 $(".mobile-header-nav li").on("click", "a", function (e) { var len = $(this).next("ul").length; if (len > 0) { window.event ? window.event.cancelBubble = true : e.stopPropagation(); $(this).parent("li").siblings().find("ul").hide(); $(this).next("ul").slideToggle(200); } }) } catch (error) { console.error(error) } try { //返回顶部 $('.top').hide(); $(window).scroll(function () { if ($(window).scrollTop() >= 200) { $('.top').fadeIn(300); } else { $('.top').fadeOut(300); } }); $('.top').click(function () { $('html,body').animate({ scrollTop: '0px' }, 200); }); } catch (error) { console.error(error) } try { $('#search').bind('keypress', function (event) { if (event.keyCode == 13) { if ($("#search").val().trim().length == 0) { return; } location.href = "/search?str=" + $("#search").val(); } }); $('#search-btn').bind('click', function (event) { if ($("#search").val().trim().length == 0) { $('#search').focus(); return; } location.href = "/search?str=" + $("#search").val(); }); } catch (error) { console.error(error) } try { clearImgTextIndent(); } catch (error) { console.error(error); } try { replaceMobileATarget(); } catch (error) { console.error(error); } }) function initTableStyle() { var articleMain = $("div.article-main"); if (articleMain.length == 0) { return; } $("head").append(' ') $(articleMain).find("tr").attr("style", ""); $(articleMain).find("td").attr("style", ""); $(articleMain).find("table").attr("style", ""); $(articleMain).find("table").addClass("layui-table"); } // 解决旧网站抓取的图片不居中的问题 function clearImgTextIndent() { var pAry = $("div.article-main").find("p"); for (var i = 0; i < pAry.length; i++) { if ($(pAry[i]).find("img").length > 0) { $(pAry[i]).css("textIndent", "inherit") } } } // 如果是移动端,就把所有的新窗口改为当前页面打开 function replaceMobileATarget() { if(isMobile()) { $("a").attr("target", "_self"); } } function isMobile() { let userAgentInfo = navigator.userAgent; let mobileAgents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]; let mobile_flag = false; //根据userAgent判断是否是手机 for (let v = 0; v < mobileAgents.length; v++) { if (userAgentInfo.indexOf(mobileAgents[v]) > 0) { mobile_flag = true; break; } } return mobile_flag; }