這一題其實包含兩個獨立但常被一起用到的技巧:自製右鍵選單,以及讓彈出視窗可以被拖動。兩者的關鍵都在事件處理,尤其是「阻止預設行為」與「阻止事件冒泡」這兩個觀念。
要攔下使用者的右鍵,監聽的是 document.oncontextmenu;要在選單出現的位置擺放,用 e.clientX/e.clientY 取得滑鼠座標;要阻止瀏覽器跳出自己的預設選單,在事件處理函數的最後 return false。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>右鍵選單</title>
<style type="text/css">
#menu {
position: fixed;
left: 0; top: 0;
width: 200px; height: 400px;
background-color: blue;
display: none;
}
</style>
</head>
<body>
<div id="menu"></div>
<script type="text/javascript">
var menu = document.getElementById("menu");
document.oncontextmenu = function (e) {
var e = e || window.event;
var oX = e.clientX; // 滑鼠點的座標
var oY = e.clientY;
menu.style.display = "block";
menu.style.left = oX + "px";
menu.style.top = oY + "px";
return false; // 少了這行,瀏覽器預設的右鍵選單還是會跳出來
}
document.onclick = function (e) {
menu.style.display = "none"; // 點任何地方就收起來
}
menu.onclick = function (e) {
var e = e || window.event;
e.cancelBubble = true; // 點選單本身不要觸發上面那個收起動作
}
</script>
</body>
</html>
最後那個 cancelBubble = true 是整段最容易漏的一行。少了它,你點選單裡的項目時事件會冒泡到 document,選單在你看見結果之前就先關掉了。除了 return false,也可以用 addEventListener('contextmenu', fn, false) 搭配 e.preventDefault(),兩種寫法效果相同,註解掉的區塊就是後者。
拖曳的標準三步是 mousedown → mousemove → mouseup:按下時記錄滑鼠與元素的起始距離,移動時依差值更新位置,放開時解除 mousemove 監聽。
動手前先釐清四個容易混淆的東西:
document.documentElement.clientWidth 指的是整個 html 文件的可視寬度,document.body.clientWidth 是 body 的寬度,兩者不一定相同。可以在 console 用 console.log(document.documentElement) 與 console.log(document.body) 實際比對。
getBoundingClientRect().left 取得的是元素相對於「視窗」的位置,offsetLeft 取得的是相對於「父元素」的位置。拖曳計算要用前者。
e.clientX 是滑鼠相對於視窗的座標。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>彈窗</title>
<style type="text/css">
#mask {
position: fixed;
left: 0; top: 0;
width: 100%; height: 100%;
background-color: hsla(250,100%,50%,0.6);
display: none;
}
#popBox {
position: absolute;
background-color: #fff;
width: 200px; height: 200px;
}
</style>
</head>
<body>
<button id="clickBtn">點擊</button>
<div id="mask">
<div id="popBox"></div>
</div>
<script type="text/javascript">
var clickBtn = document.getElementById("clickBtn");
var popBox = document.getElementById("popBox");
var mask = document.getElementById("mask");
clickBtn.onclick = function () {
mask.style.display = "block";
// 水平垂直居中
popBox.style.left = (document.documentElement.clientWidth - popBox.offsetWidth) / 2 + "px";
popBox.style.top = (document.documentElement.clientHeight - popBox.offsetHeight) / 2 + "px";
}
popBox.onclick = function (e) {
var e = e || window.event;
e.cancelBubble = true; // 阻止冒泡,點視窗內容不該關掉遮罩
}
mask.onclick = function () {
mask.style.display = "none";
}
// 拖曳:mousedown -> mousemove -> mouseup
popBox.onmousedown = function (e) {
var e = e || window.event;
var pos = popBox.getBoundingClientRect();
var oX = e.clientX - pos.left; // 滑鼠點與元素左上角差了多遠
var oY = e.clientY - pos.top;
document.onmousemove = function (e) {
var e = e || window.event;
var oLeft = e.clientX - oX;
var oTop = e.clientY - oY;
popBox.style.left = oLeft + "px";
popBox.style.top = oTop + "px";
// 邊界約束:不讓視窗被拖出畫面
if (oLeft < 0) { popBox.style.left = 0 + "px"; }
if (oLeft > document.documentElement.clientWidth - popBox.offsetWidth) {
popBox.style.left = document.documentElement.clientWidth - popBox.offsetWidth + "px";
}
if (oTop < 0) { popBox.style.top = 0 + "px"; }
if (oTop > document.documentElement.clientHeight - popBox.offsetHeight) {
popBox.style.top = document.documentElement.clientHeight - popBox.offsetHeight + "px";
}
}
popBox.onmouseup = function () {
document.onmousemove = null; // 一定要解除,否則放開後仍會跟著滑鼠跑
}
}
</script>
</body>
</html>
兩個實作細節值得補充。一是 onmousemove 掛在 document 而不是 popBox 上——滑鼠移動速度快時會瞬間移出元素範圍,掛在元素上就會「拖到一半卡住」。二是 oX/oY 這個偏移量的意義:它記住你按下滑鼠的位置距離元素左上角多遠,之後用當前座標減掉它,元素才不會在按下的瞬間「跳」一下把左上角對齊滑鼠。
以上就是這篇的全部內容,希望對你有幫助。