js製作浮動廣告

來源:互聯網
上載者:User

標籤: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製作浮動廣告

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.