2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > html自动广告业代码 html+javascript实现广告窗自由浮动

html自动广告业代码 html+javascript实现广告窗自由浮动

时间:2019-10-20 06:06:36

相关推荐

html自动广告业代码 html+javascript实现广告窗自由浮动

首先介绍一种设置div初始位置的定位方法:

.html代码如下:

层的移动

#ad{width:120px;height:120px;background:url(images/default.gif);position:relative;}

var obj = null;

function init(){

obj = document.getElementById("ad");

obj.style.left="10px";//设置初始位置

obj.style.top="10px";

alert(obj.style.posLeft);//获得方式一

alert(parseInt(obj.style.left));//获得方式二

}

function move(){

}

实现广告窗口在可视范围内浮动代码如下:

层的移动

#ad{width:120px;height:120px;background:url(images/default.gif);position:relative;}

var id;

var directionX=true,directionY=true;

var x=10,y=10;

var obj = null;

function init(){

obj = document.getElementById("ad");//获得对象

obj.style.posLeft = x;//对象属性设置

obj.style.posTop = y;

id = setInterval(move,100);//100ms重复执行move方法

}

function move(){

var width=document.documentElement.clientWidth-obj.offsetWidth;

var height= document.documentElement.clientHeight-obj.offsetHeight;

if(x>=width ){//说明移动到最右边

directionX = false;

}

if(x==0){//移动到最左边

directionX = true;

}

if(y >= height){

directionY = false;

}

if(y==0){

directionY = true;

}

if(directionX)

x += 10;

else

x -= 10;

if(directionY)

y += 10;

else

y -= 10;

obj.style.posLeft = x;

obj.style.posTop = y;

}

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