1

Есть сайт на wordpress. Не могу сделать такой функционал:

при скроллинге страницы - div появляется,

после того как пользователь перестал скроллить страницу - div сразу прячется.

Пытался сделать путём добавления класса, класс при скролле появляется, но потом не пропадает... Помогите, пожалуйста.

jQuery(window).scroll(function() { 
jQuery('.one-page-down').addClass('visible-buttons');
});

2 ответа 2

2

Добиться такого поведения можно с помощью setTimeout.

Посмотрите пример.

var timeoutId = null;

function hide() {
  $('.one-page-down').removeClass('visible-buttons');
  timeoutId = null;
}
$(window).scroll(function() {
  $('.one-page-down').addClass('visible-buttons');
  if (timeoutId)
    clearTimeout(timeoutId);
  timeoutId = setTimeout(hide, 500); // будет скрываться через 500 мс
});
.one-page-down {
  position: fixed;
  top: 0;
  left: 50px;
  background-color: red;
  display: none;
}

.one-page-down.visible-buttons {
  display: block;
}

.content {
  height: 2000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="one-page-down">DOWN</div>

<div class="content"></div>

1
2

$.fn.scrollStopped = function(callback) {
  var that = this, 
     $this = $(that);
  
  $this.scroll(function(ev) {
    clearTimeout($this.data('scrollTimeout'));
    $this.data('scrollTimeout', setTimeout(callback.bind(that), 250, ev));
    $("#visiblescroll").css({"opacity":"1"});
  });
  
};

$(window).scrollStopped(function(ev){  
  $("#visiblescroll").css({"opacity":"0"});
});
#visiblescroll {
    width: 200px;
    height: 200px;
    background: #5fba7d;
    opacity: 0;
    position: fixed;
    top: 5%;
    left: 5%
    transition: 0.4s;
    display: flex;
    text-align: center;
    align-items: center;
    color: white;
    justify-content: center;
    text-transform: uppercase;
    font-size: 21px;
}
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body style="height:3000px">


<div id="visiblescroll"> hello </div>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</body>
</html>

Ваш ответ

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

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