想在網站上放一個符合法規要求的 Cookie 條款彈窗,讓訪客選擇同意或拒絕,把下面這段程式碼直接加到範本末端(</body> 標籤前)就可以:
<div id="cookie-consent-banner" class="cookie-banner">
<div class="cookie-content">
<p>我們使用 Cookies 來提升您的瀏覽體驗。繼續使用本網站,即表示您同意我們的 <a href="/privacy-policy" target="_blank">Cookie 政策</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>
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 提示視窗');
});
});
</script>使用注意事項:
- 把
<a href="/privacy-policy">裡的連結換成你網站實際的 Cookie 政策/隱私權政策頁面 - 可以依網站風格調整 CSS 裡的顏色、字型、間距等樣式
- 如果需要多語系,把彈窗內的文字換成英文即可(例如 "We use cookies to enhance your browsing experience...")
換一種漂浮在左下角的樣式:
<!DOCTYPE html>
<html lang="zh-TW">
<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-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>
<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';
}
acceptBtn.addEventListener('click', function() {
localStorage.setItem('ztee_cookie_consent', 'accepted');
modal.style.display = 'none';
});
settingsBtn.addEventListener('click', function() {
alert('Cookie 設定面板(可依需求擴充)');
});
window.addEventListener('click', function(e) {
if (e.target === modal) {
modal.style.display = 'none';
}
});
});
</script>
</body>
</html>
評論 暫無評論