Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show or hide a menu when I scroll down or up?

enter image description here When I am scrolling down to hide this menu and when I am scrolling up to show this.

My menu bot is:

<script>
            var previousScroll = 0;
            $(window).scroll(function(event){
               var scroll = $(this).scrollTop();
               if (scroll > previousScroll){
                   $("menu-footer").filter(':not(:animated)').slideUp();
               } else {
                  $("menu-footer").filter(':not(:animated)').slideDown();
               }
               previousScroll = scroll;
            });
    </script>

    <section id="menu-footer">
        <ul>
            <li>
                <li><a href="javascript:history.back()"><i class="fa fa-arrow-circle-left"></i><?php _e("Back", ET_DOMAIN); ?></a></li>
            </li>
            <li>
                <a class="<?php echo $nearby_active; ?>" href="#" id="search-nearby"><i class="fa fa-compass"></i><?php _e("Nearby", ET_DOMAIN); ?></a>
                <form id="nearby" action="<?php echo get_post_type_archive_link('place')  ?>" method="get" >
                    <input type="hidden" name="center" id="center_nearby" />
                </form>
            </li>
            <!--<li><a href="#"><i class="fa fa-plus"></i>Submit</a></li>-->
            <!--<li>
                <a class="<?php echo $review_active; ?>" href="<?php echo et_get_page_link('list-reviews') ?>">
                    <i class="fa fa-comment"></i><?php _e("Reviews", ET_DOMAIN); ?>
                </a>
            </li>-->
            <li><a class="<?php echo $post-place; ?>" href="<?php echo et_get_page_link('post-place')?>"><i class="fa fa-flag-checkered"></i><?php _e("Post an Ad", ET_DOMAIN); ?></a></li>
            <?php if(has_nav_menu('et_mobile_header')) { ?>
            <li>
                <li><a href="#" class="search-btn"><i class="fa fa-search-plus"></i><?php _e("Search", ET_DOMAIN); ?></a></li>
            </li>       
            <li>                
                <a href="javascript:history.back()"><i class="fa fa-refresh"></i><?php _e("Refresh", ET_DOMAIN); ?></a>
            </li>
            <?php } ?>
        </ul>
    </section>

The script above is what I try to use for hiding my menu. My CSS for menu-footer is:

#menu-footer {
    width: 100%;
    background: #5f6f81;
    position: fixed;
    bottom: 0;
    transition: top 0.2s ease-in-out;
    z-index: 100
}

What am I missing to make this script working? If you have another solution for me it will be helpful.

like image 202
Vladut Avatar asked Apr 06 '16 10:04

Vladut


1 Answers

I made this first example in plain Javascript to let it easy to understand with a quick look in the code. It hides the menu changing the 'bottom' attribute of the CSS style (from 0 to -100) according to the scrollbar's position (when the scrollbar is more than 0 pixels from the top). The menu shows up again (from -100 to 0) if the scrollbar comes back to the top (0px). A CSS transition effect animates the change:

window.addEventListener("scroll", bringmenu);

function bringmenu() {
    if (document.body.scrollTop > 0 || document.documentElement.scrollTop > 0) {
        document.getElementById("bottommenu").style.bottom = "-100%";
    } else {
        document.getElementById("bottommenu").style.bottom = "0";
    }
}
body {
  margin: 0;
  background: lavender;
}

#bottommenu {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: auto;
  background: tomato;  
  -webkit-transition: bottom 2s;
  transition: bottom 2s;
}
<div id=content>
<p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p>
</div>

<div id=bottommenu>
<span>bottom </span><span>bottom </span><span>bottom </span><span>bottom </span><br><span>bottom </span><span>bottom </span><span>bottom </span><span>bottom </span>
</div>

Update: As requested on the comments, this second snippet brings/hides the menu when scrolling up/down, regardless of the bar's current position (to find the direction, when the scroll is activated it compares the current position with the previous position then stores the current position in a variable to be compared in the next scroll event):

var lastScrollTop = 0;

window.addEventListener("scroll", function(){  
   var st = window.pageYOffset || document.documentElement.scrollTop;  
   if (st > lastScrollTop){
       document.getElementById("bottommenu").style.bottom = "-100%";
   } else {
      document.getElementById("bottommenu").style.bottom = "0";
   }
   lastScrollTop = st;
}, false);
body {
  margin: 0;
  background: honeydew;
}

#bottommenu {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: auto;
  background: hotpink;  
  -webkit-transition: bottom 2s;
  transition: bottom 2s;
}
<div id=content>
<p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p>
</div>

<div id=bottommenu>
<span>bottom </span><span>bottom </span><span>bottom </span><span>bottom </span><br><span>bottom </span><span>bottom </span><span>bottom </span><span>bottom </span>
</div>

scroll direction code by @Prateek

like image 90
Le____ Avatar answered Oct 05 '22 16:10

Le____