想要在网站上实现一个符合欧美规范的 Cookies 条款弹窗,让用户选择同意或拒绝,以达合规要求。

将以下代码直接添加到网站模板的末尾(</body>标签前)即可:

<div id="cookie-consent-banner" class="cookie-banner">
  <div class="cookie-content">
    <p>我们使用Cookies来提升您的浏览体验。By continuing to use our website, you agree to our <a href="/privacy-policy" target="_blank">Cookie Policy</a>.</p>
    <div class="cookie-buttons">
      <button id="cookie-accept" class="cookie-btn accept">同意</button>
      <button id="cookie-decline" class="cookie-btn decline">拒绝</button>
      <button id="cookie-close" class="cookie-btn close">×</button>
    </div>
  </div>
</div>

<style>
.cookie-banner {position: fixed;bottom: 0;left: 0;width: 100%;background-color: #2c3e50;color: #fff;padding: 15px 0;z-index: 9999;box-shadow: 0 -2px 10px rgba(0,0,0,0.1);transform: translateY(100%);transition: transform 0.3s ease;}
.cookie-banner.show {transform: translateY(0);}
.cookie-content {max-width: 1200px;margin: 0 auto;padding: 0 20px;display: flex;flex-wrap: wrap;justify-content: space-between;align-items: center;gap: 15px;}
.cookie-content p {margin: 0;font-size: 14px;line-height: 1.5;}
.cookie-content a {color: #3498db;text-decoration: underline;}
.cookie-buttons {display: flex;gap: 10px;}
.cookie-btn {padding: 8px 20px;border: none;border-radius: 4px;cursor: pointer;font-size: 14px;transition: background-color 0.2s;}
.cookie-btn.accept {background-color: #27ae60;color: #fff;}
.cookie-btn.accept:hover {background-color: #219653;}
.cookie-btn.decline {background-color: #e74c3c;color: #fff;}
.cookie-btn.decline:hover {background-color: #c0392b;}
.cookie-btn.close {padding: 8px 12px;background-color: #7f8c8d;color: #fff;font-size: 16px;line-height: 1;}
.cookie-btn.close:hover {background-color: #6c7a7b;}
@media (max-width: 768px) {
  .cookie-content {flex-direction: column;text-align: center;}
}
</style>

<script>
// Cookies 弹窗核心逻辑
document.addEventListener('DOMContentLoaded', function() {
  const banner = document.getElementById('cookie-consent-banner');
  const acceptBtn = document.getElementById('cookie-accept');
  const declineBtn = document.getElementById('cookie-decline');
  const closeBtn = document.getElementById('cookie-close'); 

  // 检查用户是否已选择过
  const cookieConsent = localStorage.getItem('cookieConsent');
  if (!cookieConsent) {
    // 未选择过,显示弹窗
    setTimeout(() => {
      banner.classList.add('show');
    }, 500);
  }

  // 同意按钮逻辑
  acceptBtn.addEventListener('click', function() {
    localStorage.setItem('cookieConsent', 'accepted');
    banner.classList.remove('show');
    // 这里可以添加同意后执行的代码(如加载统计脚本)
    console.log('用户同意了Cookies条款');
  });

  // 拒绝按钮逻辑
  declineBtn.addEventListener('click', function() {
    localStorage.setItem('cookieConsent', 'declined');
    banner.classList.remove('show');
    console.log('用户拒绝了Cookies条款');
  });

  // 关闭按钮逻辑(仅关闭弹窗,不记录同意/拒绝)
  closeBtn.addEventListener('click', function() {
    banner.classList.remove('show');
    console.log('用户关闭了Cookies弹窗');
    // 注意:关闭按钮未记录localStorage,刷新页面后弹窗会重新显示
    // 如果需要关闭后不再显示,可取消下面这行注释:
    // localStorage.setItem('cookieConsent', 'closed');
  });
});
</script>

使用注意事项:

  • 替换 <a href="/privacy-policy"> 中的链接为你网站实际的 Cookie 政策 / 隐私政策页面
  • 可根据网站风格修改 CSS 中的颜色、字体、间距等样式
  • 如果需要多语言,可将弹窗内的文字替换为英文(如 "We use cookies to enhance your browsing experience...")

换一种漂浮在左下角的样式:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>漂浮左下角 Cookie 弹窗</title>
    <style>
        /* 全局重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
        }

        /* Cookie 弹窗核心样式 */
        .cookie-consent-container {
            position: fixed;
            bottom: 24px;
            left: 24px;
            background: #ffffff;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
            padding: 20px;
            max-width: 420px;
            width: calc(100% - 48px);
            z-index: 999999;
            border: 1px solid #f0f0f0;
        }

        .cookie-consent-header {
            display: flex;
            align-items: center;
            margin-bottom: 12px;
        }

        .cookie-consent-icon {
            width: 24px;
            height: 24px;
            margin-right: 10px;
            color: #0066ff;
        }

        .cookie-consent-title {
            font-size: 16px;
            font-weight: 600;
            color: #1a1a1a;
        }

        .cookie-consent-desc {
            font-size: 14px;
            line-height: 1.6;
            color: #666666;
            margin-bottom: 16px;
        }

        .cookie-consent-desc a {
            color: #0066ff;
            text-decoration: none;
        }

        .cookie-consent-desc a:hover {
            text-decoration: underline;
        }

        .cookie-consent-actions {
            display: flex;
            justify-content: flex-end;
            gap: 12px;
        }

        .cookie-btn {
            padding: 8px 16px;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            border: none;
            transition: all 0.2s ease;
        }

        .cookie-btn-accept {
            background: #0066ff;
            color: #ffffff;
        }

        .cookie-btn-accept:hover {
            background: #0052cc;
        }

        .cookie-btn-settings {
            background: #f5f5f5;
            color: #1a1a1a;
        }

        .cookie-btn-settings:hover {
            background: #e8e8e8;
        }

        /* 响应式适配(移动端) */
        @media (max-width: 480px) {
            .cookie-consent-container {
                bottom: 16px;
                left: 16px;
                width: calc(100% - 32px);
                padding: 16px;
            }

            .cookie-consent-actions {
                flex-wrap: wrap;
                justify-content: center;
            }

            .cookie-btn {
                flex: 1;
                min-width: 120px;
            }
        }
    </style>
</head>
<body>
    <!-- Cookie 弹窗 -->
    <div id="cookieConsentModal" class="cookie-consent-container">
        <div class="cookie-consent-header">
            <svg class="cookie-consent-icon" viewBox="0 0 24 24" fill="currentColor">
                <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm-1-13h2v6h-2zm0 8h2v2h-2z"/>
            </svg>
            <h3 class="cookie-consent-title">Cookie 政策提示</h3>
        </div>
        <p class="cookie-consent-desc">
            我们使用 Cookie 来提升你的浏览体验,包括提供个性化内容、分析网站流量等。继续使用本网站,即表示你同意我们按照<a href="/privacy-policy" target="_blank">隐私政策</a>使用 Cookie。你可随时通过浏览器设置管理 Cookie 偏好。
        </p>
        <div class="cookie-consent-actions">
            <button id="cookieSettingsBtn" class="cookie-btn cookie-btn-settings">Cookie 设置</button>
            <button id="cookieAcceptBtn" class="cookie-btn cookie-btn-accept">接受</button>
        </div>
    </div>

    <script>
        // 核心交互逻辑
        document.addEventListener('DOMContentLoaded', function() {
            const modal = document.getElementById('cookieConsentModal');
            const acceptBtn = document.getElementById('cookieAcceptBtn');
            const settingsBtn = document.getElementById('cookieSettingsBtn');

            // 检查本地存储,避免重复弹窗
            const hasCookieConsent = localStorage.getItem('ztee_cookie_consent');
            if (hasCookieConsent) {
                modal.style.display = 'none';
            }

            // 接受 Cookie
            acceptBtn.addEventListener('click', function() {
                localStorage.setItem('ztee_cookie_consent', 'accepted');
                modal.style.display = 'none';
                // 可添加:加载分析脚本、营销脚本等逻辑
            });

            // Cookie 设置(可扩展为弹窗/新页面)
            settingsBtn.addEventListener('click', function() {
                // 此处可扩展为 Cookie 设置面板
                alert('Cookie 设置面板(可根据需求扩展)');
                // 示例:跳转到隐私政策页面
                // window.open('/privacy-policy', '_blank');
            });

            // 可选:点击页面其他区域关闭弹窗
            window.addEventListener('click', function(e) {
                if (e.target === modal) {
                    modal.style.display = 'none';
                }
            });
        });
    </script>
</body>
</html>