您的位置:首页 > Web前端 > JavaScript

JavaScript-浮动广告

2013-03-13 14:47 288 查看
<img id="float" style="position:absolute" src="http://img.baidu.com/img/logo-zhidao.gif" />
<script>
var xPos = 0, yPos = 0, step = 1, yon = 0, xon = 0;
var img = document.getElementById("float");

function changePos() {
var width = document.body.clientWidth;
var height = document.body.clientHeight;
var Hoffset = img.offsetHeight;
var Woffset = img.offsetWidth;
img.style.left = xPos + document.body.scrollLeft;
img.style.top = yPos + document.body.scrollTop;
if (yon) {
yPos = yPos + step;
} else {
yPos = yPos - step;
}

if (yPos < 0) {
yon = 1;
yPos = 0;
}

if (yPos >= (height - Hoffset)) {
yon = 0;
yPos = (height - Hoffset);
}

if (xon) {
xPos = xPos + step;
} else {
xPos = xPos - step;
}

if (xPos < 0) {
xon = 1;
xPos = 0;
}

if (xPos >= (width - Woffset)) {
xon = 0;
xPos = (width - Woffset);
}

setTimeout('changePos()', 30);
}

window.onload = changePos;
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: