Javascript 屏幕滚动显示时隐藏按钮,向上滚动时显示
我有这样一个代码:Javascript 屏幕滚动显示时隐藏按钮,向上滚动时显示,javascript,php,html,jquery,Javascript,Php,Html,Jquery,我有这样一个代码: <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 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;
}
});
对不起,你能用我的id和html代码编写javascript吗?