This commit is contained in:
haoouba 2021-01-24 23:27:05 +08:00
parent 94050964e1
commit 7d49bfe1cb
3 changed files with 54 additions and 88 deletions

View File

@ -1,27 +1,26 @@
window.Joe = function () { window.Joe = {
/* 解决移动端Hover问题 */ BASE_API: '/index.php/joe/api',
{ IS_MOBILE: /windows phone|iphone|android/gi.test(window.navigator.userAgent),
document.addEventListener('touchstart', () => {}) 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) { $('.joe_dropdown').each(function (index, item) {
const menu = $(this).find('.joe_dropdown__menu') const menu = $(this).find('.joe_dropdown__menu')
/* 弹出方式 */
const trigger = $(item).attr('trigger') || 'click' const trigger = $(item).attr('trigger') || 'click'
/* 弹出高度 */
const placement = $(item).attr('placement') || $(this).height() || 0 const placement = $(item).attr('placement') || $(this).height() || 0
/* 设置弹出高度 */
menu.css('top', placement) menu.css('top', placement)
/* 如果是hover则绑定hover事件 */
if (trigger === 'hover') { if (trigger === 'hover') {
$(this).hover( $(this).hover(
() => $(this).addClass('active'), () => $(this).addClass('active'),
() => $(this).removeClass('active') () => $(this).removeClass('active')
) )
} else { } else {
/* 否则绑定点击事件 */
$(this).on('click', function (e) { $(this).on('click', function (e) {
$(this).toggleClass('active') $(this).toggleClass('active')
$(document).one('click', () => $(this).removeClass('active')) $(document).one('click', () => $(this).removeClass('active'))
@ -34,13 +33,10 @@ window.Joe = function () {
/* 激活全局返回顶部功能 */ /* 激活全局返回顶部功能 */
{ {
const handleScroll = () => { const handleScroll = () => ((document.documentElement.scrollTop || document.body.scrollTop) > 300 ? $('.joe_action_item.scroll').addClass('active') : $('.joe_action_item.scroll').removeClass('active'))
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')
}
handleScroll() handleScroll()
$(window).on('scroll', () => 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 = '' let htmlStr = ''
timelife.forEach((item, index) => { timelife.forEach((item, index) => {
htmlStr += ` htmlStr += `
<div class="item"> <div class="item">
<div class="title"> <div class="title">
${item.title} ${item.title}
<span class="text">${item.num}</span> <span class="text">${item.num}</span>
${item.endTitle} ${item.endTitle}
</div>
<div class="progress">
<div class="progress-bar">
<div class="progress-bar-inner progress-bar-inner-${index}" style="width: ${item.percent}"></div>
</div> </div>
<div class="progress-percentage">${item.percent}</div> <div class="progress">
</div> <div class="progress-bar">
</div> <div class="progress-bar-inner progress-bar-inner-${index}" style="width: ${item.percent}"></div>
` </div>
<div class="progress-percentage">${item.percent}</div>
</div>
</div>`
}) })
$('.joe_aside__item.timelife .joe_aside__item-contain').html(htmlStr) $('.joe_aside__item.timelife .joe_aside__item-contain').html(htmlStr)
} }
@ -118,7 +113,7 @@ window.Joe = function () {
{ {
if ($('.joe_aside__item.ranking').length !== 0) { if ($('.joe_aside__item.ranking').length !== 0) {
$.ajax({ $.ajax({
url: Joe.prototype.BASE_API, url: Joe.BASE_API,
type: 'POST', type: 'POST',
data: { routeType: 'aside_ranking' }, data: { routeType: 'aside_ranking' },
success(res) { success(res) {
@ -127,11 +122,11 @@ window.Joe = function () {
if (res.code === 1) { if (res.code === 1) {
res.data.forEach((item, index) => { res.data.forEach((item, index) => {
htmlStr += ` htmlStr += `
<li class="item"> <li class="item">
<span class="sort">${index + 1}</span> <span class="sort">${index + 1}</span>
<a class="link" href="${item.url}" title="${item.title}" target="_blank" rel="noopener noreferrer nofollow">${item.title}</a> <a class="link" href="${item.url}" title="${item.title}" target="_blank" rel="noopener noreferrer nofollow">${item.title}</a>
</li> </li>
` `
}) })
} else { } else {
htmlStr += `<li class="error">数据抓取异常!</li>` htmlStr += `<li class="error">数据抓取异常!</li>`
@ -146,23 +141,8 @@ window.Joe = function () {
{ {
$('.joe_aside__item:last-child').css('top', $('.joe_header').height() + 15) $('.joe_aside__item:last-child').css('top', $('.joe_header').height() + 15)
} }
/* 懒加载 */
new LazyLoad('.lazyload') new LazyLoad('.lazyload')
}
/* 加密 */ console.timeEnd('Global.js执行时长')
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())

View File

@ -1,17 +1,11 @@
console.time("Index.js执行时长")
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
/* 激活轮播图功能 */ /* 激活轮播图功能 */
{ {
if ($('.joe_index__banner .swiper-container').length !== 0) { if ($('.joe_index__banner .swiper-container').length !== 0) {
let direction = 'horizontal' let direction = 'horizontal'
if (!Joe.prototype.IS_MOBILE && $('.joe_index__banner-recommend .item').length === 2) direction = 'vertical' if (!Joe.IS_MOBILE && $('.joe_index__banner-recommend .item').length === 2) direction = 'vertical'
new Swiper('.swiper-container', { new Swiper('.swiper-container', { keyboard: true, direction, loop: true, autoplay: true, mousewheel: true, pagination: { el: '.swiper-pagination' } })
keyboard: true,
direction,
loop: true,
autoplay: true,
mousewheel: true,
pagination: { el: '.swiper-pagination' }
})
} }
} }
@ -31,7 +25,7 @@ document.addEventListener('DOMContentLoaded', () => {
$('.joe_load').html('加载中') $('.joe_load').html('加载中')
$('.joe_index__list .joe_list__loading').show() $('.joe_index__list .joe_list__loading').show()
$.ajax({ $.ajax({
url: Joe.prototype.BASE_API, url: Joe.BASE_API,
type: 'POST', type: 'POST',
data: { routeType: 'publish_list', page: queryData.page, pageSize: queryData.pageSize, type: queryData.type }, data: { routeType: 'publish_list', page: queryData.page, pageSize: queryData.pageSize, type: queryData.type },
success(res) { success(res) {
@ -106,15 +100,8 @@ document.addEventListener('DOMContentLoaded', () => {
/* 激活列表特效 */ /* 激活列表特效 */
{ {
const wow = $('.joe_index__list').attr('data-wow') const wow = $('.joe_index__list').attr('data-wow')
if (wow !== 'off' && wow) { if (wow !== 'off' && wow) new WOW({ boxClass: 'wow', animateClass: `animated ${wow}`, offset: 0, mobile: true, live: true, scrollContainer: null }).init()
new WOW({
boxClass: 'wow',
animateClass: `animated ${wow}`,
offset: 0,
mobile: true,
live: true,
scrollContainer: null
}).init()
}
} }
console.timeEnd("Index.js执行时长")
}) })

View File

@ -1,8 +1,10 @@
console.time('Post&Page.js执行时长')
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
/* 获取本篇文章百度收录情况 */ /* 获取本篇文章百度收录情况 */
{ {
$.ajax({ $.ajax({
url: Joe.prototype.BASE_API, url: Joe.BASE_API,
type: 'POST', type: 'POST',
data: { routeType: 'baidu_record', site: window.location.href }, data: { routeType: 'baidu_record', site: window.location.href },
success(res) { success(res) {
@ -31,12 +33,7 @@ document.addEventListener('DOMContentLoaded', () => {
/* 设置文章内的链接为新窗口打开 */ /* 设置文章内的链接为新窗口打开 */
{ {
$('.joe_detail__article a').each(function () { $('.joe_detail__article a').each(() => $(this).attr({ target: '_blank', rel: 'noopener noreferrer nofollow' }))
$(this).attr({
target: '_blank',
rel: 'noopener noreferrer nofollow'
})
})
} }
/* 当前页的CID */ /* 当前页的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) const flag = viewsArr.includes(cid)
if (!flag) { if (!flag) {
$.ajax({ $.ajax({
url: Joe.prototype.BASE_API, url: Joe.BASE_API,
type: 'POST', type: 'POST',
data: { routeType: 'handle_views', cid }, data: { routeType: 'handle_views', cid },
success(res) { success(res) {
if (res.code !== 1) return if (res.code !== 1) return
$('#Joe_Article_Views').html(`${res.data.views} 阅读`) $('#Joe_Article_Views').html(`${res.data.views} 阅读`)
viewsArr.push(cid) viewsArr.push(cid)
const name = Joe.prototype.encryption('views') const name = Joe.encryption('views')
const val = Joe.prototype.encryption(JSON.stringify(viewsArr)) const val = Joe.encryption(JSON.stringify(viewsArr))
localStorage.setItem(name, val) 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') if (agreeArr.includes(cid)) $('.joe_detail__agree .icon-1').addClass('active')
else $('.joe_detail__agree .icon-2').addClass('active') else $('.joe_detail__agree .icon-2').addClass('active')
let _loading = false let _loading = false
$('.joe_detail__agree .icon').on('click', function () { $('.joe_detail__agree .icon').on('click', function () {
if (_loading) return if (_loading) return
_loading = true _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) let flag = agreeArr.includes(cid)
$.ajax({ $.ajax({
url: Joe.prototype.BASE_API, url: Joe.BASE_API,
type: 'POST', type: 'POST',
data: { routeType: 'handle_agree', cid, type: flag ? 'disagree' : 'agree' }, data: { routeType: 'handle_agree', cid, type: flag ? 'disagree' : 'agree' },
success(res) { success(res) {
@ -93,8 +90,8 @@ document.addEventListener('DOMContentLoaded', () => {
$('.joe_detail__agree .icon-1').addClass('active') $('.joe_detail__agree .icon-1').addClass('active')
$('.joe_detail__agree .icon').addClass('active') $('.joe_detail__agree .icon').addClass('active')
} }
const name = Joe.prototype.encryption('agree') const name = Joe.encryption('agree')
const val = Joe.prototype.encryption(JSON.stringify(agreeArr)) const val = Joe.encryption(JSON.stringify(agreeArr))
localStorage.setItem(name, val) localStorage.setItem(name, val)
}, },
complete() { complete() {
@ -103,4 +100,6 @@ document.addEventListener('DOMContentLoaded', () => {
}) })
}) })
} }
console.timeEnd('Post&Page.js执行时长')
}) })