niveanvp73 Posted July 8, 2008 Share Posted July 8, 2008 Hi, I would like to use the function "moveBy". I know it's being used like self.moveBy - this shakes the window included the browser...but with this function I have always problems - in IE7 it does not work. Is there any to use on the whole "<body>" tag?I tried it this but it doesn't work...Could someone help? <script type="text/javascript">x=document.getElementsByTagName("body")[0]function shakeit(n){ if (x.moveBy){ for (i = 10; i > 0; i--) { for (j = n; j > 0; j--) { x.moveBy(0,i); x.moveBy(i,0); x.moveBy(0,-i); x.moveBy(-i,0); }} } }</script> Link to comment Share on other sites More sharing options...
justsomeguy Posted July 8, 2008 Share Posted July 8, 2008 Just pick up the monitor and move it rapidly from side to side.You can't move the body element, the position of it never changes relative to the window. You would need to wrap your entire page in a div and move the div around. You wouldn't use moveBy on an element either, moveBy is only for the window object. If you want to change the X and Y coordinates of an element it's best to first set them using CSS, and then access the CSS properties and change them. <div id="move" style="position: relative; top: 10px; left: 20px;">test</div> <script type="text/javascript"> el = document.getElementById("move"); left = parseInt(el.style.left.substr(0, el.style.left.length - 2), 10); // get current left position top = parseInt(el.style.top.substr(0, el.style.top.length - 2), 10); // get current top position alert(left); alert(top); left += 10; // add 10 to left top += 10; // add 10 to top el.style.left = left + "px"; // set new left el.style.top = top + "px"; // set new top </script> Link to comment Share on other sites More sharing options...
niveanvp73 Posted July 8, 2008 Author Share Posted July 8, 2008 Thank you very much.Apparently you understand javascript pretty well...may I ask why are those alert(left) and alert(top) there?I tried to run it without them but it didn't work...is it possible to do without them? Link to comment Share on other sites More sharing options...
jesh Posted July 8, 2008 Share Posted July 8, 2008 You can also move the entire window around on the screen, but it's one of the most annoying things I've ever seen: <html><body><script type="text/javascript">function moveWin(){ var x = (Math.random() * 10) - 5; var y = (Math.random() * 10) - 5; this.moveBy(x, y); setTimeout("moveWin();", 100);}moveWin();</script></body></html> See this:http://www.w3schools.com/htmldom/met_win_moveby.asp Link to comment Share on other sites More sharing options...
shadowayex Posted July 8, 2008 Share Posted July 8, 2008 The script works without the alerts probably. The alerts are probably there just to let you know what those values are in case you want to change them or if something goes wrong you can make sure it's using the right values. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 8, 2008 Share Posted July 8, 2008 The alerts are just for debugging, so you can verify that it's reading the position properly. All that script does is add 10 pixels to the top and left, so it moves the box 10 pixels down and to the right. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.