You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ad.js 4.6 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ; (function () {
  2. /*const adList = [
  3. {
  4. "width": 144,
  5. "height": 108,
  6. "pos": {
  7. "left": 50,
  8. "bottom": 50
  9. },
  10. "src": "https://openi.pcl.ac.cn/OpenIOSSG/promote/raw/branch/master/imgs/invitation/pic-01.png",
  11. "url": "/user/invitation_tpl",
  12. "show": true
  13. },
  14. {
  15. "width": 144,
  16. "height": 108,
  17. "pos": {
  18. "right": 50,
  19. "bottom": 50
  20. },
  21. "src": "https://openi.pcl.ac.cn/OpenIOSSG/promote/raw/branch/master/imgs/invitation/pic-01.png",
  22. "url": "/user/invitation_tpl",
  23. "show": false
  24. }
  25. ];*/
  26. const exceptPages = [
  27. // '/user/invitation_tpl'
  28. ];
  29. function initAd() {
  30. $.ajax({
  31. type: "GET",
  32. url: "/dashboard/invitation",
  33. dataType: "json",
  34. data: { filename: 'ad-pop-up.json' },
  35. success: function (res) {
  36. try {
  37. var data = JSON.parse(res);
  38. createAd(data);
  39. } catch (err) {
  40. console.log(err);
  41. }
  42. },
  43. error: function (err) {
  44. console.log(err);
  45. }
  46. });
  47. }
  48. function createAd(adList) {
  49. const adInfoStr = window.localStorage.getItem('ads') || '{}';
  50. let adInfoObj = JSON.parse(adInfoStr);
  51. const today = new Date();
  52. const timeTodayEnd = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1).getTime();
  53. const now = Date.now();
  54. const expTime = now + 4 * 60 * 60 * 1000;
  55. if (!adInfoObj.expires || adInfoObj.expires <= now) {
  56. adInfoObj = {
  57. expires: Math.min(timeTodayEnd, expTime),
  58. };
  59. }
  60. for (var i = 0, iLen = adList.length; i < iLen; i++) {
  61. var adI = adList[i];
  62. if (adI.show === false) continue;
  63. var showOr = adInfoObj[i] === false ? false : true;
  64. adInfoObj[i] = showOr;
  65. if (!showOr) continue;
  66. var adEl = $(`<div class="__ad_c__" _id="${i}" style="position:fixed;z-index:99999999;
  67. width:${adI.width}px;height:${adI.height}px;
  68. left:${adI.pos.left !== undefined ? adI.pos.left + 'px' : ''};
  69. top:${adI.pos.top !== undefined ? adI.pos.top + 'px' : ''};
  70. right:${adI.pos.right !== undefined ? adI.pos.right + 'px' : ''};
  71. bottom:${adI.pos.bottom !== undefined ? adI.pos.bottom + 'px' : ''};">
  72. <a style="" href="${adI.url}" target="_blank">
  73. <img style="height:100%;width:100%;" src="${adI.src}" />
  74. </a>
  75. <div class="__ad_close_c__" style="position:absolute;top:6px;right:6px;">
  76. <i class="ri-close-circle-line __ad_close__" style="color:white;font-size:18px;cursor:pointer;"></i>
  77. </div>
  78. </div>`);
  79. adEl.data('data', adI);
  80. $('body').append(adEl);
  81. }
  82. window.localStorage.setItem('ads', JSON.stringify(adInfoObj));
  83. }
  84. function initAdEvent() {
  85. $('body').on('click', '.__ad_c__ .__ad_close__', function () {
  86. var self = $(this);
  87. var adEl = self.closest('.__ad_c__');
  88. var adId = adEl.attr('_id');
  89. const adInfoStr = window.localStorage.getItem('ads') || '{}';
  90. const adInfoObj = JSON.parse(adInfoStr);
  91. adInfoObj[adId] = false;
  92. window.localStorage.setItem('ads', JSON.stringify(adInfoObj));
  93. adEl.remove();
  94. });
  95. var scrollTopOld = $(document).scrollTop();
  96. var timeHandler = null;
  97. $(window).scroll(function (e) {
  98. var scrollTop = $(document).scrollTop();
  99. var offSet = scrollTop - scrollTopOld;
  100. scrollTopOld = scrollTop;
  101. timeHandler && clearTimeout(timeHandler);
  102. $('.__ad_c__').each(function (_, item) {
  103. var self = $(item);
  104. var adData = self.data('data');
  105. if (adData.pos.bottom !== undefined) {
  106. self.animate({ bottom: adData.pos.bottom + offSet + 'px' }, 0);
  107. }
  108. if (adData.pos.top !== undefined) {
  109. self.animate({ top: adData.pos.top - offSet + 'px' }, 0);
  110. }
  111. })
  112. timeHandler = setTimeout(function () {
  113. $('.__ad_c__').each(function (_, item) {
  114. var self = $(item);
  115. var adData = self.data('data');
  116. if (adData.pos.bottom !== undefined) {
  117. self.animate({ bottom: adData.pos.bottom + 'px' }, 0);
  118. }
  119. if (adData.pos.top !== undefined) {
  120. self.animate({ top: adData.pos.top + 'px' }, 0);
  121. }
  122. })
  123. }, 20);
  124. });
  125. }
  126. setTimeout(function () {
  127. if (!$('meta[name="_uid"]').length) { // 未登录,不显示
  128. window.localStorage.removeItem('ads');
  129. return;
  130. }
  131. var pathName = window.location.pathname;
  132. if (exceptPages.indexOf(pathName) > -1) return; // 排除页,不显示
  133. initAd();
  134. initAdEvent();
  135. }, 0);
  136. })();