html - Make element move by using Javascript -
i trying make webpage when click link, link moves diagonally every 100 milliseconds.
so have javascript, right when click link nothing happens
also, know of javascript ide can use make sure have no errors in code?
ps: know why elements dont stretch fit whole 200px 200px of div element? links small when should same width parent div element.
edited new advice, although still wont move.
<script language="javascript" type = "text/javascript"> <!-- var block = null; var clockstep = null; var index = 0; var maxindex = 6; var x = 0; var y = 0; var timerinterval = 100; // milliseconds var xpos = null; var ypos = null; function moveblock() { if ( index < 0 || index >= maxindex || block == null || clockstep == null ) { clearinterval( clockstep ); return; } block.style.left = xpos[index] + "px"; block.style.top = ypos[index] + "px"; index++; } function onblockclick( blockid ) { if ( clockstep != null ) { return; } block = document.getelementbyid( blockid ); index = 0; x = parseint( block.style.left, 10 ); y = parseint( block.style.top, 10 ); xpos = new array( x+10, x+20, x+30, x+40, x+50, x+60 ); ypos = new array( y-10, y-20, y-30, y-40, y-50, y-60 ); clockstep = self.setinterval( moveblock(), timerinterval ); } --> </script> <style type="text/css" media="all"> <!-- @import url("styles.css"); #blockmenu { z-index: 0; width: 650px; height: 600px; background-color: blue; padding: 0; } #block1 { z-index: 30; position: relative; top: 10px; left: 10px; background-color: red; width: 200px; height: 200px; margin: 0; padding: 0; /* background-image: url("images/block1.png"); */ } #block2 { z-index: 30; position: relative; top: 50px; left: 220px; background-color: red; width: 200px; height: 200px; margin: 0; padding: 0; /* background-image: url("images/block1.png"); */ } #block3 { z-index: 30; position: relative; top: 50px; left: 440px; background-color: red; width: 200px; height: 200px; margin: 0; padding: 0; /* background-image: url("images/block1.png"); */ } #block4 { z-index: 30; position: relative; top: 0px; left: 600px; background-color: red; width: 200px; height: 200px; margin: 0; padding: 0; /* background-image: url("images/block1.png"); */ } #block1 { display: block; width: 100%; height: 100%; } #block2 { display: block; width: 100%; height: 100%; } #block3 { display: block; width: 100%; height: 100%; } #block4 { display: block; width: 100%; height: 100%; } #block1 a:hover { background-color: green; } #block2 a:hover { background-color: green; } #block3 a:hover { background-color: green; } #block4 a:hover { background-color: green; } #block1 a:active { background-color: yellow; } #block2 a:active { background-color: yellow; } #block3 a:active { background-color: yellow; } #block4 a:active { background-color: yellow; } --> </style>
errors needed fix
to fill width of div
elements, a
elements need display: block;
not default display: inline;
.
knowing runtime errors more important in opinion, , ides don't catch dom errors or more complex syntax; use error logging in browser (firefox's called error console). that'll catch in-development errors syntax errors.
this important point stress: block.style.left
, block.style.top
not numbers implicit pixel values in them. setting number without unit suffix absolutely nothing. need add %
or px
or whatever unit when setting left
, top
.
when getting current value, in var x = ...
, var y = ...
, need number()
manually numeric portion of string.
also, believe meant || block == null
, not =
, set block
null
.
tips
you can use moveblock
instead of "moveblock();"
argument settimeout
. avoids parsing string code, , avoids scope problems (though not in example moveblock
global).
i know have array of values, both x
, y
move 10 each time. assume want move @ 45 degree angle. if so, won't work expect after fixing errors x
percentage , y
in pixels.
Comments
Post a Comment