本文实例为大家分享了js实现悬浮窗的具体代码,供大家参考,具体内容如下
说明:点击”+“按钮,悬浮窗收缩/展开
思路
1、在html中定义一个div块,定一个id;一个按钮,点击时用。
2、写一个js,包含收缩以及展开的函数;为按钮添加点击事件。
3、想要让悬浮窗好看点,可设置对应的参数。
步骤
html
- item one
- item two
- item three
- item four
- item five
+
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,可直接用
悬浮窗 #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; }
- item one
- item two
- item three
- item four
- item five
+
小结到此。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



