亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于JavaScript實(shí)現(xiàn)右鍵菜單和拖拽功能

 更新時(shí)間:2016年11月28日 09:40:38   投稿:mrr  
本文給大家分享基于js實(shí)現(xiàn)的右鍵菜單功能和拖拽功能的代碼解析,非常不錯(cuò),具有一定的參考借鑒價(jià)值,感興趣的朋友一起看看吧

下面先給大家介紹下js實(shí)現(xià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)的右鍵菜單,寫了這句代碼就可以阻止該默認(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 拖曳功能的代碼解析

這一章解決的問題

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的寬度。這兩者是不一樣的。可以在console控制臺(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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論