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

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

1ebf83563932ebdc1f1b2054a88fc765.png

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

思路

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

+

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

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

}

}

小结到此。

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

时间: 2017-09-17


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部