|
- ; (function () {
- /*const adList = [
- {
- "width": 144,
- "height": 108,
- "pos": {
- "left": 50,
- "bottom": 50
- },
- "src": "https://openi.pcl.ac.cn/OpenIOSSG/promote/raw/branch/master/imgs/invitation/pic-01.png",
- "url": "/user/invitation_tpl",
- "show": true
- },
- {
- "width": 144,
- "height": 108,
- "pos": {
- "right": 50,
- "bottom": 50
- },
- "src": "https://openi.pcl.ac.cn/OpenIOSSG/promote/raw/branch/master/imgs/invitation/pic-01.png",
- "url": "/user/invitation_tpl",
- "show": false
- }
- ];*/
- const exceptPages = [
- // '/user/invitation_tpl'
- ];
-
- function initAd() {
- $.ajax({
- type: "GET",
- url: "/dashboard/invitation",
- dataType: "json",
- data: { filename: 'ad-pop-up.json' },
- success: function (res) {
- try {
- var data = JSON.parse(res);
- createAd(data);
- } catch (err) {
- console.log(err);
- }
- },
- error: function (err) {
- console.log(err);
- }
- });
- }
-
- function createAd(adList) {
- const adInfoStr = window.localStorage.getItem('ads') || '{}';
- let adInfoObj = JSON.parse(adInfoStr);
- const today = new Date();
- const timeTodayEnd = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1).getTime();
- const now = Date.now();
- const expTime = now + 4 * 60 * 60 * 1000;
- if (!adInfoObj.expires || adInfoObj.expires <= now) {
- adInfoObj = {
- expires: Math.min(timeTodayEnd, expTime),
- };
- }
- for (var i = 0, iLen = adList.length; i < iLen; i++) {
- var adI = adList[i];
- if (adI.show === false) continue;
- var showOr = adInfoObj[i] === false ? false : true;
- adInfoObj[i] = showOr;
- if (!showOr) continue;
- var adEl = $(`<div class="__ad_c__" _id="${i}" style="position:fixed;z-index:99999999;
- width:${adI.width}px;height:${adI.height}px;
- left:${adI.pos.left !== undefined ? adI.pos.left + 'px' : ''};
- top:${adI.pos.top !== undefined ? adI.pos.top + 'px' : ''};
- right:${adI.pos.right !== undefined ? adI.pos.right + 'px' : ''};
- bottom:${adI.pos.bottom !== undefined ? adI.pos.bottom + 'px' : ''};">
- <a style="" href="${adI.url}" target="_blank">
- <img style="height:100%;width:100%;" src="${adI.src}" />
- </a>
- <div class="__ad_close_c__" style="position:absolute;top:6px;right:6px;">
- <i class="ri-close-circle-line __ad_close__" style="color:white;font-size:18px;cursor:pointer;"></i>
- </div>
- </div>`);
- adEl.data('data', adI);
- $('body').append(adEl);
- }
- window.localStorage.setItem('ads', JSON.stringify(adInfoObj));
- }
-
- function initAdEvent() {
- $('body').on('click', '.__ad_c__ .__ad_close__', function () {
- var self = $(this);
- var adEl = self.closest('.__ad_c__');
- var adId = adEl.attr('_id');
- const adInfoStr = window.localStorage.getItem('ads') || '{}';
- const adInfoObj = JSON.parse(adInfoStr);
- adInfoObj[adId] = false;
- window.localStorage.setItem('ads', JSON.stringify(adInfoObj));
- adEl.remove();
- });
- var scrollTopOld = $(document).scrollTop();
- var timeHandler = null;
- $(window).scroll(function (e) {
- var scrollTop = $(document).scrollTop();
- var offSet = scrollTop - scrollTopOld;
- scrollTopOld = scrollTop;
- timeHandler && clearTimeout(timeHandler);
- $('.__ad_c__').each(function (_, item) {
- var self = $(item);
- var adData = self.data('data');
- if (adData.pos.bottom !== undefined) {
- self.animate({ bottom: adData.pos.bottom + offSet + 'px' }, 0);
- }
- if (adData.pos.top !== undefined) {
- self.animate({ top: adData.pos.top - offSet + 'px' }, 0);
- }
- })
- timeHandler = setTimeout(function () {
- $('.__ad_c__').each(function (_, item) {
- var self = $(item);
- var adData = self.data('data');
- if (adData.pos.bottom !== undefined) {
- self.animate({ bottom: adData.pos.bottom + 'px' }, 0);
- }
- if (adData.pos.top !== undefined) {
- self.animate({ top: adData.pos.top + 'px' }, 0);
- }
- })
- }, 20);
- });
- }
-
- setTimeout(function () {
- if (!$('meta[name="_uid"]').length) { // 未登录,不显示
- window.localStorage.removeItem('ads');
- return;
- }
- var pathName = window.location.pathname;
- if (exceptPages.indexOf(pathName) > -1) return; // 排除页,不显示
- initAd();
- initAdEvent();
- }, 0);
- })();
|