原生javascript实现仿QQ延时菜单
一、实现原理
定时器和排他思想
二、代码
<!DOCTYPE html> <html> <head> <title></title> <style type="text/css"> *{margin: 0;padding: 0} .main{ width: 100px; height: 300px; background: #eee; float: left; margin-top: 10px; } .box{width: 100px; height: 100px; background:orange; float: left; margin-top: 10px; margin-left:10px;display: none;} </style> </head> <body> <div class="main"></div> <div class="box"></div> <script type="text/javascript"> window.onload = function(){ var main = document.querySelector('.main'); var box = document.querySelector('.box'); var timer = null; main.onmouseover = box.onmouseover = show; main.onmouseout = box.onmouseout = hide; function show(){ clearTimeout(timer) box.style.display = 'block'; } function hide(){ timer = setTimeout(function(){ box.style.display = 'none'; },500) } } </script> </body> </html>
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步