小庚资源网 · 免费提供绿色软件、活动线报以及其他网络资源,好货不私藏!
当前位置:网站首页 > 网站源码 > 只有源码 >正文

利用AI写了一个镶嵌在自己网站的公告代码

作者:小编日期:2026-02-22浏览:5079分类:只有源码

image.png

  1. <!-- 公告弹窗 -->

  2. <div id="announcement-modal" class="announcement-modal">

  3.   <div class="announcement-content">

  4.     <button id="close-announcement" class="close-btn">&times;</button>

  5.     <div class="announcement-header">

  6.       <h3>重要公告</h3>

  7.     </div>

  8.     <div class="announcement-body">

  9.       <p>欢迎访问我们的网站!近期我们完成了系统升级,新增了多项实用功能,使用体验将更加流畅。如有任何问题,请联系客服。</p>

  10.       <!-- 你可以修改这里的公告内容 -->

  11.     </div>

  12.     <div class="announcement-footer">

  13.       <button class="confirm-btn">我知道了</button>

  14.     </div>

  15.   </div>

  16. </div>


  17. <style>

  18. .announcement-modal * {

  19.   margin: 0;

  20.   padding: 0;

  21.   box-sizing: border-box;

  22.   font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;

  23. }

  24. .announcement-modal {

  25.   position: fixed;

  26.   top: 0;

  27.   left: 0;

  28.   width: 100%;

  29.   height: 100%;

  30.   background-color: rgba(0, 0, 0, 0.5);

  31.   display: flex;

  32.   justify-content: center;

  33.   align-items: center;

  34.   z-index: 9999;

  35.   opacity: 0;

  36.   visibility: hidden;

  37.   transition: opacity 0.3s ease, visibility 0.3s ease;

  38. }

  39. .announcement-modal.show {

  40.   opacity: 1;

  41.   visibility: visible;

  42. }

  43. .announcement-content {

  44.   background-color: #ffffff;

  45.   width: 90%;

  46.   max-width: 500px;

  47.   border-radius: 12px;

  48.   box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);

  49.   overflow: hidden;

  50.   position: relative;

  51.   transform: translateY(-20px);

  52.   transition: transform 0.3s ease;

  53. }

  54. .announcement-modal.show .announcement-content {

  55.   transform: translateY(0);

  56. }

  57. .close-btn {

  58.   position: absolute;

  59.   top: 15px;

  60.   right: 15px;

  61.   background: none;

  62.   border: none;

  63.   font-size: 24px;

  64.   color: #666666;

  65.   cursor: pointer;

  66.   width: 30px;

  67.   height: 30px;

  68.   display: flex;

  69.   justify-content: center;

  70.   align-items: center;

  71.   border-radius: 50%;

  72.   transition: background-color 0.2s ease;

  73.   z-index: 10;

  74. }

  75. .close-btn:hover {

  76.   background-color: #f5f5f5;

  77.   color: #333333;

  78. }

  79. .announcement-header {

  80.   background-color: #4096ff;

  81.   color: white;

  82.   padding: 20px 25px;

  83. }

  84. .announcement-header h3 {

  85.   font-size: 18px;

  86.   font-weight: 600;

  87. }

  88. .announcement-body {

  89.   padding: 25px;

  90.   color: #333333;

  91.   line-height: 1.6;

  92. }

  93. .announcement-body p {

  94.   font-size: 14px;

  95.   margin-bottom: 0;

  96. }

  97. .announcement-footer {

  98.   padding: 15px 25px;

  99.   background-color: #f8f9fa;

  100.   text-align: right;

  101. }

  102. .confirm-btn {

  103.   background-color: #4096ff;

  104.   color: white;

  105.   border: none;

  106.   padding: 8px 20px;

  107.   border-radius: 6px;

  108.   cursor: pointer;

  109.   font-size: 14px;

  110.   transition: background-color 0.2s ease;

  111. }

  112. .confirm-btn:hover {

  113.   background-color: #337ecc;

  114. }

  115. @media (max-width: 480px) {

  116.   .announcement-content {

  117.     width: 95%;

  118.   }

  119.   

  120.   .announcement-header {

  121.     padding: 15px 20px;

  122.   }

  123.   

  124.   .announcement-body {

  125.     padding: 20px;

  126.   }

  127.   

  128.   .announcement-footer {

  129.     padding: 15px 20px;

  130.   }

  131. }

  132. </style>


  133. <script>

  134. // 公告弹窗核心逻辑

  135. document.addEventListener('DOMContentLoaded', function() {

  136.   const modal = document.getElementById('announcement-modal');

  137.   const closeBtn = document.getElementById('close-announcement');

  138.   const confirmBtn = document.querySelector('.confirm-btn');

  139.   

  140.   // 从localStorage获取关闭时间

  141.   const closeTime = localStorage.getItem('announcementClosedTime');

  142.   const now = new Date().getTime();

  143.   const thirtyMinutes = 30 * 60 * 1000; // 30分钟的毫秒数

  144.   

  145.   // 判断是否需要显示弹窗:没有关闭记录 或 关闭时间已超过30分钟

  146.   if (!closeTime || (now - closeTime) > thirtyMinutes) {

  147.     modal.classList.add('show');

  148.   }

  149.   

  150.   // 关闭弹窗的通用函数

  151.   function closeModal() {

  152.     modal.classList.remove('show');

  153.     // 记录关闭时间到localStorage

  154.     localStorage.setItem('announcementClosedTime', new Date().getTime().toString());

  155.   }

  156.   

  157.   // 绑定关闭按钮事件

  158.   closeBtn.addEventListener('click', closeModal);

  159.   confirmBtn.addEventListener('click', closeModal);

  160.   

  161.   // 可选:点击遮罩层关闭弹窗

  162.   modal.addEventListener('click', function(e) {

  163.     if (e.target === modal) {

  164.       closeModal();

  165.     }

  166.   });

  167. });

  168. </script>


暂无评论,来添加一个吧。

取消回复欢迎发表评论:

Copyright© XGW9.COM版权所有〖小庚资源网〗
〖恒创科技〗为本站提供专业云计算服务  
本站发布的内容来源于互联网,如果有侵权内容,请联系我们删除!E-mail:xgzyw6@outlook.com
关于我们|我要投稿|免责声明|XML地图