resolution - window.innerWidth in JavaScript -


here's code:

<body>     <header></header>     <div id="slider" class="slider">         <p>slider content</p>     </div>     <script src="jas.js"></script> </body> 


var divpos = 50; var direction = "l";  function preparepage() {     document.getelementbyid("slider").style.position = "absolute";     document.getelementbyid("slider").style.left = divpos + "px";     document.getelementbyid("slider").style.top = "50px";     animatediv = setinterval(beginanimate,50); }  function beginanimate () {     if (direction == "l") {         divpos+=50;         if (divpos == 1300) {             direction = "r";         }     } else if (direction == "r"){         divpos-=50;         if (divpos == 150) {             direction = "l";         }     }     document.getelementbyid("slider").style.left = divpos + "px"; }  window.onload = function () {     settimeout(preparepage,200); }; 

i want #slider go left right , when reaches point go back. code works fine, tried way:

if (direction == "l") {     divpos+=50;     if (divpos == window.innerwidth - 300) {         direction = "r";     } } 

and goes left ever. why happening? can make go @ point, no what screen resolution is?

use:

if (divpos > window.innerwidth - 300) { 

unless window's width multiple of 50, incrementing divpos 50 won't hit innerwidth - 300 exactly, need less exacting.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -