<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>常见悬浮广告-jq22.com</title>
<script src="page-transitions/js/jquery-3.2.1.js"></script>
<style>
div {
width:200px;
height:200px;
background:red;
position:absolute;
left:0;
top:0;
}
</style>
</head>
<body>
<div></div><script>
window.onload = function() {
var oDiv = document.body.children[0];
var iSpeedX = 3;
var iSpeedY = 3;
setInterval(function() {
var l = oDiv.offsetLeft + iSpeedX;
var t = oDiv.offsetTop + iSpeedY;
if (t > document.documentElement.clientHeight - oDiv.offsetHeight) {
t = document.documentElement.clientHeight - oDiv.offsetHeight;
iSpeedY *= -1;
}
if (l > document.documentElement.clientWidth - oDiv.offsetWidth) {
l = document.documentElement.clientWidth - oDiv.offsetWidth;
iSpeedX *= -1;
}
if (t < 0) {
t = 0;
iSpeedY *= -1;
}
if (l < 0) {
l = 0;
iSpeedX *= -1;
}
oDiv.style.left = l + 'px';
oDiv.style.top = t + 'px';
}, 16);
};</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>常见悬浮广告-jq22.com</title>
<script src="page-transitions/js/jquery-3.2.1.js"></script>
<style>
div {
width:200px;
height:200px;
background:red;
position:absolute;
left:0;
top:0;
}
</style>
</head>
<body>
<div></div><script>
window.onload = function() {
var oDiv = document.body.children[0];
var iSpeedX = 3;
var iSpeedY = 3;
setInterval(function() {
var l = oDiv.offsetLeft + iSpeedX;
var t = oDiv.offsetTop + iSpeedY;
if (t > document.documentElement.clientHeight - oDiv.offsetHeight) {
t = document.documentElement.clientHeight - oDiv.offsetHeight;
iSpeedY *= -1;
}
if (l > document.documentElement.clientWidth - oDiv.offsetWidth) {
l = document.documentElement.clientWidth - oDiv.offsetWidth;
iSpeedX *= -1;
}
if (t < 0) {
t = 0;
iSpeedY *= -1;
}
if (l < 0) {
l = 0;
iSpeedX *= -1;
}
oDiv.style.left = l + 'px';
oDiv.style.top = t + 'px';
}, 16);
};</script>
</body>
</html>
本文介绍了一种使用HTML和JavaScript实现悬浮广告自动移动的方法。通过设置元素的绝对定位并利用setInterval定时器调整其位置,使得广告框在浏览器窗口内自动移动,同时避免了广告框移出可视区域。
1559

被折叠的 条评论
为什么被折叠?



