20分奖励:javascript写一个浮动div
发布网友
发布时间:2022-04-24 05:48
我来回答
共2个回答
热心网友
时间:2022-04-24 07:18
给你个简单明了的
要完成此效果需要三个步骤
第一步:把如下代码加入到<head>区域中
<SCRIPT LANGUAGE="JavaScript">
function setVariables() {
imgwidth=235;
imgheight=19;
if (navigator.appName == "Netscape") {
horz=".left";
vert=".top";
docStyle="document.";
styleDoc="";
innerW="window.innerWidth";
innerH="window.innerHeight";
offsetX="window.pageXOffset";
offsetY="window.pageYOffset";
}
else {
horz=".pixelLeft";
vert=".pixelTop";
docStyle="";
styleDoc=".style";
innerW="document.body.clientWidth";
innerH="document.body.clientHeight";
offsetX="document.body.scrollLeft";
offsetY="document.body.scrollTop";
}
}
function checkLocation() {
objectXY="branding";
var availableX=eval(innerW);
var availableY=eval(innerH);
var currentX=eval(offsetX);
var currentY=eval(offsetY);
x=availableX-(imgwidth+30)+currentX;
y=availableY-(imgheight+65)+currentY;
evalMove();
setTimeout("checkLocation()",10);
}
function evalMove() {
eval(docStyle + objectXY + styleDoc + horz + "=" + x);
eval(docStyle + objectXY + styleDoc + vert + "=" + y);
}
</script>
第二步:把如下代码加入到<body>区域中
<div id="branding" style="position:absolute; visibility:show; left:235px; top:50px; z-index:2">
<table width=10 ><td>
<a href="http://free.2259.com" onmouseover="window.status='Typhoon Start';return true" onmouseout="window.status='';return true"><center>
<img src="2259.gif" border="0">
</center></a></font></td>
</table>
</div>
第三步:把“onload="setVariables(); checkLocation();"”加到<body>标记中
例如:<body onload="setVariables(); checkLocation();">
热心网友
时间:2022-04-24 08:36
这是我网站上的代码,你应该能看明白吧:
<DIV ID=dd STYLE="Z-INDEX:1; LEFT:46px; POSITION: absolute; TOP: 124px;" onmouseover="fly_ctrl=false" onmouseout="fly_ctrl=true">
<A HREF="/cai/ymessage.html" title="向你推荐阳光QQ:工作联系的最佳工具"><IMG HEIGHT=32 WIDTH=32 BORDER=0 style="filter:chroma(color=#ffffff)" SRC="/ym.gif"></A></DIV>
<SCRIPT LANGUAGE=JavaScript>
var fly_ctrl=true;
var xPos = 20;
var yPos = document.body.clientHeight;
var step = 1;
var delay = 30;
var height = 0;
var Hoffset = 0;
var Woffset = 0;
var yon = 0;
var xon = 0;
var pause = true;
var interval;
dd.style.top = yPos;
function changePos() {
if (fly_ctrl==false) return;
width = document.body.clientWidth;
height = document.body.clientHeight;
Hoffset = dd.offsetHeight;
Woffset = dd.offsetWidth;
dd.style.left = xPos + document.body.scrollLeft;
dd.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);
}
}
function start() {
dd.visibility = "visible";
interval = setInterval('changePos()', delay);
}
function pause_resume() {
if(pause) {
clearInterval(interval);
pause = false;
}
else {
interval = setInterval('changePos()',delay);
pause = true;
}
}
start();
</SCRIPT>