基于JavaScript實(shí)現(xiàn)右鍵菜單和拖拽功能
下面先給大家介紹下js實(shí)現(xiàn)的右鍵菜單功能,具體詳情如下所示:
這一章解決的問(wèn)題
1、實(shí)現(xiàn)右鍵菜單功能代碼。
2、阻止默認(rèn)事件的實(shí)際應(yīng)用。
<!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;
//鼠標(biāo)點(diǎn)的坐標(biāo)
var oX = e.clientX;
var oY = e.clientY;
//菜單出現(xiàn)后的位置
menu.style.display = "block";
menu.style.left = oX + "px";
menu.style.top = oY + "px";
//阻止瀏覽器默認(rèn)事件
return false;//一般點(diǎn)擊右鍵會(huì)出現(xiàn)瀏覽器默認(rèn)的右鍵菜單,寫(xiě)了這句代碼就可以阻止該默認(rèn)事件。
}
document.onclick = function(e) {
var e = e || window.event;
menu.style.display = "none"
}
menu.onclick = function(e) {
var e = e || window.event;
e.cancelBubble = true;
}
// document.addEventListener("contextmenu",function(e){
// var e = e || window.event;
// e.preventDefault();
// alert("menu");
// },false)
</script>
</body>
</html>
好了,以上代碼詳情是js實(shí)現(xiàn)的右鍵菜單功能。下面接著給大家介紹下js 拖曳功能的代碼解析
這一章解決的問(wèn)題
1、怎樣在網(wǎng)頁(yè)中實(shí)現(xiàn)拖曳功能。
2、document.documentElement與document.body的區(qū)別。
document.documentElement.clientWidth指整個(gè)html文檔的寬度,document.body.clientWidth的寬度。這兩者是不一樣的??梢栽赾onsole控制臺(tái)通過(guò)console.log(document.documentElement)和console.log(document.body)進(jìn)行測(cè)試。
3、getBoundingClientRect().left與offsetLeft的區(qū)別。
getBoundingClientRect()用于獲取元素的left、top、right、bottom。offset獲取相對(duì)于父級(jí)的left和top。getBoundingClientRect()獲取相對(duì)于窗口的left、top、right、bottom。
4、e.clientX指的是鼠標(biāo)點(diǎn)相對(duì)于窗口的坐標(biāo)。
<!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;
/*left:50%;
top:50%;*/
/*margin-top: -100px;
margin-left: -100px;*/
}
</style>
</head>
<body>
<button id="clickBtn">點(diǎn)擊</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指所有代碼的集合,通過(guò)它可以獲取事件的各種屬性。
e.cancelBubble = true;//阻止事件冒泡,即點(diǎn)擊事件不會(huì)傳遞到mask這一層,意味著不會(huì)觸發(fā)點(diǎn)擊mask事件代碼。
}
mask.onclick = function() {
mask.style.display = "none";
}
//拖拽 mousedown->mousemove->mouseup
popBox.onmousedown = function(e) {
var e = e || window.event;
var pos = popBox.getBoundingClientRect();//getBoundingClientRect()用于獲取元素的left、top、right、bottom。offset獲取相對(duì)于父級(jí)的left和top。getBoundingClientRect()獲取相對(duì)于窗口的left、top、right、bottom。
var oX = e.clientX - pos.left;//clientX指相對(duì)于窗口的坐標(biāo)。
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";//document.documentElement.clientWidth指整個(gè)html文檔的寬度,document.body.clientWidth的寬度。這兩者是不一樣的??梢栽赾onsole控制臺(tái)通過(guò)console.log(document.documentElement)和console.log(document.body)進(jìn)行測(cè)試。
}
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>
以上所述是小編給大家介紹的基于JavaScript實(shí)現(xiàn)右鍵菜單和拖拽功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- JS實(shí)現(xiàn)拖拽的方法分析
- JS基于面向?qū)ο髮?shí)現(xiàn)的拖拽功能示例
- 利用JavaScript實(shí)現(xiàn)拖拽改變?cè)卮笮?/a>
- 淺析JavaScript動(dòng)畫(huà)模擬拖拽原理
- 原生js實(shí)現(xiàn)彈出層登錄拖拽功能
- js實(shí)現(xiàn)小窗口拖拽效果
- js拖拽功能實(shí)現(xiàn)代碼解析
- Sortable.js拖拽排序使用方法解析
- js實(shí)現(xiàn)拖拽效果
- js完美的div拖拽實(shí)例代碼
- js 表格排序(編輯+拖拽+縮放)
- 鼠標(biāo)拖拽移動(dòng)子窗體的JS實(shí)現(xiàn)
- js實(shí)現(xiàn)的簡(jiǎn)練高效拖拽功能示例
相關(guān)文章
JS數(shù)組索引檢測(cè)中的數(shù)據(jù)類型問(wèn)題詳解
這篇文章主要給大家介紹了關(guān)于JS數(shù)組索引檢測(cè)中的數(shù)據(jù)類型問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
微信小程序使用radio顯示單選項(xiàng)功能【附源碼下載】
這篇文章主要介紹了微信小程序使用radio顯示單選項(xiàng)功能,涉及針對(duì)radio組件事件響應(yīng)相關(guān)操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2017-12-12
JavaScript 快捷鍵設(shè)置實(shí)現(xiàn)代碼
屏蔽Alt+F4等快捷鍵 IE Javascript快捷鍵操作2009-03-03
JavaScript性能優(yōu)化之小知識(shí)總結(jié)
JavaScript的性能問(wèn)題不容小覷,這就需要我們開(kāi)發(fā)人員在編寫(xiě)JavaScript程序時(shí)多注意一些細(xì)節(jié),本文給大家介紹javascript性能優(yōu)化之小知識(shí)總結(jié),需要的朋友可以參考下2015-11-11
JavaScript 跨域之POST實(shí)現(xiàn)方法
本篇文章主要介紹了JavaScript 跨域之POST實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
解決layui上傳文件提示上傳異常,實(shí)際文件已經(jīng)上傳成功的問(wèn)題
今天小編就為大家分享一篇解決layui上傳文件提示上傳異常,實(shí)際文件已經(jīng)上傳成功的問(wèn)題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
用javascript實(shí)現(xiàn)倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了用javascript實(shí)現(xiàn)倒計(jì)時(shí)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02
關(guān)于javascript獲取內(nèi)聯(lián)樣式與嵌入式樣式的實(shí)例
這篇文章主要介紹了關(guān)于javascript獲取內(nèi)聯(lián)樣式與嵌入式樣式的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
全國(guó)省市二級(jí)聯(lián)動(dòng)下拉菜單 js版
這篇文章主要為大家詳細(xì)介紹了基于javascript實(shí)現(xiàn)全國(guó)省市二級(jí)聯(lián)動(dòng)下拉菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05

