html图片怎么设置悬浮效果,图片漂浮效果js实现

当鼠标在图片上方时,图片停止漂浮,点击关闭按钮可隐藏图片。

%E9%80%89%E6%8B%A9%E4%BD%A0%E8%A6%81%E6%B5%AE%E5%8A%A8%E7%9A%84%E5%9B%BE%E7%89%87%E5%85%B3%E9%97%AD%E5%9B%BE%E7%89%87

var xPos=0;

var yPos=0;

var directionX=true;

var directionY=true;

var step=1;

var speed=1;

var float=document.getElementById("float");

function move(){

var height=document.documentElement.clientHeight;

var width=document.documentElement.clientWidth;

var imgHeight=float.offsetHeight;

var imgWidth=float.offsetWidth;

float.style.left=xPos+document.documentElement.scrollLeft+"px";

float.style.top=yPos+document.documentElement.scrollTop+"px";

if(directionX){xPos=xPos+step;}else{xPos=xPos-step;}

if(xPos<=0){xPos=0;directionX=true;}

if(xPos>=width-imgWidth){xPos=width-imgWidth;directionX=false;}

if(directionY){yPos=yPos+step;}else{yPos=yPos-step;}

if(yPos<=0){yPos=0;directionY=true;}

if(yPos>=height-imgHeight){yPos=height-imgHeight;directionY=false;}

t=setTimeout(move,speed);

}

float.οnmοuseοver=function(){clearTimeout(t);}

float.οnmοuseοut=function(){t=setTimeout("move()",speed);}

window.οnlοad=move();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值