標籤:style http io color ar os java div on
-----------------------------------------------------------------------------------------------------------------css
@charset "gb2312";
/* CSS Document */
body{
margin:0;
margin-top:3px;
padding:0;
font-size:12px;
line-height:20px;
color:#333;
}
.float{
position:absolute;
z-index:1;
}
.main{
width:95%;
margin-left:auto;
margin-right:auto;
}
.left_indent{
padding-left:20px;
}
.red{
color:#F00;
}
-----------------------------------------------------------------------------------------------------------------js
//定義全域變數
var moveX = 0; //X軸方向移動的距離
var moveY = 0; //Y軸方向移動的距離
var step = 1; //圖片移動的速度
var directionY = 0; //設定圖片在Y軸的移動方向
var directionX = 0; //設定圖片在X軸的移動方向
function changePos(){
var img = document.getElementById("float"); //圖片所在層ID
var width = document.documentElement.clientWidth; //瀏覽器寬度
var height = document.documentElement.clientHeight; //瀏覽器高度
var imgHeight=document.getElementById("floatImg").height; //漂浮圖片高度
var imgWidth=document.getElementById("floatImg").width; //漂浮圖片寬度
img.style.left =parseInt(moveX + document.documentElement.scrollLeft)+"px"; //漂浮圖片距瀏覽器左側位置
img.style.top = parseInt(moveY + document.documentElement.scrollTop)+"px"; //漂浮圖片距瀏覽器頂端位置
//alert(img.style.left);
if (directionY==0){
moveY = moveY + step; //漂浮圖片在Y軸方向上向下移動
}
else{
moveY = moveY - step; //漂浮圖片在Y軸方向上向上移動
}
if (moveY < 0) { //如果漂浮圖片漂到瀏覽器頂端時,設定圖片在Y軸方向上向下移動
directionY = 0;
moveY = 0;
}
if (moveY >= (height - imgHeight)) { //如果漂浮圖片漂到瀏覽器底端時,設定圖片在Y軸方向上向上移動
directionY = 1;
moveY = (height - imgHeight);
}
if (directionX==0){
moveX = moveX + step; //漂浮圖片在X軸方向上向右移動
}
else {
moveX = moveX - step; //漂浮圖片在X軸方向上向左移動
}
if (moveX < 0) { //如果漂浮圖片漂到瀏覽器左側時,設定圖片在X軸方向上向右移動
directionX = 0;
moveX = 0;
}
if (moveX >= (width - imgWidth)) { //如果漂浮圖片漂到瀏覽器右側時,設定圖片在X軸方向上向左移動
directionX = 1;
moveX = (width - imgWidth);
}
// setTimeout("changePos()",30);
}
setInterval("changePos()",30);
//window.onload=changePos;
-----------------------------------------------------------------------------------------------------------------網頁原始碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>漂浮廣告</title>
<link href="css/float.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="float" class="float"><img src="images/float.jpg" id="floatImg" alt="漂浮廣告" /></div>
<script type="text/javascript" src="js/float.js"></script>
</body>
</html>
js製作浮動廣告