javascript - 动画淡入淡出固定返回顶部按钮

标签 javascript jquery html css

我有一个向下链接,当用户单击时,该链接会将页面向下移动到网站的下一部分。当用户开始滚动时,如何使其淡入返回顶部按钮。还有没有办法将其固定到位。猜测这将通过 Jquery 完成,但不太确定。

<div class="down-link"><a href="#about" id="w-downlink"><i class="ss-navigatedown"></i></a></div>


.down-link {
    width:100%;
    height:50px;    
}

#w-downlink i {
    line-height: 42px;
    font-size: 24px;
    color: #fff;
    display: block;
    width: 24px;
    margin: 0 auto;
    margin-top:10px;
}

#w-downlink {
    height: 60px;
    width: 60px;
    background-color: #191919;
    background-color: rgba(20, 20, 20, 0.4);
    position:absolute;
    bottom:0;
    margin-bottom:30px;
    right:0;
    margin-right:20px;
    cursor: pointer;
    -webkit-transform: translate3d(0, 0, 0);
    opacity: 1;
}

.w-downlink:hover {
    height: 60px;
    width: 60px;
    background-color: #191919;
    background-color: rgba(20, 20, 20, 0.4);
    position:absolute;
    bottom:0;
    margin-bottom:30px;
    right:0;
    margin-right:20px;
     cursor: pointer;
    -webkit-transform: translate3d(0, 0, 0);
    opacity: 0.5;
}

最佳答案

这应该可以实现您想要的大部分效果;你不需要 jQuery。

页面顶部有一个按钮,当您开始滚动时它会发生变化;这只是 CSS position:fixed;

JS 只是监听 window 对象上的滚动事件。

我刚刚对其进行了编辑,通过添加 if(){} 语句来检查垂直滚动,一旦用户向上滚动页面,它也会变回来。

您可以将不同的元素转储到 div 中,而不是像我在这里所做的那样将字符串转储到内部 HTML 中。

如果您希望元素淡出,请查看 CSS 过渡。

您可以在滚动事件开始时更改它的类,或者使用 JavaScript 来完成这一切。

以下是 W3C 可能提供帮助的一些资源:

onscroll event documentation

css3 Transitions

var downLink = document.getElementById('down-link');
window.addEventListener("scroll", function(){//Provide a window listener
  if(window.scrollY){
    downLink.innerHTML = "Back to Top";
  }else{
    downLink.innerHTML = "Down Link";
  }
  
});
#down-link {
    position: fixed;
    top: 0;
    left: 0;
    border: 2px solid black /*Just to show the element bounds*/
    
  }

#userScrollElement {
    max-height:  500px;
    height: 800px;
    overflow: auto;
}
<div id="userScrollElement">
  <div id="down-link">Down Link
  </div>
</div>

关于javascript - 动画淡入淡出固定返回顶部按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39184337/

相关文章:

javascript - 使用 node-xmpp 在 Node 中写入 Google CCS 服务器时出错

javascript - 点击事件对象跟踪问题

jquery - 将鼠标悬停在链接上时更改 div 的背景图像

html - CSS Bootstrap 导航栏间距问题

android - 在 Android 中将 Cordova 与 Crosswalk 结合使用时使用自定义插件

javascript - 动态附加项目时,masonry 不会将它们布局出来

javascript - 无法让 Knockout 执行简单的 'options' 绑定(bind)

javascript - jQuery 中的 PHP (WordPress)

html - 填充顶部表现出奇怪的行为

javascript - TypeScript 中的 querySelectorAll 等效项