1

Как скрыть див при прокрутки скролл на вниз? А вверх показать?

body {
  padding: 0;
  margin: 0;
  height: 2000px;
}
.fixed {
  width: 100%;
  height: 50px;
  text-align: center;
  padding-top: 25px;
  background: #555;
  position: fixed;
  bottom: 0;
  color: #FFF;
}
<div class="fixed">
Fixed Div
</div>

1

1 ответ 1

5

Используем событие window.onscroll

var oldScrollY = 0;
var div = document.getElementById("fixedDiv");

window.onscroll = function() {
  var scrolled = window.pageYOffset || document.documentElement.scrollTop;
  var dY = scrolled - oldScrollY;
  
  if ( dY > 0 ){
    div.className = "fixed fixed-bottom";
  } else {
    div.className = "fixed fixed-top";
  }
  
  oldScrollY = scrolled;
}
body {
  padding: 0;
  margin: 0;
  height: 2000px;
}
    
.fixed {
  width: 100%;
  height: 50px;
  text-align: center;
  padding-top: 25px;
  background: #555;
  position: fixed;
  color: #FFF;
  -webkit-transition: bottom .2s ease-out 0s;
  -moz-transition: bottom .2s ease-out 0s;
  -o-transition: bottom .2s ease-out 0s;
  transition: bottom .2s ease-out 0s;
}

.fixed-top{
  bottom: 0;
}

.fixed-bottom{
  bottom: -75px;
}
<div id="fixedDiv" class="fixed fixed-top">
    Fixed Div
</div>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.