window.Joe = function () {
/* 请求基准URL */
const BASE_API = '/index.php/joe/api';
/* 头部高度 */
const Header_Height = $('.joe_header').height();
/* 解决移动端Hover问题 */
document.addEventListener('touchstart', () => {});
/* 判断是否是手机 */
const IsMobile = /windows phone|iphone|android/gi.test(window.navigator.userAgent);
/* 设置侧边栏最后一个元素的高度 */
$('.joe_aside .joe_aside__item:last-child').css('top', Header_Height + 15);
/* Dropdown */
$('.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'));
e.stopPropagation();
});
menu.on('click', e => e.stopPropagation());
}
});
/* Timelife */
if ($('.joe_aside__item.timelife').length !== 0) {
let timelife = [
{ title: '今日已经过去', endTitle: '小时', num: 0, percent: '0%' },
{ title: '这周已经过去', endTitle: '天', num: 0, percent: '0%' },
{ title: '本月已经过去', endTitle: '天', num: 0, percent: '0%' },
{ title: '今年已经过去', endTitle: '个月', num: 0, percent: '0%' }
];
{
let nowDate = +new Date();
let todayStartDate = new Date(new Date().toLocaleDateString()).getTime();
let todayPassHours = (nowDate - todayStartDate) / 1000 / 60 / 60;
let todayPassHoursPercent = (todayPassHours / 24) * 100;
timelife[0].num = parseInt(todayPassHours);
timelife[0].percent = parseInt(todayPassHoursPercent) + '%';
}
{
let weeks = { 0: 7, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6 };
let weekDay = weeks[new Date().getDay()];
let weekDayPassPercent = (weekDay / 7) * 100;
timelife[1].num = parseInt(weekDay);
timelife[1].percent = parseInt(weekDayPassPercent) + '%';
}
{
let year = new Date().getFullYear();
let date = new Date().getDate();
let month = new Date().getMonth() + 1;
let monthAll = new Date(year, month, 0).getDate();
let monthPassPercent = (date / monthAll) * 100;
timelife[2].num = date;
timelife[2].percent = parseInt(monthPassPercent) + '%';
}
{
let month = new Date().getMonth() + 1;
let yearPass = (month / 12) * 100;
timelife[3].num = month;
timelife[3].percent = parseInt(yearPass) + '%';
}
let htmlStr = '';
timelife.forEach((item, index) => {
htmlStr += `
${item.title}
${item.num}
${item.endTitle}
`;
});
$('.joe_aside__item.timelife .joe_aside__item-contain').html(htmlStr);
}
/* Weather */
if ($('.joe_aside__item.weather').length !== 0) {
const key = $('.joe_aside__item.weather').attr('data-key');
const style = $('.joe_aside__item.weather').attr('data-style');
const aqiColor = { 1: 'FFFFFF', 2: '4A4A4A', 3: 'FFFFFF' };
window.WIDGET = { CONFIG: { layout: 2, width: '220', height: '270', background: style, dataColor: aqiColor[style], language: 'zh', key: key } };
}
/* Ranking */
if ($('.joe_aside__item.ranking').length !== 0) {
$.ajax({
url: BASE_API,
type: 'POST',
data: { routeType: 'ranking' },
success(res) {
$('.joe_aside__item.ranking .joe_aside__item-title .text').html(res.title);
let htmlStr = '';
if (res.code === 1) {
res.data.forEach((item, index) => {
htmlStr += `
${index + 1}
${item.title}
`;
});
} else {
htmlStr += `数据抓取异常!`;
}
$('.joe_aside__item.ranking .joe_aside__item-contain').html(htmlStr);
}
});
}
/* Index Swiper */
if ($('.joe_index__banner .swiper-container').length > 0) {
let direction = 'horizontal';
if (!IsMobile && $('.joe_index__banner-recommend .item').length === 2) direction = 'vertical';
new Swiper('.swiper-container', { direction, loop: true, autoplay: true, mousewheel: true, pagination: { el: '.swiper-pagination' } });
}
/* Index List */
if ($('.joe_index__list').length > 0) {
let queryData = { page: 1, pageSize: 10, type: 'created' };
const initDom = () => {
$('.joe_index__list .joe_list').html('');
const activeItem = $(`.joe_index__title-title .item[data-type="${queryData.type}"]`);
const activeLine = $('.joe_index__title-title .line');
activeItem.addClass('active').siblings().removeClass('active');
activeLine.css({ left: activeItem.position().left, width: activeItem.width() });
};
const pushDom = () => {
return new Promise((reslove, reject) => {
$('.joe_load').attr('loading', true);
$('.joe_load').html('加载中');
$('.joe_index__list .joe_list__loading').show();
$.ajax({
url: BASE_API,
type: 'POST',
data: { routeType: 'list', page: queryData.page, pageSize: queryData.pageSize, type: queryData.type },
success(res) {
if (res.data.length === 0) {
$('.joe_load').remove();
$('.joe_index__list .joe_list__loading').hide();
return;
}
res.data.forEach(_ => {
$('.joe_index__list .joe_list').append(`
`);
});
$('.joe_load').removeAttr('loading');
$('.joe_load').html('查看更多');
$('.joe_index__list .joe_list__loading').hide();
new LazyLoad('.list_lazyload');
reslove(res.data.length > 0 ? res.data.length - 1 : 0);
}
});
});
};
initDom();
pushDom();
$('.joe_index__title-title .item').on('click', async function () {
if ($(this).attr('data-type') === queryData.type) return;
queryData = { page: 1, pageSize: 10, type: $(this).attr('data-type') };
initDom();
pushDom();
});
$('.joe_load').on('click', async function () {
if ($(this).attr('loading')) return;
queryData.page++;
let length = await pushDom();
length = $('.joe_index__list .joe_list .joe_list__item').length - length;
const queryElement = `.joe_index__list .joe_list .joe_list__item:nth-child(${length})`;
const offset = $(queryElement).offset().top - Header_Height;
window.scrollTo({ top: offset - 15, behavior: 'smooth' });
});
}
new LazyLoad('.lazyload');
};
document.addEventListener('DOMContentLoaded', () => Joe());