返回列表 发布新帖

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

1342 0
ZO论坛 发表于 6 小时前 | 查看全部 阅读模式 <

马上注册,结交更多好友,享用更多功能,让你轻松玩转小K网。

您需要 登录 才可以下载或查看,没有账号?立即注册 微信登录

×
利用AI写了一个镶嵌在自己网站的公告代码
  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. /* 基础样式重置 */
  19. .announcement-modal * {
  20.   margin: 0;
  21.   padding: 0;
  22.   box-sizing: border-box;
  23.   font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  24. }
  25. /* 弹窗遮罩层 */
  26. .announcement-modal {
  27.   position: fixed;
  28.   top: 0;
  29.   left: 0;
  30.   width: 100%;
  31.   height: 100%;
  32.   background-color: rgba(0, 0, 0, 0.5);
  33.   display: flex;
  34.   justify-content: center;
  35.   align-items: center;
  36.   z-index: 9999; /* 确保弹窗在最上层 */
  37.   opacity: 0;
  38.   visibility: hidden;
  39.   transition: opacity 0.3s ease, visibility 0.3s ease;
  40. }
  41. /* 弹窗显示状态 */
  42. .announcement-modal.show {
  43.   opacity: 1;
  44.   visibility: visible;
  45. }
  46. /* 弹窗内容容器 */
  47. .announcement-content {
  48.   background-color: #ffffff;
  49.   width: 90%;
  50.   max-width: 500px;
  51.   border-radius: 12px;
  52.   box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  53.   overflow: hidden;
  54.   position: relative;
  55.   transform: translateY(-20px);
  56.   transition: transform 0.3s ease;
  57. }
  58. .announcement-modal.show .announcement-content {
  59.   transform: translateY(0);
  60. }
  61. /* 关闭按钮 */
  62. .close-btn {
  63.   position: absolute;
  64.   top: 15px;
  65.   right: 15px;
  66.   background: none;
  67.   border: none;
  68.   font-size: 24px;
  69.   color: #666666;
  70.   cursor: pointer;
  71.   width: 30px;
  72.   height: 30px;
  73.   display: flex;
  74.   justify-content: center;
  75.   align-items: center;
  76.   border-radius: 50%;
  77.   transition: background-color 0.2s ease;
  78.   z-index: 10;
  79. }
  80. .close-btn:hover {
  81.   background-color: #f5f5f5;
  82.   color: #333333;
  83. }
  84. /* 公告头部 */
  85. .announcement-header {
  86.   background-color: #4096ff;
  87.   color: white;
  88.   padding: 20px 25px;
  89. }
  90. .announcement-header h3 {
  91.   font-size: 18px;
  92.   font-weight: 600;
  93. }
  94. /* 公告内容 */
  95. .announcement-body {
  96.   padding: 25px;
  97.   color: #333333;
  98.   line-height: 1.6;
  99. }
  100. .announcement-body p {
  101.   font-size: 14px;
  102.   margin-bottom: 0;
  103. }
  104. /* 公告底部 */
  105. .announcement-footer {
  106.   padding: 15px 25px;
  107.   background-color: #f8f9fa;
  108.   text-align: right;
  109. }
  110. /* 确认按钮 */
  111. .confirm-btn {
  112.   background-color: #4096ff;
  113.   color: white;
  114.   border: none;
  115.   padding: 8px 20px;
  116.   border-radius: 6px;
  117.   cursor: pointer;
  118.   font-size: 14px;
  119.   transition: background-color 0.2s ease;
  120. }
  121. .confirm-btn:hover {
  122.   background-color: #337ecc;
  123. }
  124. /* 响应式适配 */
  125. @media (max-width: 480px) {
  126.   .announcement-content {
  127.     width: 95%;
  128.   }
  129.   
  130.   .announcement-header {
  131.     padding: 15px 20px;
  132.   }
  133.   
  134.   .announcement-body {
  135.     padding: 20px;
  136.   }
  137.   
  138.   .announcement-footer {
  139.     padding: 15px 20px;
  140.   }
  141. }
  142. </style>
  143. <script>
  144. // 公告弹窗核心逻辑
  145. document.addEventListener('DOMContentLoaded', function() {
  146.   const modal = document.getElementById('announcement-modal');
  147.   const closeBtn = document.getElementById('close-announcement');
  148.   const confirmBtn = document.querySelector('.confirm-btn');
  149.   
  150.   // 从localStorage获取关闭时间
  151.   const closeTime = localStorage.getItem('announcementClosedTime');
  152.   const now = new Date().getTime();
  153.   const thirtyMinutes = 30 * 60 * 1000; // 30分钟的毫秒数
  154.   
  155.   // 判断是否需要显示弹窗:没有关闭记录 或 关闭时间已超过30分钟
  156.   if (!closeTime || (now - closeTime) > thirtyMinutes) {
  157.     modal.classList.add('show');
  158.   }
  159.   
  160.   // 关闭弹窗的通用函数
  161.   function closeModal() {
  162.     modal.classList.remove('show');
  163.     // 记录关闭时间到localStorage
  164.     localStorage.setItem('announcementClosedTime', new Date().getTime().toString());
  165.   }
  166.   
  167.   // 绑定关闭按钮事件
  168.   closeBtn.addEventListener('click', closeModal);
  169.   confirmBtn.addEventListener('click', closeModal);
  170.   
  171.   // 可选:点击遮罩层关闭弹窗
  172.   modal.addEventListener('click', function(e) {
  173.     if (e.target === modal) {
  174.       closeModal();
  175.     }
  176.   });
  177. });
  178. </script>
复制代码

回复

您需要登录后才可以回帖 登录 | 立即注册 微信登录

本版积分规则

您需要 登录 后才可以回复,轻松玩转社区,没有帐号?立即注册
快速回复
关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表