案例1:自定义右击菜单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } #menu { width: 200px; height: 217px; background: tomato; /* 添加定位 */ position: absolute; /* 默认隐藏 */ display: none; } li { list-style: none; width: 200px; height: 30px; border-bottom: 1px solid white; } li:hover { background: skyblue; } body { height: 2000px; } </style> </head> <body> <div id="menu"> <ul> <li>HTML</li> <li>CSS</li> <li>JavaScript</li> <li>Node.js</li> <li>Vue</li> <li>React</li> <li>小程序</li> </ul> </div> <script> var menu = document.getElementById(‘menu‘); // 右击事件,取消默认事件 document.oncontextmenu = function (e) { // 获取鼠标的位置, 赋值给菜单的定位 e = e || window.event; // console.log(e.clientX, e.clientY); // 相对于可视区 // console.log(e.pageX, e.pageY); // 相对于页面 menu.style.left = e.pageX + ‘px‘; menu.style.top = e.pageY + ‘px‘; // 让自定义菜单显示 menu.style.display = ‘block‘; // 取消默认事件(默认的右击菜单不显示) return false; } // 点击页面,隐藏自定义菜单 document.onclick = function () { menu.style.display = ‘none‘; } </script> </body> </html>
案例2:键盘控制元素移动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div { width: 100px; height: 100px; background: tomato; position: absolute; top: 300px; left: 300px; } </style> </head> <body> <div id="box"></div> <script> var box = document.getElementById(‘box‘); // 获取box的位置 var l = parseInt(getStyle(box, ‘left‘)); var t = parseInt(getStyle(box, ‘top‘)); // console.log(l, t); // 监听键盘的按下 document.onkeydown = function (e) { e = e || window.event; // console.log(e.keyCode); switch (e.keyCode) { case 37: l--; break; case 38: t--; break; case 39: l++; break; case 40: t++; break; } // 赋值 box.style.left = l + ‘px‘; box.style.top = t + ‘px‘; } // 获取经过浏览器渲染的样式 (行内样式 内部样式 外部样式) function getStyle(ele, attr) { return window.getComputedStyle ? window.getComputedStyle(ele)[attr] : ele.currentStyle[attr]; } </script> </body> </html>
案例3:根据滚轮控制元素大小
<body> <div class="box"></div>
<script> var box = document.querySelector(‘.box‘); var h = 300; // box的高度 // Chrome box.onmousewheel = wheel; // firefox box.addEventListener(‘DOMMouseScroll‘, wheel); function wheel(e) { var direction; // 表示方向 1表示向下 -1表示向上 e = e || window.event; if (e.wheelDelta) { // chrome if (e.wheelDelta > 0) { // console.log(‘chrome--up‘); direction = -1; } else { // console.log(‘chrome--down‘); direction = 1; } } else { // firefox if (e.detail > 0) { // console.log(‘firefox--down‘); direction = 1; } else { // console.log(‘firefox--up‘); direction = -1; } } // 根据方向修改box的大小 h += direction; box.style.height = h + ‘px‘; } </script> </body>
案例4:拖拽元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div { width: 200px; height: 200px; background: tomato; position: absolute; top: 100px; left: 100px; } </style> </head> <body> <div class="box">box</div> <div class="box1">box1</div> <script> var box = document.querySelector(‘.box‘); var box1 = document.querySelector(‘.box1‘); drag(box); drag(box1); function drag(ele) { // 鼠标在box上按下 ele.onmousedown = function (e) { e = e || window.event; // 按下瞬间,计算误差 var deltaX = e.clientX - ele.offsetLeft; var deltaY = e.clientY - ele.offsetTop; // console.log(deltaX, deltaY); // 鼠标在页面上移动 document.onmousemove = function (e) { e = e || window.event; // console.log(e.clientX, e.clientY); // 计算当前的位置 var l = e.clientX - deltaX; var t = e.clientY - deltaY; // 验收,限制范围 if (l <= 0) l = 0; if (t <= 0) t = 0; // 获取屏幕的宽高 var screenW = document.documentElement.clientWidth; var screenH = document.documentElement.clientHeight; if (l >= screenW - ele.offsetWidth) l = screenW - ele.offsetWidth; if (t >= screenH - ele.offsetHeight) t = screenH - ele.offsetHeight; // 赋值 ele.style.left = l + ‘px‘; ele.style.top = t + ‘px‘; } } // 鼠标抬起, 取消移动事件 document.onmouseup = function () { document.onmousemove = null; } } </script> </body> </html>
案例5:碰撞检测
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box { width: 200px; height: 200px; background: orange; position: absolute; top: 200px; left: 200px; } .move { width: 100px; height: 100px; background: orange; position: absolute; top: 0; left: 0; } </style> </head> <body> <div class="box">box</div> <div class="move">move</div> <script> var box = document.querySelector(‘.box‘); var move = document.querySelector(‘.move‘); // 鼠标在box上按下 move.onmousedown = function (e) { e = e || window.event; // 按下瞬间,计算误差 var deltaX = e.clientX - move.offsetLeft; var deltaY = e.clientY - move.offsetTop; // 鼠标在页面上移动 document.onmousemove = function (e) { e = e || window.event; // 计算当前的位置 var l = e.clientX - deltaX; var t = e.clientY - deltaY; // 验收,限制范围 if (l <= 0) l = 0; if (t <= 0) t = 0; // 获取屏幕的宽高 var screenW = document.documentElement.clientWidth; var screenH = document.documentElement.clientHeight; if (l >= screenW - move.offsetWidth) l = screenW - move.offsetWidth; if (t >= screenH - move.offsetHeight) t = screenH - move.offsetHeight; // 赋值,移动 move.style.left = l + ‘px‘; move.style.top = t + ‘px‘; // 移动的过程中进行判断碰撞 if (move.offsetLeft + move.offsetWidth >= box.offsetLeft && move.offsetTop + move.offsetHeight >= box.offsetTop && box.offsetLeft + box.offsetWidth >= move.offsetLeft && box.offsetTop + box.offsetHeight >= move.offsetTop) { move.style.background = ‘blue‘; } else { move.style.background = ‘orange‘; } } return false; } // 鼠标抬起, 取消移动事件 document.onmouseup = function () { document.onmousemove = null; } </script> </body> </html>
案例6:改变元素多个任意属性(透明度 左/右等属性)
案例7:运动函数添加回调函数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box1 { width: 100px; height: 100px; position: absolute; top: 0; left: 0; background: blue; } </style> </head> <body> <div class="box1"></div> <script src="../ujiuye.js"></script> <script> var box1 = document.querySelector(‘.box1‘); box1.onclick = function () { bufferMove(box1, { ‘left‘: 500, ‘top‘: 1 }, function () { box1.style.background = ‘pink‘; }); } </script> </body> </html>
原文:https://www.cnblogs.com/chenhaiyun/p/14594418.html