throttle = function (func, wait, options) { var context, args, result; var timeout = null; var previous = 0; options || (options = {}); var later = function () { previous = options.leading === false ? 0 : new Date(); timeout = null; result = func.apply(context, args); }; return function () { var now = new Date(); if (!previous && options.leading === false) previous = now; var remaining = wait - (now - previous); context = this; args = arguments; if (remaining <= 0) { clearTimeout(timeout); timeout = null; previous = now; result = func.apply(context, args); } else if (!timeout && options.trailing !== false) { timeout = setTimeout(later, remaining); } return result; }; }; /** * 获取屏幕尺寸 * @returns {string} */ window.getScreenSize = function () { var iWinWidth = window.innerWidth || $(window).width(); return iWinWidth >= 1200 ? 'lg' : iWinWidth >= 1024 ? 'md' : 'xs'; }; function scrollFn() { var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop; var siteGlobal = $('.new-site-global'); var oBox = $('.new-left-header'); var footerY = $('footer').offset().top - $('footer').height(); console.log(getScreenSize()); if (getScreenSize() === 'xs' || getScreenSize() === 'md') { var pageY = siteGlobal.offset().top; if (scrollTop > pageY) { oBox.addClass('fixed'); } else { oBox.removeClass('fixed'); } for (var i = 0; i < siteGlobal.find('.cont-item').length; i++) { var offsetY = siteGlobal.find('.cont-item').eq(i).offset().top; var scrollY = scrollTop + $('header').height() + siteGlobal.find('.cont-item').eq(i).height() * 0.5; if (scrollY > offsetY) { oBox.find('li').removeClass('active').eq(i).addClass('active'); var text = oBox.find('li').eq(i).text(); $('.first-menu .h5').text(text); } } } else { var aside = $('.aside-global-wrap-new'); var _pageY = scrollTop - $('.new-site-global').offset().top + $('header').height() + 50; if (_pageY > 0) { oBox.addClass('fixed'); // console.log(scrollTop , footerY); if (scrollTop >= footerY) { console.log('超过'); oBox.css({ top: -(scrollTop - footerY) + 'px', left: (aside.width() - aside.find('.cont-wrapper').width()) / 2 + 'px' }); } else { console.log('ccc'); oBox.css({ top: '140px', left: (aside.width() - aside.find('.cont-wrapper').width()) / 2 + 'px' }); } } else { oBox.removeClass('fixed'); oBox.css({ top: 0, left: 0 }); } for (var i = 0; i < siteGlobal.find('.cont-item').length; i++) { var offsetY = siteGlobal.find('.cont-item').eq(i).offset().top; var scrollY = scrollTop + $('header').height() + siteGlobal.find('.cont-item').eq(i).height() * 0.75; if (scrollY > offsetY) { oBox.find('li').removeClass('active').eq(i).addClass('active'); var text = oBox.find('li').eq(i).text(); $('.first-menu .h5').text(text); } } } } window.onscroll = throttle(scrollFn, 0); // 点击dropdown 下拉伸展 $(document).ready(function () { if (getScreenSize() == 'xs' || getScreenSize() === 'md') { $('.new-left-header').find('ul').slideUp(); } $(window).resize(function () { if (getScreenSize() == 'xs' || getScreenSize() === 'md') { $('.new-left-header').removeAttr('style'); $('.new-left-header').find('ul').slideUp(); } }); $('.new-left-header').on('click', 'li', function (event) { event.stopPropagation(); var that = $(this); var ele = $(that.find('a').attr('href')); var index = that.index(); $('.new-left-header').find('li').removeClass('active').eq(index).addClass('active'); $('.new-left-header').find('.first-menu .text').html(that.text()); if (getScreenSize() === 'xs') { $('.new-left-header') .find('ul') .stop() .slideUp(300, function () { $('.new-left-header').removeClass('active'); $('html, body').animate( { scrollTop: ele.offset().top - $('header').height() - 60 + 'px' }, 500 ); }); } else { $('html, body').animate( { scrollTop: ele.offset().top - $('header').height() + 'px' }, 500 ); } return false; }); $('.new-left-header').on('click', '.first-menu', function (event) { console.log(getScreenSize()); if (!(getScreenSize() === 'xs' || getScreenSize() === 'md')) { return; } event.stopPropagation(); var that = $(this); console.log(that.parents('.new-left-header').hasClass('active')); if (that.parents('.new-left-header').hasClass('active')) { console.log('aaa'); that.siblings('ul') .stop() .slideUp(300, function () { that.parents('.new-left-header').removeClass('active'); }); } else { console.log('bbb'); that.parents('.new-left-header').addClass('active'); that.siblings('ul').stop().slideDown(300); } }); // 模拟blur });