开发者

Javascript height statement

开发者 https://www.devze.com 2023-02-01 09:19 出处:网络
This is not working and I can\'t figure out where I went wrong: <style> * { margin: 0px } div { height: 250px;

This is not working and I can't figure out where I went wrong:

<style>
* {
margin: 0px
}  

div {
    height: 250px;
    width: 630px;
    overflow: hidden;
    vertical-align: top;
    position: relative;
}
  iframe {
    position: absolute;
    left: -50px;
    top: -130px;
  }
</style>
<script>
window.onload = function() {
   document.getElementsByTagName('body')[0].onkeyup = function(e) { 
      var div = document.getElementById('capture');
      if(e.keyCode == 70) {
         if(div.style.height == 250){
             alert("Yes");
         }
         else {alert("no");}
      }
   }
};
</script>

++++++++++++++EDIT+++++++++++++

Ok, well this works fine:

         if(div.style.height < '260px'){
             alert("Yes!");
             div.style.height = '475px';
             pan.style.top = '-25';
         }
         else if(div.style.height > '260px') {
            alert("no");
            div.style.height = '250px';
            pan.style.top = '-130';
         }

But this doesn't:

     if(div.style.height = '250px'){
         alert("Yes!");
         div.style.height = '475px';
         pan.style.top = '-25';
     }
     else if(div.style.height = '475px') {开发者_如何学Go
        alert("no");
        div.style.height = '250px';
        pan.style.top = '-130';
     }

What gives?!?


div.style.height returns "250px" String rather than 250 Number, so your if(div.style.height == 250){ condition is not met.


Use parseInt to convert the value to number:

var divHeight = parseInt(div.style.height, 10);
if(divHeight === 250) {
    alert("Yes!");
    div.style.height = "475px";
    pan.style.top = "-25px";
}
else if (divHeight === 475) {
    alert("no");
    div.style.height = "250px";
    pan.style.top = "-130px";
}


I have decided to change it from toggling, to use numbers 1-3, which worked for me no problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号