Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide icon with jQuery

Tags:

jquery

I have

<img class="arrow_down" src="arrow_down.png" alt="scroll down" style="max-width: 5%; height: auto;">

Now, I want that image visible, until I scroll down the webpage, so from the first scroll it will be hidden. I code it in java-script or jQuery like this:

jQuery(function($, undefined) {
  if ($("body").scrollTop() = 0 || $("html").scrollTop() = 0) {
            $(".arrow_down").fadeIn(400);
        }

        else {
            $(".arrow_down").hide();
        }

    };

This doesn't work, please help me...

like image 301
David Stančík Avatar asked Sep 14 '15 17:09

David Stančík


1 Answers

You can do something like this:

$(function () {
  $('.arrow_down').hide();
  var curScroll = $(window).scrollTop();
  $(window).scroll(function() {
    if (curScroll < $(window).scrollTop())
      $('.arrow_down').show();
    if ($(window).scrollTop() == 0)
      $('.arrow_down').hide();
    curScroll = $(window).scrollTop();
  });
});

What happens here is, when the scrolling is done, the script checks if the scroll has been done down or up. If the scroll has been only down, then it shows the down arrow.

like image 76
Praveen Kumar Purushothaman Avatar answered Oct 25 '22 00:10

Praveen Kumar Purushothaman