• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

JavaScript实现的漂浮广告效果代码

JavaScript 水墨上仙 2693次浏览

当鼠标在图片上方时,图片停止漂浮,点击关闭按钮可隐藏图片,代码简单,直接插入网页更换成自己的漂浮图片就可以使用。

<body>
<div id="float" style=" position:absolute; z-index:3">
<img id="img1" src="http://www.75271.com/images/logo.png" style=" border:1px #ccc solid" /><img id="img2" onClick="float.style.display='none'" src="关闭图片" style=" position:absolute; margin-left:-13px;" />
</div>
<script type="text/javascript">
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.onmouseover=function(){clearTimeout(t);}
	float.onmouseout=function(){t=setTimeout("move()",speed);}
window.onload=move();
</script>
</body>


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明JavaScript实现的漂浮广告效果代码
喜欢 (0)
加载中……