jquery - 有没有办法隐藏滚动元素并在用户停止滚动时显示?

标签 jquery html css

我有一个滚动指示器按钮,它在用户滚动时将 X 移出屏幕,并在用户向上滚动时显示回来。我希望按钮在用户停止滚动时出现/返回,而不是在他们向上滚动时出现。

我尝试切换类,但它的行为不正确。

jQuery:

var prev = 0;
var $window = $(window);

$window.on('scroll', function(){
    var scrollTop = $window.scrollTop();
    scrollButton.toggleClass('hidden', scrollTop > prev);
    prev = scrollTop;
});

CSS:

.hidden {
   transform: translateX(250%);
 }

最佳答案

您可以通过将超时用作阈值来实现这一点。

例如:

var timer;
$(document).scroll(function(){

  if(timer != "undefined"){
    clearTimeout(timer);
  }
  
  $('p').hide();
  timer = setTimeout(function(){
    
    $('p').show();

  },250)//Threshold is 100ms

});
<style>

body{height:1000px;width:100%;}
div{width:100%;height:150px;overflow:hidden;}
p{background-color:black;color:white;width:100px;margin:50px auto;padding:20px;}  

</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <p>Hide on scroll</p>
</div>

Note: Make sure that the section you want to hide is contained inside a div container with a overflow:hidden; property, Also use a fixed height, because every time that section is toggled the document will scroll automatically which will trigger the event again (debouncing).

我希望这有效!

关于jquery - 有没有办法隐藏滚动元素并在用户停止滚动时显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57891804/

相关文章:

java - jsp中显示有限行数

javascript - 垂直对齐和具有动态内容和高度的元素

javascript - 重新解析从部分 View 创建的表单后,jQuery 提交事件未触发

html - 使 css 背景图像 (svg) 响应式

css - 如何使用 HTML 和 CSS 重新创建图像?

jquery - Knockout.js:无法解析 JSON 中的绑定(bind)

html - 仅在所有版本的 I.E 中页脚下方有空白

jquery - 某些页面中 Bootstrap 选项卡的闪烁

html - 自定义样式的单选按钮?

jquery - rails 4-jQuery : add/remove class only works in certain cases