I have the simple function of changing content of container div. There is a <table> inside the container <div>, with 2 <td>s, the javascript simply moves left this <table> and it works, but onclick and I need to execute the moving function with time interval.
<div onclick="move(this.children[0])" class="table_wrap"> <div class="table_scroll" style="left: 0px;"> <table cellpadding="0" cellspacing="5" width="1920px"> <tr> <td style="background-color: red;"> a </td> <td style="background-color: blue;"> b </td> </tr> </table> </div> </div> <script language="javascript"> function move(elem) { var left = 0 function frame() { left -= 5; // update parameters elem.style.left = left + 'px' // show frame if (left == -965) // check finish condition clearInterval(id) } var id = setInterval(frame, 1) // draw every 10ms } </script> And CSS:
<style type="text/css"> .table_wrap{ position:relative; overflow:hidden; width: 967px; height: 250px; left: -2px; top: 5px; } .table_wrap .table_scroll{ position: absolute; text-align:center; white-space:nowrap; } </style> I tryed to use setInterval, but failed.
http://jsfiddle.net/hqztm2dt/1/ here is the JSfiddle, just click on the red line.. Thanks in advance for the attention !
idis a local variable, and will be destroyed as soon as the execution offramehas ended. You've to declare the said variable outside offrame. Also you'd probably need asetTimeoutinstead ofsetInterval.setInterval(or preferablysetTimeout)?setIntervalif you don't show us what you did. My gut feeling is that because you don't have anidon the<div>your code is no longer able to (easily) access it through the DOM