R
R
Ruslan Timurziev2016-10-19 14:29:32
JavaScript
Ruslan Timurziev, 2016-10-19 14:29:32

How do we make an object (let's say a menu) appear when we've scrolled down about 500px?

And vice versa: when we move up, the element disappears.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Edward, 2016-10-19
@RuslanTimuziyev

.menu { display: none; }

$(window).on('scroll', function() {
    if ($(this).scrollTop() > 500) {
        $('.menu').fadeIn();
    } else {
        $('.menu').fadeOut();
    }
});

D
DTX, 2016-10-19
@DirecTwiX

var ttl = 0;
var prevS, prevScrollDir;
$(window).on('scroll', function() {
  var s = $(this).scrollTop();
  var scrollDir = s > prevS;
  
  if (scrollDir == prevScrollDir) {
    ttl += (s-prevS);
    if (ttl > 500) {
      $(".m").removeClass('hide');
    }
  } else {
    ttl = 0;
    $(".m").addClass('hide');
  }
  
  prevS = s;
  prevScrollDir = scrollDir;
});

codepen.io/anon/pen/BLGjpV

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question