From a4ef07b8c4ffb7d091ab443ee4e833f774e1ad64 Mon Sep 17 00:00:00 2001 From: ZNEIAT <1149527164@qq.com> Date: Mon, 30 Apr 2018 23:06:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=A4=E6=A0=8F=E5=B8=83?= =?UTF-8?q?=E5=B1=80=E4=B8=8D=E8=83=BD=E5=9B=BA=E5=AE=9A=20#profile=20?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20(#320)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/js/main.js | 103 +++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/source/js/main.js b/source/js/main.js index 2fd1780..675813b 100644 --- a/source/js/main.js +++ b/source/js/main.js @@ -43,77 +43,84 @@ e.stopPropagation(); }); - // To Top & Fixed Profile + // To Top var sidebarElem = $('#sidebar'), toTopElem = $('#toTop'); - if (sidebarElem.length) { - checkDisplayToTop(); - checkFixedProfile(); + (function () { + if (!sidebarElem.length) return; + checkDisplayToTop(); $(document).on('scroll', function () { checkDisplayToTop(); - checkFixedProfile(); }); toTopElem.click(function () { $('body, html').animate({ scrollTop: 0 }, 600); }); - } - var isToTopDisplayed = false; - function checkDisplayToTop() { - var toTop = (sidebarElem.height() - $(window).height()) + 60; - var currentScrollTop = $(document).scrollTop(); - var needDisplay = (currentScrollTop > toTop) && (currentScrollTop > 0); + var isToTopDisplayed = false; + function checkDisplayToTop() { + var toTop = (sidebarElem.height() - $(window).height()) + 60; + var currentScrollTop = $(document).scrollTop(); + var needDisplay = (currentScrollTop > toTop) && (currentScrollTop > 0); - if ($(document).width() >= 800) { - if (needDisplay) { - if (isToTopDisplayed) return; - toTopElem.fadeIn(); - toTopElem.css('left', sidebarElem.offset().left); - isToTopDisplayed = true; + if ($(document).width() >= 800) { + if (needDisplay) { + if (isToTopDisplayed) return; + toTopElem.fadeIn(); + toTopElem.css('left', sidebarElem.offset().left); + isToTopDisplayed = true; + } else { + if (!isToTopDisplayed) return; + toTopElem.fadeOut(); + isToTopDisplayed = false; + } } else { - if (!isToTopDisplayed) return; - toTopElem.fadeOut(); - isToTopDisplayed = false; + toTopElem.show(); + toTopElem.css('right', 20); } - } else { - toTopElem.show(); - toTopElem.css('right', 20); } - } + })(); - var isFixedProfile = false; - function checkFixedProfile() { - if (!profileElem.is('.profile-fixed')) return; - if ($(document).width() < 800) return; + // Fixed Profile + (function () { + checkFixedProfile(); + $(document).on('scroll', function () { + checkFixedProfile(); + }); - var currentScrollTop = $(document).scrollTop(); - var profileInnerElem = $('#profile .profile-inner'); - var needFixed = currentScrollTop >= profileElem.offset().top + profileElem.outerHeight(true); + var isFixedProfile = false; + function checkFixedProfile() { + if (!profileElem.is('.profile-fixed')) return; + if ($(document).width() < 800) return; - if (needFixed) { - if (isFixedProfile) return; + var currentScrollTop = $(document).scrollTop(); + var profileInnerElem = $('#profile .profile-inner'); + var needFixed = currentScrollTop >= profileElem.offset().top + profileElem.outerHeight(true); - profileInnerElem.css('position', 'fixed') - .css('width', profileElem.innerWidth() + 'px') - .css('top', '0'); + if (needFixed) { + if (isFixedProfile) return; - // css animation fade-in - profileInnerElem.css('animation', ''); - profileInnerElem.addClass('anim-fade-in'); - isFixedProfile = true; - } else { - if (!isFixedProfile) return; + profileInnerElem.css('position', 'fixed') + .css('width', profileElem.innerWidth() + 'px') + .css('top', '0'); - profileInnerElem.css('position', '') - .css('width', '') - .css('top', ''); + // css animation fade-in + profileInnerElem.css('animation', ''); + profileInnerElem.addClass('anim-fade-in'); + isFixedProfile = true; + } else { + if (!isFixedProfile) return; - profileInnerElem.css('animation', 'none'); - isFixedProfile = false; + profileInnerElem.css('position', '') + .css('width', '') + .css('top', ''); + + profileInnerElem.css('animation', 'none'); + isFixedProfile = false; + } } - } + })(); })(jQuery);