common.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. $(function () {
  2. try {
  3. initTableStyle();
  4. } catch (error) {
  5. console.error(error);
  6. }
  7. // 移动端导航显示、隐藏
  8. try {
  9. $(".mobile-nav-icon").click(function () {
  10. if ($(".mobile-header-nav").css("opacity") == "0") {
  11. var headerHeight = $(".header-img").height();
  12. var windowHeight = $(window).height();
  13. $(".mobile-header-nav").height(windowHeight - headerHeight);
  14. $(".mobile-header-nav").css({
  15. "top": headerHeight + "px",
  16. "opacity": "1"
  17. })
  18. $(".mobile-header-nav").animate({
  19. "right": "0",
  20. }, 100)
  21. $(this).addClass("nav-icon-active");
  22. $("body").addClass("not-roll");
  23. } else {
  24. $(".mobile-header-nav").animate({
  25. "right": "-100%",
  26. "opacity": "0"
  27. }, 100);
  28. $(this).removeClass("nav-icon-active");
  29. $("body").removeClass("not-roll");
  30. }
  31. })
  32. } catch (error) {
  33. console.error(error)
  34. }
  35. try {
  36. // 移动端导航子栏目显示与隐藏
  37. $(".mobile-header-nav li").on("click", "a", function (e) {
  38. var len = $(this).next("ul").length;
  39. if (len > 0) {
  40. window.event ? window.event.cancelBubble = true : e.stopPropagation();
  41. $(this).parent("li").siblings().find("ul").hide();
  42. $(this).next("ul").slideToggle(200);
  43. }
  44. })
  45. } catch (error) {
  46. console.error(error)
  47. }
  48. try {
  49. //返回顶部
  50. $('.top').hide();
  51. $(window).scroll(function () {
  52. if ($(window).scrollTop() >= 200) {
  53. $('.top').fadeIn(300);
  54. } else { $('.top').fadeOut(300); }
  55. });
  56. $('.top').click(function () { $('html,body').animate({ scrollTop: '0px' }, 200); });
  57. } catch (error) {
  58. console.error(error)
  59. }
  60. try {
  61. $('#search').bind('keypress', function (event) {
  62. if (event.keyCode == 13) {
  63. if ($("#search").val().trim().length == 0) {
  64. return;
  65. }
  66. location.href = "/search?str=" + $("#search").val();
  67. }
  68. });
  69. $('#search-btn').bind('click', function (event) {
  70. if ($("#search").val().trim().length == 0) {
  71. $('#search').focus();
  72. return;
  73. }
  74. location.href = "/search?str=" + $("#search").val();
  75. });
  76. } catch (error) {
  77. console.error(error)
  78. }
  79. try {
  80. clearImgTextIndent();
  81. } catch (error) {
  82. console.error(error);
  83. }
  84. try {
  85. replaceMobileATarget();
  86. } catch (error) {
  87. console.error(error);
  88. }
  89. })
  90. function initTableStyle() {
  91. var articleMain = $("div.article-main");
  92. if (articleMain.length == 0) {
  93. return;
  94. }
  95. $("head").append('<link rel="stylesheet" type="text/css" href="/main/dist/websites/css/table.css"> ')
  96. $(articleMain).find("tr").attr("style", "");
  97. $(articleMain).find("td").attr("style", "");
  98. $(articleMain).find("table").attr("style", "");
  99. $(articleMain).find("table").addClass("layui-table");
  100. }
  101. // 解决旧网站抓取的图片不居中的问题
  102. function clearImgTextIndent() {
  103. var pAry = $("div.article-main").find("p");
  104. for (var i = 0; i < pAry.length; i++) {
  105. if ($(pAry[i]).find("img").length > 0) {
  106. $(pAry[i]).css("textIndent", "inherit")
  107. }
  108. }
  109. }
  110. // 如果是移动端,就把所有的新窗口改为当前页面打开
  111. function replaceMobileATarget() {
  112. if(isMobile()) {
  113. $("a").attr("target", "_self");
  114. }
  115. }
  116. function isMobile() {
  117. let userAgentInfo = navigator.userAgent;
  118. let mobileAgents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
  119. let mobile_flag = false;
  120. //根据userAgent判断是否是手机
  121. for (let v = 0; v < mobileAgents.length; v++) {
  122. if (userAgentInfo.indexOf(mobileAgents[v]) > 0) {
  123. mobile_flag = true;
  124. break;
  125. }
  126. }
  127. return mobile_flag;
  128. }