Here I am testing to see if a box can follow a cursor around the screen using JavaScript and CSS. Give it a try and see what happens. Below, I go through the source code for you and my usability findings.
Some random text.
Below is the source code for the JavaScript file, it features the mouse coordinate functions. It may be more complicated than other examples which can be found on the internet but you’ll see that this script does not use browser profiling and it also switches the position of the box if it thinks that some of the box will be outside of the browser’s window.
varfoundDrags=false;varpArray=newArray();varimgArray=newArray();functionfindDrags(){pArray=document.getElementsByTagName("p");imgArray=document.getElementsByTagName("img");foundDrags=true;}vargap=20;varpBorder=5;varpWidth=400;// Padding + WidthfunctionmouseMove(event){if(!event){event=window.event// Required for IE}mousex=event.clientX;mousey=event.clientY;if(window.innerWidth){windowx=window.innerWidth;}else{windowx=document.body.offsetWidth;// Fallback for IE}if(foundDrags){for(i=0;i<pArray.length;i=i+1){if(pArray[i].className=="drag"){if((mousex+gap+pBorder+pWidth+pBorder+gap)<windowx){pArray[i].style.left=(mousex+gap)+"px";}else{pArray[i].style.left=(mousex-pBorder-pWidth-pBorder-gap)+"px";}pArray[i].style.top=mousey+"px";}}for(i=0;i<imgArray.length;i=i+1){if(imgArray[i].className=="drag"){if((mousex+gap+gap+imgArray[i].width)<windowx){imgArray[i].style.left=(mousex+gap)+"px";}else{imgArray[i].style.left=(mousex-imgArray[i].width-gap)+"px";}imgArray[i].style.top=mousey+"px";}}}else{findDrags();}}document.onmousemove=mouseMove;