屏幕滚动时隐藏按钮,向上滚动时显示

问题描述

我有这样的代码:

<a href="/form" class="fixed-bottom" ><button type="submit" style="margin-left: auto;margin-right: auto;display: block; name="submit" class="btn btn-primary">...</button></a>

此问题固定在移动网页的下方 我想在页面向下滚动时隐藏按钮,并在向上滚动页面时显示

解决方法

您必须在此处使用javascript事件,才能检测页面上的滚动事件。

在下一个示例中,有一个初始滚动位置设置为0。您只需要将此值与页面的当前顶部距离进行比较即可。

var scrollPos = 0;
// adding scroll event
window.addEventListener('scroll',function(){
  // detects new state and compares it with the new one
  if ((document.body.getBoundingClientRect()).top > scrollPos) {
    // change here the style of your button

    console.log('going up');
  else {
    // change here the style of your button

    console.log('going down');
    // saves the new position for iteration.
    scrollPos = (document.body.getBoundingClientRect()).top;
  }
});

you have a working codepen here showing the example live

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...