0

Does anyone know how add to this script?

I need to check whether the visitor is at the top of the page? If not call the fallback script.

E.g. if user is at top of page delay animation, if user is elsewhere remove delay (fallback else condition).

var viewportWidth = $(window).width(),
    stopTabletAnimation = 1024,
    scrTop;

$(window).on("load scroll", function() {
    scrTop = $("html, body").scrollTop();
    if (!scrTop && viewportWidth > stopTabletAnimation){
        // DELAY ANIMATION - FORCE TOP CORRECTLY (WITH A DELAY - .9s)
        $('.website-navigation').delay(3500).animate({top:0}, 900, function() {
            // Confirm Above if successful!
            console.log('ANIMATION COMPLETE: .site-head Delayed, then loaded!');
        });

    } else {
        // Do Not Animate
        $('.website-navigation').css({top: '0'});
        console.log('NO ANIMATION: Inforce Set Height');
    }
});
Neil
  • 971
  • 2
  • 12
  • 33

2 Answers2

1
var scrTop;

$(window).on("load scroll", function() {
  scrTop = $("html, body").scrollTop();
  if(!scrTop){ //if scrollTop is at 0

  }else{

  }
});

or in your case probably something like:

if (!scrTop && viewportWidth > stopTabletAnimation){
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • Just about to add my comment, 6 seconds to slow :) Let me update the code. And see what happens! – Neil Jan 12 '14 at 18:24
  • May I ask on your latest update what the difference between && and || is? – Neil Jan 12 '14 at 18:30
  • 1
    @Neil `&&` means that both `condition` **AND** `condition` must be truthy, while `||` means that just one `condition` **OR** `condition` need to be truthy to validate – Roko C. Buljan Jan 12 '14 at 18:33
  • I have made the amends, but to no avail. It seems to be trapped in the loop as it does not return the else statement. If I scroll down the page and reloaded the page I still get the incorrect console message and coded delay. Cache has been emptied. – Neil Jan 12 '14 at 18:49
  • Thanks for get the grunt of scrTop variable. Much appreciated! – Neil Jan 13 '14 at 12:15
1

Change the check from

scrTop = $("html, body").scrollTop();

To

scrTop = $("body").scrollTop();

This will detect how far away from the top the user is.

Edit: This should be set with

scrTop = $(window).scrollTop();

Ass explained here: https://stackoverflow.com/a/2422159/1809751

Community
  • 1
  • 1
Tom Burgess
  • 286
  • 2
  • 7