英文:
After scroll down button for go to the top of the website will show smoothly
问题
我需要帮助处理我的滚动返回顶部按钮。我设置了按钮在向下滚动200像素后显示,但我不知道如何使按钮平滑显示或如何进行动画设置。你能帮助我吗?
谢谢。
let mybutton = document.getElementById("btnScrollToTop");
window.onscroll = function() {
scrollFunction()
};
function scrollFunction() {
if (document.body.scrollTop > 200 || document.documentElement.scrollTop > 200) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
const btnScrollToTop = document.querySelector("#btnScrollToTop");
btnScrollToTop.addEventListener("click", function() {
window.scrollTo({
top: 0,
left: 0,
behavior: "smooth"
})
});
#btnScrollToTop {
display: none;
position: fixed;
right: 10px;
bottom: 10px;
width: 50px;
height: 50px;
border-radius: 50%;
background: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.25);
color: #000;
font-size: 22px;
border: none;
outline: none;
cursor: pointer;
z-index: 1000;
}
#btnScrollToTop:hover {
color: #fff;
background-color: #ffb038;
border: 1px solid #000;
transition: all 0.5s ease-in-out;
}
<button id="btnScrollToTop">
<i class="fa-solid fa-arrow-up"></i>
</button>
英文:
I need help with my scrool to top button. I set that the button will show after I scroll down 200px but i don´t know how to set that button will show smoothly or how to animate it. Can you help me?
Thank you.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let mybutton = document.getElementById("btnScrollToTop");
window.onscroll = function() {
scrollFunction()
};
function scrollFunction() {
if (document.body.scrollTop > 200 || document.documentElement.scrollTop > 200) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
const btnScrollToTop = document.querySelector("#btnScrollToTop");
btnScrollToTop.addEventListener("click", function() {
window.scrollTo({
top: 0,
left: 0,
behavior: "smooth"
})
});
<!-- language: lang-css -->
#btnScrollToTop {
display: none;
position: fixed;
right: 10px;
bottom: 10px;
width: 50px;
height: 50px;
border-radius: 50%;
background: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.25);
color: #000;
font-size: 22px;
border: none;
outline: none;
cursor: pointer;
z-index: 1000;
}
#btnScrollToTop:hover {
color: #fff;
background-color: #ffb038;
border: 1px solid #000;
transition: all 0.5s ease-in-out;
}
<!-- language: lang-html -->
<button id="btnScrollToTop">
<i class="fa-solid fa-arrow-up"></i>
</button>
<!-- end snippet -->
答案1
得分: 1
无法通过更改button
的display
属性来实现该效果,因为该属性不可动画化。
替代display
属性的一个快速方法,且可以进行动画处理的属性是使用visibility
属性。假设我们希望在button
出现和消失时有漂亮的淡入
和淡出
效果。
为此,我们需要使用opacity
属性以及visibility
属性:
- 最初,通过
visibility: hidden
规则将按钮隐藏,同时也应用opacity: 0
规则,以便在需要时使按钮平滑淡入。 - 一旦页面滚动超过
200px
,我们将通过将opacity
设置为1
(opacity: 1
)来编程地使按钮淡入,并将visibility
属性设置为visible
,以便我们的淡入效果可以平滑运行。 - 最后,当我们需要隐藏按钮时,比如当页面位于顶部时,我们将恢复
visibility
和opacity
属性分别为hidden
和0
,以获得漂亮的淡出效果。
为了稍微优化一下,我们将声明一个新的CSS
类,我们将其称为visible
,当需要使按钮淡入时,我们将其应用于button
,并在需要使按钮淡出时将其移除(这个类),借助这个类,我们只需将其应用于按钮或从按钮上移除它,不再需要在JavaScript
部分手动更改button
的样式。
以下是一个快速演示,请尝试向下滚动一点,您将看到button
的淡入效果,然后尝试单击它或手动滚动回到顶部以查看淡出效果:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const toTopBtn = document.getElementById('btnScrollToTop'),
scrollFunction = () => toTopBtn.classList[document.documentElement.scrollTop > 200 ? 'add' : 'remove']('visible');
window.addEventListener('scroll', scrollFunction);
toTopBtn.addEventListener('click', () => window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth'
}));
<!-- language: lang-css -->
/* 仅用于展示按钮上的显示/隐藏,我们在页面上添加了一些滚动区域 */
body {
height: 300vh;
border: 2px solid red;
}
#btnScrollToTop {
visibility: hidden; /* 初始隐藏 */
opacity: 0; /* 初始设置为 "0" 不透明度 */
position: fixed;
right: 10px;
bottom: 10px;
width: 50px;
height: 50px;
border-radius: 50%;
background: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.25);
color: #000;
font-size: 22px;
border: none;
outline: none;
cursor: pointer;
z-index: 1000;
transition: all .4s 0s ease; /* 需要良好的淡入/淡出过渡 */
}
#btnScrollToTop:hover {
color: #fff;
background-color: #ffb038;
border: 1px solid #000;
transition: all 0.5s ease-in-out;
}
/* 我们的新类,允许淡入/淡出效果 */
#btnScrollToTop.visible {
visibility: visible;
opacity: 1;
}
<!-- language: lang-html -->
<button id="btnScrollToTop">Top</button>
<!-- end snippet -->
[1]: https://developer.mozilla.org/en-US/docs/Web/CSS/display#formal_definition
[2]: https://developer.mozilla.org/en-US/docs/Web/CSS/visibility
[3]: https://developer.mozilla.org/en-US/docs/Web/CSS/opacity
英文:
You can't achieve that by changing the button
's display
property as that property is NOT animatable.
A quick alternative to the display
property, and that can be animatable, is to use the visibility
property. Let's say we'll have a nice fade in
and fade out
effects on the button
when that later appears and disappears respectively.
To do so, we'll need to use the opacity
property along with the visibility
property:
- Initially, the button is hidden using
visibility: hidden
rule and also to have a fade in/out effect we'll initially applyopacity: 0
rule as well so the button can fade in smoothly when we want to. - Once the page scrolls after
200px
we'll programmatically fade in the button by applying anopacity
of1
(opacity: 1
) and also to set thevisibility
property tovisible
so our fade effect can run smoothly as we want. - Lastly, when we need to hide the button like when we're at the top of the page, we'll revert the
visibility
and theopacity
properties tohidden
and0
respectively so we get a nice fade out effect.
To optimize things a bit, we'll declare a new CSS
class, let's call it visible
, that we apply to the button
when we need it to fade in and remove it (the class) when we want the button
to fade out. With that class we only need to apply it or remove it from the button and we no longer need to change the button
's styles manually on the JavaScript
part.
Here's a quick demo, try to scroll down a bit and you'll see the fade in effect on the button
and try to click it or manually scroll back up to see the fade out effect:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const toTopBtn = document.getElementById('btnScrollToTop'),
scrollFunction = () => toTopBtn.classList[document.documentElement.scrollTop > 200 ? 'add' : 'remove']('visible');
window.addEventListener('scroll', scrollFunction);
toTopBtn.addEventListener('click', () => window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth'
}));
<!-- language: lang-css -->
/* only to showcase the hide/show on the button we add some scrolling region to the page */
body {
height: 300vh;
border: 2px solid red;
}
#btnScrollToTop {
visibility: hidden;
/* initially hidden */
opacity: 0;
/* initially set to "0" opacity */
position: fixed;
right: 10px;
bottom: 10px;
width: 50px;
height: 50px;
border-radius: 50%;
background: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.25);
color: #000;
font-size: 22px;
border: none;
outline: none;
cursor: pointer;
z-index: 1000;
transition: all .4s 0s ease;
/* needed to have a nice fade in/out transitions */
}
#btnScrollToTop:hover {
color: #fff;
background-color: #ffb038;
border: 1px solid #000;
transition: all 0.5s ease-in-out;
}
/* our new class that will allow the fade in/out effect */
#btnScrollToTop.visible {
visibility: visible;
opacity: 1;
}
<!-- language: lang-html -->
<button id="btnScrollToTop">Top</button>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论