Code:
<!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" xml:lang="en" lang="en">
<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
.p {
position:relative;left:100px;top:100px;width:400px;height:400px;border:solid black 1px;
}
#float {
position:absolute;left:10px;top:0px;width:100px;height:40px;border:solid black 1px;
}
/*]]>*/
</style>
<script type="text/javascript">
/*<![CDATA[*/
function zxcFloat(id){
this.obj=document.getElementById(id);
this.p=this.obj.parentNode;
this.addevt(window,'scroll','Float');
}
zxcFloat.prototype.Float=function(){
this.obj.style.top=Math.min(Math.max(zxcWWHS()[3]-zxcPos(this.p)[1],0),this.p.offsetHeight-this.obj.offsetHeight)+'px';
}
zxcFloat.prototype.addevt=function(o,t,f,p){
var oop=this;
if (o.addEventListener) o.addEventListener(t,function(e){ return oop[f](e,p);}, false);
else if (o.attachEvent) o.attachEvent('on'+t,function(e){ return oop[f](e,p); });
else {
var prev=o['on'+t];
if (prev) o['on'+t]=function(e){ prev(e); oop[f](e,p); };
else o['on'+t]=o[f];
}
}
function zxcWWHS(){
if (window.innerHeight) return [window.innerWidth-10,window.innerHeight-10,window.pageXOffset,window.pageYOffset];
else if (document.documentElement.clientHeight) return [document.documentElement.clientWidth-10,document.documentElement.clientHeight-10,document.documentElement.scrollLeft,document.documentElement.scrollTop];
return [document.body.clientWidth,document.body.clientHeight,document.body.scrollLeft,document.body.scrollTop];
}
function zxcPos(obj){
var rtn=[0,0];
while(obj){
rtn[0]+=obj.offsetLeft;
rtn[1]+=obj.offsetTop;
obj=obj.offsetParent;
}
return rtn;
}
/*]]>*/
</script>
</head>
<body>
<div class="p" >
<div ></div>
<img id="float" src="http://www.vicsjavascripts.org.uk/StdImages/One.gif" />
</div>
<div style="height:1000px;" ></div>
<script type="text/javascript">
/*<![CDATA[*/
new zxcFloat('float');
/*]]>*/
</script>
</body>
</html>
or
Code:
<!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" xml:lang="en" lang="en">
<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
#p {
position:relative;left:100px;top:100px;
}
#float {
position:absolute;z-Index:101;left:110px;top:0px;width:100px;height:40px;border:solid black 1px;
}
/*]]>*/
</style>
<script type="text/javascript">
/*<![CDATA[*/
function zxcFloat(pid,id){
this.obj=document.getElementById(id);
this.p=document.getElementById(pid); ;
this.addevt(window,'scroll','Float');
this.Float();
}
zxcFloat.prototype.Float=function(){
this.obj.style.top=Math.min(Math.max(zxcPos(this.p)[1],zxcWWHS()[3]),zxcPos(this.p)[1]+this.p.offsetHeight-this.obj.offsetHeight)+'px';
}
zxcFloat.prototype.addevt=function(o,t,f,p){
var oop=this;
if (o.addEventListener) o.addEventListener(t,function(e){ return oop[f](e,p);}, false);
else if (o.attachEvent) o.attachEvent('on'+t,function(e){ return oop[f](e,p); });
else {
var prev=o['on'+t];
if (prev) o['on'+t]=function(e){ prev(e); oop[f](e,p); };
else o['on'+t]=o[f];
}
}
function zxcWWHS(){
if (window.innerHeight) return [window.innerWidth-10,window.innerHeight-10,window.pageXOffset,window.pageYOffset];
else if (document.documentElement.clientHeight) return [document.documentElement.clientWidth-10,document.documentElement.clientHeight-10,document.documentElement.scrollLeft,document.documentElement.scrollTop];
return [document.body.clientWidth,document.body.clientHeight,document.body.scrollLeft,document.body.scrollTop];
}
function zxcPos(obj){
var rtn=[0,0];
while(obj){
rtn[0]+=obj.offsetLeft;
rtn[1]+=obj.offsetTop;
obj=obj.offsetParent;
}
return rtn;
}
/*]]>*/
</script>
</head>
<body>
<table id="p" width="200" height="400" border="1">
<tr>
<td>Table</td>
</tr>
</table>
<img id="float" src="http://www.vicsjavascripts.org.uk/StdImages/One.gif" />
<div style="height:1000px;" ></div>
<script type="text/javascript">
/*<![CDATA[*/
new zxcFloat('p','float');
/*]]>*/
</script>
</body>
</html>
Bookmarks