亳州市网站建设_网站建设公司_SSL证书_seo优化
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>禁用右键示例</title> <style> /* 提示框样式 - 默认完全隐藏 */ .right-click-tip { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: rgba(0, 0, 0, 0.8); color: white; padding: 15px 25px; border-radius: 8px; font-size: 16px; z-index: 9999; opacity: 0; /* 默认透明 */ visibility: hidden; /* 默认不可见(避免占位) */ transition: all 0.3s ease; /* 渐变过渡 */ pointer-events: none; } /* 提示框显示状态 */ .right-click-tip.show { opacity: 1; /* 显示 */ visibility: visible;/* 可见 */ } </style> </head> <body> <div class="right-click-tip" id="rightClickTip">为了浏览体验,右键已关闭</div> <script> const tipBox = document.getElementById('rightClickTip'); let tipTimer = null; // 定时器变量,避免重复触发 // 显示提示框并定时隐藏 function showTip() { // 清除之前的定时器(避免多次触发时计时混乱) clearTimeout(tipTimer); // 显示提示框 tipBox.classList.add('show'); // 2秒后隐藏 tipTimer = setTimeout(() => { tipBox.classList.remove('show'); }, 2000); } // 1. 禁用右键菜单 document.addEventListener('contextmenu', function(e) { e.preventDefault(); showTip(); // 仅右键时触发提示 }); // 2. 禁用F12/Ctrl+Shift+I document.addEventListener('keydown', function(e) { if (e.key === 'F12' || (e.ctrlKey && e.shiftKey && e.key === 'I')) { e.preventDefault(); showTip(); // 仅按这些键时触发提示 } }); </script> </body> </html>