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

本文实例为大家分享了js实现悬浮窗的具体代码,供大家参考,具体内容如下

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

思路

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

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

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

步骤

html

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;

}

//嵌套在页面中,文档初始化时加载。

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';

}

}

小结到此。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值