init.js 2.79 KB
$(function() {
  // 截取参数方法
  $.getUrlParam = function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)")
    var r = window.location.search.substr(1).match(reg)
    if (r != null) {
      return unescape(r[2])
    } else {
      return null
    }
  }
  window.load = function(contentId, $default) {
    var $el = $('#' + contentId)
      // $footer = $('#footer')
    $el.load('./' + $default + '.html')
    $('.menu-name').click(function() {
      var $this = $(this)
      var url = './' + $this.attr('data-url') + '.html'
        // $footer.hide()
      $el.hide().load(url, function() {
        $el.fadeIn()
          // $footer.show()
      })
      $('.menu-item a').removeClass('menu-active')
      $this.addClass('menu-active')
    })
  }
  $('head').append('<link rel="shortcut icon" href="../../images/common/favicon.ico" /><link rel="preload" as="style" href="../../css/common.css"><link rel="stylesheet" href="../../css/common.css">')
  var $body = $('body')
  $body.hide()
  var $footer = $("<footer id='footer'></footer>")
  $footer.load('/html/components/footer.html')
  var $header = $("<header id='header' class='clearfix'></header>"),
    __data = null
  $header.load('/html/components/header.html', function() {
      $body.fadeIn(10)
      __data = $.getUrlParam('data')
      __data != null && $('.menu-name').eq(__data).trigger('click')
    }).appendTo($body.append($footer))
    // $(window).scroll(function (e) {
    //   if ($(document).scrollTop() >= 430) {
    //     $('.menu-box').addClass('fixed')
    //   } else if ($(document).scrollTop() < 430) {
    //     $('.menu-box').removeClass('fixed')
    //   }
    // })

  // 防抖
  function debounce(fn, wait) {
    var timeout = null;
    return function() {
      if (timeout !== null)
        clearTimeout(timeout);
      timeout = setTimeout(fn, wait);
    }
  }
  var $document = $(document),
    $menu_box = $('.menu-box'),
    scrollTop = 0

  // 滚动事件
  $menu_box && window.addEventListener('scroll', debounce(function() {
    scrollTop = $document.scrollTop()
    if (scrollTop >= 430) {
      !$menu_box.hasClass('fixed') && $menu_box.addClass('fixed')
    } else {
      $menu_box.hasClass('fixed') && $menu_box.removeClass('fixed')
    }
  }, 1));
  (function() {
    if (/s=noRedirect/i.test(location.search)) return;
    if (
      /AppleWebKit.*Mobile/i.test(navigator.userAgent) ||
      /MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(
        navigator.userAgent
      )
    ) {
      try {
        if (
          /Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(
            navigator.userAgent
          )
        ) {
          window.location.href = '../../dist/index.html';
        }
      } catch (e) {}
    }
  })();
})