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 2.9 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ; (function () {
  2. const adList = [{
  3. id: 1,
  4. pos: {
  5. left: 50,
  6. bottom: 50,
  7. },
  8. src: '/img/ad/ad01.png',
  9. url: '/user/invitation_tpl',
  10. width: 144,
  11. height: 108,
  12. }/*, {
  13. id: 2,
  14. pos: {
  15. right: 50,
  16. bottom: 50,
  17. },
  18. src: '/img/ad/ad01.png',
  19. url: '/user/invitation_tpl',
  20. width: 144,
  21. height: 108,
  22. }*/];
  23. function createAd(adList) {
  24. const adInfoStr = window.localStorage.getItem('ads') || '{}';
  25. let adInfoObj = JSON.parse(adInfoStr);
  26. const today = new Date();
  27. const timeEnd = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1).getTime();
  28. const now = Date.now();
  29. if (!adInfoObj.expires || adInfoObj.expires <= now) {
  30. adInfoObj = {
  31. expires: timeEnd,
  32. };
  33. }
  34. for (var i = 0, iLen = adList.length; i < iLen; i++) {
  35. var adI = adList[i];
  36. var showOr = adInfoObj[adI.id] === false ? false : true;
  37. adInfoObj[adI.id] = showOr;
  38. if (!showOr) continue;
  39. var adEl = $(`<div class="__ad_c__" _id="${adI.id}" style="position:fixed;z-index:99999999;
  40. width:${adI.width}px;height:${adI.height}px;
  41. left:${adI.pos.left !== undefined ? adI.pos.left + 'px' : ''};
  42. top:${adI.pos.top !== undefined ? adI.pos.top + 'px' : ''};
  43. right:${adI.pos.right !== undefined ? adI.pos.right + 'px' : ''};
  44. bottom:${adI.pos.bottom !== undefined ? adI.pos.bottom + 'px' : ''};">
  45. <a style="" href="${adI.url}" target="_blank">
  46. <img style="height:100%;width:100%;" src="${adI.src}" />
  47. </a>
  48. <div class="__ad_close_c__" style="position:absolute;top:6px;right:6px;">
  49. <i class="ri-close-circle-line __ad_close__" style="color:white;font-size:18px;cursor:pointer;"></i>
  50. </div>
  51. </div>`);
  52. $('body').append(adEl);
  53. }
  54. window.localStorage.setItem('ads', JSON.stringify(adInfoObj));
  55. }
  56. function initAdEvent() {
  57. $('body').on('click', '.__ad_c__ .__ad_close__', function () {
  58. var self = $(this);
  59. var adEl = self.closest('.__ad_c__');
  60. var adId = adEl.attr('_id');
  61. const adInfoStr = window.localStorage.getItem('ads') || '{}';
  62. const adInfoObj = JSON.parse(adInfoStr);
  63. adInfoObj[adId] = false;
  64. window.localStorage.setItem('ads', JSON.stringify(adInfoObj));
  65. adEl.remove();
  66. });
  67. var scrollTopOld = $(document).scrollTop();
  68. var timeHandler = null;
  69. $(window).scroll(function (e) {
  70. var scrollTop = $(document).scrollTop();
  71. var offSet = scrollTop - scrollTopOld;
  72. scrollTopOld = scrollTop;
  73. timeHandler && clearTimeout(timeHandler);
  74. $('.__ad_c__').animate({ bottom: 50 + offSet + 'px' }, 0);
  75. timeHandler = setTimeout(function () {
  76. $('.__ad_c__').animate({ bottom: 50 + 'px' }, 0);
  77. }, 20);
  78. });
  79. }
  80. setTimeout(function () {
  81. createAd(adList);
  82. initAdEvent();
  83. }, 0);
  84. })();