From 7d49bfe1cb34363c994c599bc6890601072d4628 Mon Sep 17 00:00:00 2001 From: haoouba <2323333339@qq.com> Date: Sun, 24 Jan 2021 23:27:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/js/joe.global.js | 84 +++++++++++++++----------------------- assets/js/joe.index.js | 27 ++++-------- assets/js/joe.post&page.js | 31 +++++++------- 3 files changed, 54 insertions(+), 88 deletions(-) diff --git a/assets/js/joe.global.js b/assets/js/joe.global.js index 8fc98c3..ee2523c 100644 --- a/assets/js/joe.global.js +++ b/assets/js/joe.global.js @@ -1,27 +1,26 @@ -window.Joe = function () { - /* 解决移动端Hover问题 */ - { - document.addEventListener('touchstart', () => {}) - } +window.Joe = { + BASE_API: '/index.php/joe/api', + IS_MOBILE: /windows phone|iphone|android/gi.test(window.navigator.userAgent), + encryption: str => window.btoa(unescape(encodeURIComponent(str))), + decrypt: str => decodeURIComponent(escape(window.atob(str))) +} +console.time('Global.js执行时长') + +document.addEventListener('DOMContentLoaded', () => { /* 激活全局下拉框功能 */ { $('.joe_dropdown').each(function (index, item) { const menu = $(this).find('.joe_dropdown__menu') - /* 弹出方式 */ const trigger = $(item).attr('trigger') || 'click' - /* 弹出高度 */ const placement = $(item).attr('placement') || $(this).height() || 0 - /* 设置弹出高度 */ menu.css('top', placement) - /* 如果是hover,则绑定hover事件 */ if (trigger === 'hover') { $(this).hover( () => $(this).addClass('active'), () => $(this).removeClass('active') ) } else { - /* 否则绑定点击事件 */ $(this).on('click', function (e) { $(this).toggleClass('active') $(document).one('click', () => $(this).removeClass('active')) @@ -34,13 +33,10 @@ window.Joe = function () { /* 激活全局返回顶部功能 */ { - const handleScroll = () => { - let scrollTop = document.documentElement.scrollTop || document.body.scrollTop - scrollTop > 300 ? $('.joe_action .joe_action_item.scroll').addClass('active') : $('.joe_action .joe_action_item.scroll').removeClass('active') - } + const handleScroll = () => ((document.documentElement.scrollTop || document.body.scrollTop) > 300 ? $('.joe_action_item.scroll').addClass('active') : $('.joe_action_item.scroll').removeClass('active')) handleScroll() $(window).on('scroll', () => handleScroll()) - $('.joe_action .joe_action_item.scroll').on('click', () => window.scrollTo({ top: 0, behavior: 'smooth' })) + $('.joe_action_item.scroll').on('click', () => window.scrollTo({ top: 0, behavior: 'smooth' })) } /* 激活侧边栏人生倒计时功能 */ @@ -85,20 +81,19 @@ window.Joe = function () { let htmlStr = '' timelife.forEach((item, index) => { htmlStr += ` -
-
- ${item.title} - ${item.num} - ${item.endTitle} -
-
-
-
+
+
+ ${item.title} + ${item.num} + ${item.endTitle}
-
${item.percent}
-
-
- ` +
+
+
+
+
${item.percent}
+
+
` }) $('.joe_aside__item.timelife .joe_aside__item-contain').html(htmlStr) } @@ -118,7 +113,7 @@ window.Joe = function () { { if ($('.joe_aside__item.ranking').length !== 0) { $.ajax({ - url: Joe.prototype.BASE_API, + url: Joe.BASE_API, type: 'POST', data: { routeType: 'aside_ranking' }, success(res) { @@ -127,11 +122,11 @@ window.Joe = function () { if (res.code === 1) { res.data.forEach((item, index) => { htmlStr += ` -
  • - ${index + 1} - ${item.title} -
  • - ` +
  • + ${index + 1} + ${item.title} +
  • + ` }) } else { htmlStr += `
  • 数据抓取异常!
  • ` @@ -146,23 +141,8 @@ window.Joe = function () { { $('.joe_aside__item:last-child').css('top', $('.joe_header').height() + 15) } - + /* 懒加载 */ new LazyLoad('.lazyload') -} -/* 加密 */ -Joe.prototype.encryption = str => window.btoa(unescape(encodeURIComponent(str))) -/* 解密 */ -Joe.prototype.decrypt = str => decodeURIComponent(escape(window.atob(str))) -/* 请求URL */ -Joe.prototype.BASE_API = '/index.php/joe/api' -/* 是否是手机 */ -Joe.prototype.IS_MOBILE = /windows phone|iphone|android/gi.test(window.navigator.userAgent) -/* 随机值 */ -Joe.prototype.getRandomIntInclusive = (min, max) => { - min = Math.ceil(min) - max = Math.floor(max) - return Math.floor(Math.random() * (max - min + 1) + min) -} - -document.addEventListener('DOMContentLoaded', () => Joe()) + console.timeEnd('Global.js执行时长') +}) diff --git a/assets/js/joe.index.js b/assets/js/joe.index.js index 4f50c75..60399fc 100644 --- a/assets/js/joe.index.js +++ b/assets/js/joe.index.js @@ -1,17 +1,11 @@ +console.time("Index.js执行时长") document.addEventListener('DOMContentLoaded', () => { /* 激活轮播图功能 */ { if ($('.joe_index__banner .swiper-container').length !== 0) { let direction = 'horizontal' - if (!Joe.prototype.IS_MOBILE && $('.joe_index__banner-recommend .item').length === 2) direction = 'vertical' - new Swiper('.swiper-container', { - keyboard: true, - direction, - loop: true, - autoplay: true, - mousewheel: true, - pagination: { el: '.swiper-pagination' } - }) + if (!Joe.IS_MOBILE && $('.joe_index__banner-recommend .item').length === 2) direction = 'vertical' + new Swiper('.swiper-container', { keyboard: true, direction, loop: true, autoplay: true, mousewheel: true, pagination: { el: '.swiper-pagination' } }) } } @@ -31,7 +25,7 @@ document.addEventListener('DOMContentLoaded', () => { $('.joe_load').html('加载中') $('.joe_index__list .joe_list__loading').show() $.ajax({ - url: Joe.prototype.BASE_API, + url: Joe.BASE_API, type: 'POST', data: { routeType: 'publish_list', page: queryData.page, pageSize: queryData.pageSize, type: queryData.type }, success(res) { @@ -106,15 +100,8 @@ document.addEventListener('DOMContentLoaded', () => { /* 激活列表特效 */ { const wow = $('.joe_index__list').attr('data-wow') - if (wow !== 'off' && wow) { - new WOW({ - boxClass: 'wow', - animateClass: `animated ${wow}`, - offset: 0, - mobile: true, - live: true, - scrollContainer: null - }).init() - } + if (wow !== 'off' && wow) new WOW({ boxClass: 'wow', animateClass: `animated ${wow}`, offset: 0, mobile: true, live: true, scrollContainer: null }).init() } + + console.timeEnd("Index.js执行时长") }) diff --git a/assets/js/joe.post&page.js b/assets/js/joe.post&page.js index 2496daa..1dcc719 100644 --- a/assets/js/joe.post&page.js +++ b/assets/js/joe.post&page.js @@ -1,8 +1,10 @@ +console.time('Post&Page.js执行时长') + document.addEventListener('DOMContentLoaded', () => { /* 获取本篇文章百度收录情况 */ { $.ajax({ - url: Joe.prototype.BASE_API, + url: Joe.BASE_API, type: 'POST', data: { routeType: 'baidu_record', site: window.location.href }, success(res) { @@ -31,12 +33,7 @@ document.addEventListener('DOMContentLoaded', () => { /* 设置文章内的链接为新窗口打开 */ { - $('.joe_detail__article a').each(function () { - $(this).attr({ - target: '_blank', - rel: 'noopener noreferrer nofollow' - }) - }) + $('.joe_detail__article a').each(() => $(this).attr({ target: '_blank', rel: 'noopener noreferrer nofollow' })) } /* 当前页的CID */ @@ -44,19 +41,19 @@ document.addEventListener('DOMContentLoaded', () => { /* 激活浏览功能 */ { - let viewsArr = localStorage.getItem(Joe.prototype.encryption('views')) ? JSON.parse(Joe.prototype.decrypt(localStorage.getItem(Joe.prototype.encryption('views')))) : [] + let viewsArr = localStorage.getItem(Joe.encryption('views')) ? JSON.parse(Joe.decrypt(localStorage.getItem(Joe.encryption('views')))) : [] const flag = viewsArr.includes(cid) if (!flag) { $.ajax({ - url: Joe.prototype.BASE_API, + url: Joe.BASE_API, type: 'POST', data: { routeType: 'handle_views', cid }, success(res) { if (res.code !== 1) return $('#Joe_Article_Views').html(`${res.data.views} 阅读`) viewsArr.push(cid) - const name = Joe.prototype.encryption('views') - const val = Joe.prototype.encryption(JSON.stringify(viewsArr)) + const name = Joe.encryption('views') + const val = Joe.encryption(JSON.stringify(viewsArr)) localStorage.setItem(name, val) } }) @@ -65,17 +62,17 @@ document.addEventListener('DOMContentLoaded', () => { /* 激活文章点赞功能 */ { - let agreeArr = localStorage.getItem(Joe.prototype.encryption('agree')) ? JSON.parse(Joe.prototype.decrypt(localStorage.getItem(Joe.prototype.encryption('agree')))) : [] + let agreeArr = localStorage.getItem(Joe.encryption('agree')) ? JSON.parse(Joe.decrypt(localStorage.getItem(Joe.encryption('agree')))) : [] if (agreeArr.includes(cid)) $('.joe_detail__agree .icon-1').addClass('active') else $('.joe_detail__agree .icon-2').addClass('active') let _loading = false $('.joe_detail__agree .icon').on('click', function () { if (_loading) return _loading = true - agreeArr = localStorage.getItem(Joe.prototype.encryption('agree')) ? JSON.parse(Joe.prototype.decrypt(localStorage.getItem(Joe.prototype.encryption('agree')))) : [] + agreeArr = localStorage.getItem(Joe.encryption('agree')) ? JSON.parse(Joe.decrypt(localStorage.getItem(Joe.encryption('agree')))) : [] let flag = agreeArr.includes(cid) $.ajax({ - url: Joe.prototype.BASE_API, + url: Joe.BASE_API, type: 'POST', data: { routeType: 'handle_agree', cid, type: flag ? 'disagree' : 'agree' }, success(res) { @@ -93,8 +90,8 @@ document.addEventListener('DOMContentLoaded', () => { $('.joe_detail__agree .icon-1').addClass('active') $('.joe_detail__agree .icon').addClass('active') } - const name = Joe.prototype.encryption('agree') - const val = Joe.prototype.encryption(JSON.stringify(agreeArr)) + const name = Joe.encryption('agree') + const val = Joe.encryption(JSON.stringify(agreeArr)) localStorage.setItem(name, val) }, complete() { @@ -103,4 +100,6 @@ document.addEventListener('DOMContentLoaded', () => { }) }) } + + console.timeEnd('Post&Page.js执行时长') })