logo

Floating box in javascript

By Pritam Baldota
Printer Friendly Version
View My Articles
2041 Views
    

Learn how to create floating windows in JavaScript.


Floating box in javascript
///paste in ur css file

#topbar{
position:absolute;
border: 1px solid black;
padding: 2px;
background-color: #CACAFF;
width: 620px;
visibility: hidden;
z-index: 100;
}

///paste your content in js file
var persistclose=0 //set to 0 or 1. 1 means once the bar is manually closed, it will remain closed for browser session
var startX = 30 //set x offset of bar in pixels
var startY = 5 //set y offset of bar in pixels
var verticalpos="fromtop" //enter "fromtop" or "frombottom"

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function closebar(){
if (persistclose)
document.cookie="remainclosed=1"
document.getElementById("topbar").style.visibility="hidden"
}

function staticbar(){
barheight=document.getElementById("topbar").offsetHeight
var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
var d = document;
function ml(id){
var el=d.getElementById(id);
if (!persistclose || persistclose && get_cookie("remainclosed")=="")
el.style.visibility="visible"
if(d.layers)el.style=el;
el.sP=function(x,y){this.style.left=x+"px";this.style.top=y+"px";};
el.x = startX;
if (verticalpos=="fromtop")
el.y = startY;
else{
el.y = ns ? pageYOffset + innerHeight : iecompattest().scrollTop + iecompattest().clientHeight;
el.y -= startY;
}
return el;
}
window.stayTopLeft=function(){
if (verticalpos=="fromtop"){
var pY = ns ? pageYOffset : iecompattest().scrollTop;
ftlObj.y += (pY + startY - ftlObj.y)/8;
}
else{
var pY = ns ? pageYOffset + innerHeight - barheight: iecompattest().scrollTop + iecompattest().clientHeight - barheight;
ftlObj.y += (pY - startY - ftlObj.y)/8;
}
ftlObj.sP(ftlObj.x, ftlObj.y);
setTimeout("stayTopLeft()", 10);
}
ftlObj = ml("topbar");
stayTopLeft();
}

if (window.addEventListener)
window.addEventListener("load", staticbar, false)
else if (window.attachEvent)
window.attachEvent("onload", staticbar)
else if (document.getElementById)
window.onload=staticbar

/////paste below content in your htl page




Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos

Article Discussion: Floating box in javascript
Pritam Baldota posted at Friday, September 29, 2006 6:16 AM
Original Article
 

  Floating box in javascript
K Pravin Kumar Reddy replied to Pritam Baldota at Friday, September 29, 2006 6:31 AM

hello

<div id="leftSideBar" style="position:absolute;">
content here!
</div>

<script language="javascript">
<!--

function floatSideBar(bY,lPY) { //boxY, lastPageY
//pageY, pageHeight, boxHeight, boxId
var pY,pH,bH,id=\'leftSideBar\';

pY=(navigator.appName.indexOf("Netscape")!=-1)?pageYOffset:nn&&document.html.scrollTop?document.html.scrollTop:document.body.scrollTop;
if(pY!= lPY) { //they scrolled

//pH = document.body.offsetHeight;
//else if (document.layers) pH = window.innerHeight;
pH = 700;
bH = document.getElementById(id).offsetHeight;

if(pY < bY) { //need to move box up
bY = pY;
document.getElementById(id).style.top = bY+\'px\';
}
else if(pY + pH > bY + bH) { //need to move box down
bY = pY + pH - bY;
document.getElementById(id).style.top = bY+\'px\';
}
}

setTimeout('floatSideBar(\'+bY+\',\'+pY+\')',100);
};

floatSideBar(140,0);

//-->
</script>


or

The following should be inserted in the <HEAD> area of your page:


<script language="JavaScript1.2">
var offsetfromedge=10 //offset from window edge when content is "docked". Change if desired.
var dockarray=new Array() //array to cache dockit instances
var dkclear=new Array() //array to cache corresponding clearinterval pointers

function dockit(el, duration){
if (!document.all) return;
this.source=document.all? document.all[el] : document.getElementById(el);
this.source.height=this.source.offsetHeight;
this.docheight=truebody().clientHeight;
this.duration=duration;
this.pagetop=0;
this.elementoffset=this.getOffsetY();
dockarray[dockarray.length]=this;
var pointer=eval(dockarray.length-1);
var dynexpress='dkclear['+pointer+']=setInterval("dockornot(dockarray['+pointer+'])",100);';
dynexpress=(this.duration>0)? dynexpress+'setTimeout("clearInterval(dkclear['+pointer+']); dockarray['+pointer+'].source.style.top=0", duration*1000)' : dynexpress;
eval(dynexpress);
}

dockit.prototype.getOffsetY=function(){
var totaloffset=parseInt(this.source.offsetTop);
var parentEl=this.source.offsetParent;
while (parentEl!=null){
totaloffset+=parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function dockornot(obj){
obj.pagetop=truebody().scrollTop;
if (obj.pagetop>obj.elementoffset) //detect upper offset
obj.source.style.top=obj.pagetop-obj.elementoffset+offsetfromedge;
else if (obj.pagetop+obj.docheight<obj.elementoffset+parseInt(obj.source.height)) //lower offset
obj.source.style.top=obj.pagetop+obj.docheight-obj.source.height-obj.elementoffset-offsetfromedge;
else
obj.source.style.top=0;
}

function truebody(){
return (document.compatMode!="BackCompat")? document.documentElement : document.body
}

if (document.all)
document.write('<style>.dockclass{position:relative;}</style>')

</script>

The following demonstrates the insertion of the Google adsense code - in 120x600 skyscraper form - within a table:

<table width="125px" border="0" id="google_dock" class="dockclass">

<center>
<script type="text/javascript"><!--
google_ad_client = "pub-123456789";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as";
google_color_border = "000000";
google_color_bg = "000000";
google_color_link = "FF0000";
google_color_url = "666666";
google_color_text = "FFFF00";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</center>
</table>
 
<script language="JavaScript1.2">
var dock0=new dockit("google_dock", 0);
</script>


Note the ID and CLASS parameters in the opening TABLE tag above.

Finally, the following code should be inserted at the bottom of the page, just before the closing </body> tag:


<script language="JavaScript1.2">
var dock0=new dockit("google_dock", 0);
</script>
 
reference
 
 






  $1000 Contest    [)ia6l0 iii - $228  |  Jonathan VH - $161  |  Huggy Bear - $135  |  F Cali - $95  |  egg egg - $94  |  more Advertise  |  Privacy  |   (c) 2010