This article mainly introduces you to the js implementation of the floating window effect that can be clicked to shrink or expand. It has a certain reference value. Interested friends can refer to it. I hope it can help everyone.
Instructions: Click the "+" button to shrink/expand the floating window
Ideas
1. Define in html A p block, set an id; a button, used when clicked.
2. Write a js that contains shrink and expand functions; add a click event for the button.
3. If you want to make the floating window look better, you can set the corresponding parameters.
Steps
html
##
<p id="area"> <p id="small_menu"> <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> </p> <p id="on" onclick="xuanfu();"><p>+</p></p> </p>
##
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'; } }
Contains css, you can directly use
<!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> <p id="area"> <p id="small_menu"> <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> </p> <p id="on" onclick="xuanfu();"><p>+</p></p> </p> <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>
Related recommendations:
The above is the detailed content of js click to shrink or expand the floating window example sharing. For more information, please follow other related articles on the PHP Chinese website!