JavaScript----鼠标移入移出事件案例
2021/10/12 20:16:09
本文主要是介绍JavaScript----鼠标移入移出事件案例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<!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>鼠标移入移出事件</title> <style> * { margin: 0; padding: 0; } div { margin: 100px; width: 300px; height: 300px; background-color: rgb(70, 124, 150); color: white; font-size: 14px; position: relative; } p { background-color: rgb(238, 161, 130); width: 100px; height: 30px; line-height: 30px; text-align: center; position: absolute; /* 让光标事件不起作用,解决光标闪烁问题 */ pointer-events: none; } </style> </head> <body> <div> <p>我是提示信息</p> </div> <script> //获取div的dom节点 var divEle = document.querySelector('div') var pEle = document.querySelector('p') //获取元素p的尺寸 divEle.onmousemove = function (e) { //获取事件对象 e = e || window.event //获取光标位置,并把p元素的光标点设置成中心 var x = e.offsetX - pEle.offsetWidth / 2 var y = e.offsetY - pEle.offsetHeight / 2 //边界判断 //左边界 if (x < 0) { x = 0 } //右边界 if (x > divEle.offsetWidth - pEle.offsetWidth) { x = divEle.offsetWidth - pEle.offsetWidth } // 上边界 if (y < 0) { y = 0 } // 下边界 if (y > divEle.offsetHeight - pEle.offsetHeight) { y = divEle.offsetHeight - pEle.offsetHeight } pEle.style.top = y + 'px' pEle.style.left = x + 'px' } //鼠标移入出现 divEle.onmouseover = function() { pEle.style.display = 'block' } //鼠标移出隐藏 divEle.onmouseout = function() { pEle.style.display = 'none' } </script> </body> </html>
这篇关于JavaScript----鼠标移入移出事件案例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南