ThomasJSmart
Programmer
- Sep 16, 2002
- 634
Hi
iv made a script with which i position a DIV at a given X location, it works ok, the div moves within the space it should but the problem is its VERY jittery, translated to frames per second its like its doing the moving at about 1 FPS...
iv seen javascripts doing this kind of thing real time so why isnt mine working like that?
I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
iv made a script with which i position a DIV at a given X location, it works ok, the div moves within the space it should but the problem is its VERY jittery, translated to frames per second its like its doing the moving at about 1 FPS...
iv seen javascripts doing this kind of thing real time so why isnt mine working like that?
Code:
<html>
<head></head>
<body onResize="resetWidth()">
<div id="menuHolder" style="width:950px; height:378px; position:relative;">
<div id="mainmenuDiv" style="width:267px; height:378px; z-index:10; left:0px; top:0px; position:absolute; background-image:url(img/menuBack.png); background-repeat:no-repeat;">menu1 <br>menu2<br>menu3<br>menu4</div>
</div>
<script>
function FindIt(n, d){
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length)
{d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n];for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=FindIt(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function mouseCoords(ev){
if(ev.pageX){ return ev.pageX; }
return ev.clientX + document.body.scrollLeft - document.body.clientLeft;
}
function mouseMove(ev){
ev=ev||window.event;
var mousePos = mouseCoords(ev);
posX = mousePos-sideWidth;
if(posX<0){ posX=0;} if(posX>(950-menuWidth)){ posX=(950-menuWidth);}
FindIt('mainmenuDiv').style.left=posX+'px';
return false;
}
function resetWidth(){
pageWidth = getWinWidth();
sideWidth = (Math.round((pageWidth-950)/2))+menuWidth;
if(sideWidth<0){ sideWidth=0;}
}
function getIECanvas(){
var canv = null;
if (!window.opera && document.all && typeof document.body.clientWidth != "undefined"){
var cm = document.compatMode && document.compatMode == "CSS1Compat";
canv = cm ? document.documentElement : document.body;
}
return canv;
}
function getWinWidth(){
var canv;
if (canv=getIECanvas()){
width = canv.clientWidth;
}else{
width = window.innerWidth - 18;
}
return width;
}
var menuWidth=134;
var pageWidth = getWinWidth();
var sideWidth = Math.round( ((pageWidth-950)/2)+menuWidth );
document.onmousemove = mouseMove;
</script>
</body>
I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!