JS延时器提示框的应用实例代码解析
作者:Web学海无涯 时间:2024-05-10 14:00:28
废话不多说了,直接给大家贴代码了,具体代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>延时显示提示框</title>
<style>
#div1 {
float: left;
width: 60px;
height: 60px;
background-color: aqua;
}
#div2 {
position: relative;
float: left;
margin: 0 10px;
width: 200px;
height: 200px;
background-color: #cccccc;
display: none;
}
</style>
<script>
window.onload = function () {
var oDiv1 = document.getElementById('div1');
var oDiv2 = document.getElementById('div2');
var timer = null;
// oDiv1.onmouseover = function () {
// clearTimeout(timer);
// oDiv2.style.display = 'block'; //鼠标移入div1时显示div2
// };
//
// oDiv1.onmouseout = function () {
// timer = setTimeout(function () {
// oDiv2.style.display = 'none'; //鼠标移除div1时隐藏div2
// }, 500); //为了从div1移入div2,所以移出div1时div2隐藏应该有一个延时设置
// };
//
// oDiv2.onmouseover = function () {
// clearTimeout(timer); //清除延时器设置,当鼠标移入div2时,div2应当显示
// };
//
// oDiv2.onmouseout = function () {
// timer = setTimeout(function () {
// oDiv2.style.display = 'none'; //当鼠标移出div2时,div2应当隐藏
// }, 500); //当鼠标移出div2并移入div1时,div2会闪烁一下再显示,设置一个延时,清除闪烁的效果;
// // 但是设置延时器之后,鼠标移入div1时,div2却隐藏了,原因是setTimeout,
// // 应当清除延时器,在oDiv1.onmouseover事件中添加,清除延时器的代码
// };
// 由于这四个事件中代码两两对应地相同或相似,可以做如下简化处理:
oDiv1.onmouseover = oDiv2.onmouseover = function () {
clearTimeout(timer);
oDiv2.style.display = 'block';
/* 鼠标移入div1时显示div2,虽然oDiv2.onmouseover事件中没有写oDiv2.style.display = 'block';
但是实际上oDiv2是'block'状态,多写一句代码没有实质性影响*/
};
oDiv1.onmouseout = oDiv2.onmouseout = function () {
timer = setTimeout(function () {
oDiv2.style.display = 'none'; //鼠标移除div1时隐藏div2
}, 500); //为了从div1移入div2,所以移出div1时div2隐藏应该有一个延时设置
};
// 简化后的代码执行结果跟前面的代码效果完全一致.
}
</script>
</head>
<body>
<h2>延时提示框</h2>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
延时提示框编写时需要注意的点都记录在注释当中,逐个对照注意一下,这个功能看过视频教程之后实现起来比较顺畅,原因是在编写代码之前对所需功能做了一个列表按照列表一一实现,出现问题再进行相应的调整比上来直接写代码强多了。
标签:js,延时器,提示框
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Python程序中操作MySQL的基本方法
2024-01-20 18:30:46
![](https://img.aspxhome.com/file/2023/0/117960_0s.jpg)
python自动化办公操作PPT的实现
2023-06-14 03:43:47
![](https://img.aspxhome.com/file/2023/5/59835_0s.png)
python+pytest接口自动化之token关联登录的实现
2023-01-21 13:27:37
![](https://img.aspxhome.com/file/2023/7/82707_0s.png)
Pandas通过index选择并获取行和列
2023-06-02 04:30:21
QQ聊天窗口链接提示效果代码
2008-12-16 12:59:00
python 一篇文章搞懂装饰器所有用法(建议收藏)
2023-12-06 17:27:43
![](https://img.aspxhome.com/file/2023/5/114535_0s.jpg)
跟老齐学Python之赋值,简单也不简单
2021-03-09 22:44:14
Python实现身份证前六位地区码对照表文件
2022-12-25 10:55:16
![](https://img.aspxhome.com/file/2023/7/99657_0s.jpg)
Python通过2种方法输出带颜色字体
2023-02-08 20:53:22
![](https://img.aspxhome.com/file/2023/7/71507_0s.png)
Python面向对象编程之类的继承
2021-10-27 23:46:39
![](https://img.aspxhome.com/file/2023/6/83896_0s.png)
用ASP生成XBM数字图片验证码
2008-08-10 18:40:00
使用python生成云词图实现画红楼梦词云图
2022-07-19 00:21:56
![](https://img.aspxhome.com/file/2023/4/131374_0s.jpg)
FrontPage2002简明教程六:图片库
2008-09-17 11:30:00
Firefox与IE下UL预设标记的异同
2007-05-11 17:03:00
Python中第三方库Requests库的高级用法详解
2022-04-10 21:25:50
Python绘制散点图之可视化神器pyecharts
2022-03-28 18:46:02
![](https://img.aspxhome.com/file/2023/9/92189_0s.png)
Keras:Unet网络实现多类语义分割方式
2022-02-26 16:36:49
![](https://img.aspxhome.com/file/2023/1/134751_0s.jpg)
TensorFlow安装并在Pycharm搭建环境的详细图文教程
2023-09-12 16:34:00
![](https://img.aspxhome.com/file/2023/4/127794_0s.jpg)
使用Python+selenium实现第一个自动化测试脚本
2021-01-26 17:52:01
![](https://img.aspxhome.com/file/2023/2/75902_0s.jpg)
pyhton列表转换为数组的实例
2021-01-12 08:14:31