Question

Why is the if statement not working? The code is set to rotate a certain object by sdegree. The older code used to work without the if statement so im assuming the problem is there.

<script type="text/javascript">
    $(function() {
  var sdegree = 0;
  var orig = 0; 
  $(window).scroll(function() {
        if (sdegree > -30 && sdegree < 30 && sdegree-orig>=0) {
            sdegree + 4;
            orig = sdegree,
        } else if (sdegree > -30 && sdegree < 30 && sdegree-orig<0 ) {
            sdegree + 4;
            orig = sdegree,
        } else if (sdegree < -30) {
            sdegree + 4;
            orig = sdegree,
        } else if (sdegree > 30) {
            sdegree - 4;
            orig = sdegree;
        } else {
            orig = sdegree;
        }
    var srotate = "rotate(" + sdegree + "deg)";
    $(".plane").css({
      transform : srotate
    });
  });
});   
</script>
Was it helpful?

Solution

I think you probably meant:

sdegree += 4;

or

sdegree = sdegree + 4;

Notice the equals sign here. Otherwise you are doing nothing and sdegree is always 0.

Edit: I also just noticed you have commas at the ends of some of your lines in place of semicolons. Always check your browser console for js errors.

orig = sdegree,

should be:

orig = sdegree;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top