DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js實現廣告漂浮效果的小例子
js實現廣告漂浮效果的小例子
編輯:關於JavaScript     

復制代碼 代碼如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
<style type="text/css">
div{
position:absolute;
}
</style>
</head>
<body>
<div id="floatdiv">
<img src="1.jpg" height="100px" width="200px">

</div>
</body>
</html>
<script language="javascript" type="text/javascript">
/*
利用window對象,實現浮動效果

 1、有一個div,就是我們要控制的,它的起始點坐標(0,0)
 2、設定橫向和縱向的速度
 3、控制div移動
  1)div是否到達邊界,設置圖片速度反向移動
*/
//獲取圖片所在的div對象

var img=document.getElementById("floatdiv");
//設置div起始點坐標
var x=0,y=0;
//設置div行進速度
var xSpeed=2,ySpeed=1;
//設置圖片移動
var w=document.body.clientWidth-200,h=document.body.clientHeight-100;
function floatdiv(){
 //比較圖片是否到達邊界,如查到達邊界 改變方向;如未到達邊界
 if(x>w||x<0) xSpeed= -xSpeed;
 if(y>h||y<0) ySpeed= -ySpeed;

 x+=xSpeed;
 y+=ySpeed;

 //設置坐標值,起始坐標+速度
 img.style.top=y+"px";
 img.style.left=x+"px";
setTimeout("floatdiv()",10);
}
floatdiv();
</script>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved