28,391
社区成员
发帖
与我相关
我的任务
分享
<!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>
<script type="text/javascript">
//函数
var FloatContent = window.FloatContent || function () {
if (!window.Instances) {
window.Instances = {};
window.Instances.Get = function (i) {
return window.Instances[i];
}
}
var Instance = this;
this.Timer1 = null;
this.Speed = 10;
this.HtmlContent = "";
this.DivFloat = null;
this.Guid = ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".replace(/[x]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : r & 0x3 | 0x8; return v.toString(16); }).toUpperCase());
//全局实例集合。
window.Instances[this.Guid] = this;
this.Start = function () {
var d = document.createElement("div");
d.style.position = "absolute";
d.style.backgroundColor = "red";
d.innerHTML = this.HtmlContent;
d.setAttribute("id", this.Guid);
if (d.addEventListener) {
d.addEventListener("mouseover", function () { Instance.Stop(); }, false);
d.addEventListener("mouseout", function () { Instance.ReStart(); }, false);
}
else {
d.attachEvent("onmouseover", function () { Instance.Stop(); });
d.attachEvent("onmouseout", function () { Instance.ReStart(); });
}
document.body.appendChild(d);
this.DivFloat = d;
Instance.ReStart();
}
this.ReStart = function () {
this.Timer1 = window.setInterval("window.Instances['" + this.Guid + "'].changePosition()", this.Speed);
}
this.Stop = function () { if (this.Timer1) window.clearInterval(this.Timer1); }
this.x = Math.ceil(Math.random() * 600), this.y = Math.ceil(Math.random() * 400);
this.xStep = 2, this.yStep = 2;
this.Direction = true; this.yDirection = true;
function getViewportWidth() {
var width = 0;
if (document.documentElement && document.documentElement.clientWidth) {
width = document.documentElement.clientWidth;
}
else if (document.body && document.body.clientWidth) {
width = document.body.clientWidth;
}
else if (window.innerWidth) {
width = window.innerWidth;
}
return width;
}
function getContentHeight() {
if (document.body && document.body.offsetHeight) {
return document.body.offsetHeight;
}
}
function getViewportHeight() {
var height = 0;
if (window.innerHeight) {
height = window.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight) {
height = document.documentElement.clientHeight;
}
else if (document.body && document.body.clientHeight) {
height = document.body.clientHeight;
}
else if (window.innerHeight) {
height = window.innerHeight;
}
return height;
}
function getViewportScrollX() {
var scrollX = 0;
if (document.documentElement && document.documentElement.scrollLeft) {
scrollX = document.documentElement.scrollLeft;
}
else if (document.body && document.body.scrollLeft) {
scrollX = document.body.scrollLeft;
}
else if (window.pageXOffset) {
scrollX = window.pageXOffset;
}
else if (window.scrollX) {
scrollX = window.scrollX;
}
return scrollX;
}
function getViewportScrollY() {
var scrollY = 0;
if (document.documentElement && document.documentElement.scrollTop) {
scrollY = document.documentElement.scrollTop;
}
else if (document.body && document.body.scrollTop) {
scrollY = document.body.scrollTop;
}
else if (window.pageYOffset) {
scrollY = window.pageYOffset;
}
else if (window.scrollY) {
scrollY = window.scrollY;
}
return scrollY;
}
function getViewportScrollHeight() {
var scrollH = 0;
if (document.documentElement && document.documentElement.scrollHeight) {
scrollH = document.documentElement.scrollHeight;
}
else {
scrollH = document.body.scrollHeight;
}
return scrollH;
}
this.changePosition = function () {
this.x = this.x + this.xStep * (this.xDirection ? 1 : -1);
this.y = this.y + this.yStep * (this.yDirection ? 1 : -1);
//若超出浏览器的宽度反向移动
if (this.x <= 0) this.xDirection = true;
if (this.x >= getViewportWidth() - this.DivFloat.offsetWidth) this.xDirection = false;
if (this.y <= 0) this.yDirection = true;
if (this.y >= getViewportHeight() - this.DivFloat.offsetHeight) this.yDirection = false;
//Div重新定位
this.DivFloat.style.top = this.y + getViewportScrollY() + "px";
this.DivFloat.style.left = this.x + getViewportScrollX() + "px";
}
}
</script>
</head>
<body>
<script language="javascript" type="text/javascript">
dd = new FloatContent();
dd.Speed = 50;
dd.HtmlContent = "<a href='http://dotnet.aspx.cc/'><img src='http://dotnet.aspx.cc/Images/logoSite.gif' border='0' /></a>";
dd.Start();
d2 = new FloatContent();
d2.Speed = 10;
d2.HtmlContent = "<a href='http://dotnet.aspx.cc/'><img src='http://dotnet.aspx.cc/Images/logoSite.gif' border='0' /></a>";
d2.Start();
</script>
</body>
</html>