0

The code below is what I currently trying to make. As the result you can see just a bunch of buttons, I want to hide the buttons while the user is scrolling. Cause I can't insert code with code snippet :( so I just paste here:

$(function() {
  $(window).on('scroll', function() {
    $('ul.side-sticky-nav').addClass('hide-side-sticky-nav');
    setTimeout(function() {
      $('ul.side-sticky-nav').removeClass('hide-side-sticky-nav');
    }, 150);
  });
});
ul.side-sticky-nav {
  list-style-type: none;
  padding: 0;
  width: 50px;
  margin: 8px;
  position: fixed;
  right: 0;
  bottom: 10%;
  -webkit-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}

ul.side-sticky-nav li {
  width: 100%;
  height: 0;
  padding-bottom: 100%;
  border-radius: 100%;
  margin: 8px 0;
  background: blue;
}

.hide-side-sticky-nav {
  right: -40px!important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="height: 2000px; background: gray;">
  <ul class="side-sticky-nav">
    <li>
      <a></a>
    </li>
    <li>
      <a></a>
    </li>
  </ul>
</div>

What we should pay attention when using animation with js? Thank you!

Chaska
  • 3,165
  • 1
  • 11
  • 17
hayley
  • 384
  • 5
  • 17

2 Answers2

0

This is an easier way to detect stop scrolling event for your case.

$(function() {

  var st = $(window).scrollTop();
  setInterval(function() {
    if (st != $(window).scrollTop()) {
      $('ul.side-sticky-nav').addClass('hide-side-sticky-nav');
    } else {
      $('ul.side-sticky-nav').removeClass('hide-side-sticky-nav');
    }
    st = $(window).scrollTop();
  } ,300);

});
ul.side-sticky-nav {
  list-style-type: none;
  padding: 0;
  width: 50px;
  margin: 8px;
  position: fixed;
  right: 0;
  bottom: 10%;
  -webkit-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}

ul.side-sticky-nav li {
  width: 100%;
  height: 0;
  padding-bottom: 100%;
  border-radius: 100%;
  margin: 8px 0;
  background: blue;
}

.hide-side-sticky-nav {
  right: -40px!important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="height: 2000px; background: gray;">
  <ul class="side-sticky-nav">
    <li>
      <a></a>
    </li>
    <li>
      <a></a>
    </li>
  </ul>
</div>
Chaska
  • 3,165
  • 1
  • 11
  • 17
0

Maybe you could apply debounce to it with at_begin in true

$(function() {
 function() toggleNav{
    const nav = $('ul.side-sticky-nav')
    if (nav.hasClass('hide-side-sticky-nav') {
      $('ul.side-sticky-nav').removeClass('hide-side-sticky-nav');
    }
    else {
      $('ul.side-sticky-nav').addClass('hide-side-sticky-nav');
    }
  }

  $(window).on('scroll', debounce(250, true, toggleNav));
});

this will hide the nav right after user start to scroll and won't show it again until 250 ms after it stops scrolling

You could use (at least) one of this to have the debounce function:

Their api is nearly the same

kliank
  • 31
  • 5