100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > html图片怎么设置悬浮效果 图片漂浮效果js实现

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

时间:2023-04-21 05:35:32

相关推荐

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

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

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();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。