views:

56

answers:

1

Question: Why wont the If and the Else both work, when both conditions can be met at different times by scrolling below or above '.fakeheading'? Is the Else-statement overpowering the If-statement?

JQUERY:
$(window).scrollTop(function() {

    $('.grid_12').find(function () {
        var $header = $(this);

         if ($header.offset().top < $(window).Top()) {
             $(".fakeheading").css({position:"fixed"})
         } else {
            $(".fakeheading").css({position:"absolute"}) 
        } 
    });
});

additional info/thoughts:
Why wont the 'if' statement work, if I have $(".fakeheading").css({position:"absolute"})
in the 'else' statement. Either the If, or the Else is working, but they will not both work together like they should. It's like my code wont obey the if-statement when I have the code that's contained in the else-statement. but if I mess up the else-statement code,(have it be

posssssition:"absolute"

then the if-statement part of my script works fine.

+3  A: 

When you have and IF statement with and ELSE, only the first "true" condition is processed. Thus if the process looks like the following psuedo code:

 if (true) {do true stuff}
 else { do false stuff}

only the first one will actually process, the second will be not be.

On the other hand if you have:

 if (true) {do true stuff}
 if (other true stuff) {do other true stuff}

structure, they both could process if the condition is true

EDIT: One other note if you have:

if (true) {do true stuff} 
 else if (second true stuff) { do second true stuff} 
 else if (third true stuff) { do third true stuff}

only the first "true" process will execute, whichever one from the top down, equates to true first.

Mark Schultheiss
keep in mind that javascript will actually skip evaluation of secondary code once a true has been found, thus your "most often true" should be first if the logic allows as that will optimize the evaluation avoiding processing of less frequent conditionals.
Mark Schultheiss