<div id="roll">
<div id="rollClose">X</div>
<div class="roll-content">
<p>xxxxxxxxxxxx</p>
<p>举报电话:xxxxxxxxxxxx</p>
<p>举报邮箱:xxxxxxxxxxxx</p>
<p>举报信箱:xxxxxxxxxxxx</p>
<p>举报地址:xxxxxxxxxxxx</p>
</div>
</div>
<style>
roll {
width: 400px;
padding: 20px;
position: fixed;
background: rgba(22, 124, 118, 1);
color: #FFFFFF;
border-radius: 6px;
z-index: 99999999;
}
.roll-content p {
margin: 0;
padding: 0;
font-size: 16px;
line-height: 30px;
}
.roll-content p:first-child {
/* margin-left: -12px; */
}
rollClose {
position: absolute;
cursor: pointer;
top: 6px;
right: 12px;
}
</style>
<script>
class floatingW{
constructor(roll, rollClose, speed = 20, x=0, y=0){
this.interval = null; // 定义保存setInterval的变量
this.roll = roll; // 定义保存飘窗对象
this.rollClose = rollClose; // 定义保存关闭按钮的对象
this.isClose = false; // 定义是否关闭的变量
this.speed = speed; // 定义飘窗的速度
this.statusX = 1; // 定义,left属性值变化的幅度
this.statusY = 1; // 定义,top属性值变化的幅度
this.x = x; // 定义初始状态下left属性的值
this.y = y; // 定义初始状态下top属性的值
// 定义飘窗可以移动的宽度
this.winW = document.documentElement.clientWidth - this.roll.offsetWidth;
// 定义飘窗可以移动的高度
this.winH = document.documentElement.clientHeight - this.roll.offsetHeight;
}
// 飘动
Move(){
this.interval = setInterval(this.__Go.bind(this), this.speed)
}
// 停止
stop(){
clearInterval(this.interval);
}
__Go(){
// 设置div的left属性值
this.roll.style.left = this.x + 'px';
// 设置div的top属性值
this.roll.style.top = this.y + 'px';
// 如果statusX=1则每次减少1px,否则加1px
this.x = this.x + (this.statusX ? -1 : 1)
//如果left属性值小于0,也就是div要超出左边界了,就将statusX设置为0
if (this.x < 0) { this.statusX = 0 }
//如果top属性值大于winW,也就是div要超出右边界了,就将statusX设置为1
if (this.x > this.winW) { this.statusX = 1 }
this.y = this.y + (this.statusY ? -1 : 1)
if (this.y < 0) { this.statusY = 0 }
if (this.y > this.winH) { this.statusY = 1 }
}
// 关闭
Close(){
this.isClose = true
this.roll.style.display = 'none'
this.stop()
}
}
let roll = document.getElementById("roll");
let rollClose = document.getElementById("rollClose");
let floatingWindow = new floatingW(roll, rollClose);
floatingWindow.Move();
roll.onmouseover = function(){floatingWindow.stop();}
roll.onmouseout = function(){
if(!floatingWindow.isClose)
floatingWindow.Move();
}
rollClose.onclick = function () { floatingWindow.Close() };
</script>