js实现可以点击收缩或张开的悬浮窗

js实现可以点击收缩或张开的悬浮窗

说明:点击”+“按钮,悬浮窗收缩/展开

思路

1、在html中定义一个div块,定一个id;一个按钮,点击时用。

2、写一个js,包含收缩以及展开的函数;为按钮添加点击事件。

3、想要让悬浮窗好看点,可设置对应的参数。

步骤

html

<div> <div> <ul> <li><a href="#">item one</a></li> <li><a href="#">item two</a></li> <li><a href="#">item three</a></li> <li><a href="#">item four</a></li> <li><a href="#">item five</a></li> </ul> </div> <div><p>+</p></div> </div>

js

var menubox = document.getElementById("area"); //area为菜单栏的id var cli_on = document.getElementById("on"); //on为按钮 var flag = false, timer = null, initime = null, r_len = 0; if(menubox.style.right=== 0){ flag = true; } else{ flag = false; } cli_on.onclick = function () { //为on按钮绑定click事件 clearTimeout(initime); //根据状态flag执开展开收缩 if (flag) { r_len = 0; timer = setInterval(slideright, 10); } else { r_len = -160; timer = setInterval(slideleft, 10); } } //展开 function slideright() { if (r_len <= -160) { clearInterval(timer); flag = !flag; return false; }else{ r_len -= 5; menubox.style.right = r_len + 'px'; } } //收缩 function slideleft() { if (r_len >= 0) { clearInterval(timer); flag = !flag; return false; } else { r_len += 5; menubox.style.right = r_len + 'px'; } }

完整代码

含css,可直接用

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>悬浮窗</title> </head> <style type="text/css"> #area{ position:fixed; width:160px; right:-160px; top:27%;} #small_menu ul { list-style: none; } #area #on{ position: absolute; top: 40%; right: 100%; width: 30px; height: 30px; cursor: pointer; border-radius: 15px; background-color: rgba(13, 143, 143, 0.2); } #area #on p{ font-size:30px; text-align:center; margin-top:-6px; color:#01E290; } #area #small_menu { width:100%; } #area #small_menu ul li { width:100%; height: 44px; text-align:left; background-color: rgba(2, 27, 38, 0.62); border-top: 1px solid #043B46; line-height: 44px; } #area #small_menu ul li a{ text-decoration: none; margin-left:30px; color: #bfbfbf; font-size:16px; font-family: 'Microsoft Yahei'; } #area #small_menu li.active { width: 156px; background-color: rgba(2, 27, 38, 0.87); border-left: 4px solid #00ffff; border-top: 0px; } #area #small_menu li.active a{ color: #00ffff; } #area #small_menu ul li:hover { width: 156px; background-color: rgba(2, 27, 38, 0.87); border-left: 4px solid #00ffff; } #area #small_menu ul li:hover a{ color: #00ffff; } </style> <body> <div> <div> <ul> <li><a href="#">item one</a></li> <li><a href="#">item two</a></li> <li><a href="#">item three</a></li> <li><a href="#">item four</a></li> <li><a href="#">item five</a></li> </ul> </div> <div><p>+</p></div> </div> <script> //嵌套在页面中,文档初始化时加载。 var menubox = document.getElementById("area"); //area为菜单栏的id var cli_on = document.getElementById("on"); //on为按钮 var flag = false, timer = null, initime = null, r_len = 0; if(menubox.style.right=== 0){ flag = true; } else{ flag = false; } cli_on.onclick = function () { //为on按钮绑定click事件 clearTimeout(initime); //根据状态flag执开展开收缩 if (flag) { r_len = 0; timer = setInterval(slideright, 10); } else { r_len = -160; timer = setInterval(slideleft, 10); } } //展开 function slideright() { if (r_len <= -160) { clearInterval(timer); flag = !flag; return false; }else{ r_len -= 5; menubox.style.right = r_len + 'px'; } } //收缩 function slideleft() { if (r_len >= 0) { clearInterval(timer); flag = !flag; return false; } else { r_len += 5; menubox.style.right = r_len + 'px'; } } </script> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyzsgd.html