滚动到顶部的按钮会平滑显示。

huangapple go评论68阅读模式
英文:

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(&quot;btnScrollToTop&quot;);

window.onscroll = function() {
  scrollFunction()
};

function scrollFunction() {
  if (document.body.scrollTop &gt; 200 || document.documentElement.scrollTop &gt; 200) {
    mybutton.style.display = &quot;block&quot;;
  } else {
    mybutton.style.display = &quot;none&quot;;
  }
}
const btnScrollToTop = document.querySelector(&quot;#btnScrollToTop&quot;);
btnScrollToTop.addEventListener(&quot;click&quot;, function() {
  window.scrollTo({
    top: 0,
    left: 0,
    behavior: &quot;smooth&quot;
  })
});

<!-- 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 -->

&lt;button id=&quot;btnScrollToTop&quot;&gt;
      &lt;i class=&quot;fa-solid fa-arrow-up&quot;&gt;&lt;/i&gt;
&lt;/button&gt;

<!-- end snippet -->

答案1

得分: 1

无法通过更改buttondisplay属性来实现该效果,因为该属性不可动画化

替代display属性的一个快速方法,且可以进行动画处理的属性是使用visibility属性。假设我们希望在button出现和消失时有漂亮的淡入淡出效果。

为此,我们需要使用opacity属性以及visibility属性:

  • 最初,通过visibility: hidden规则将按钮隐藏,同时也应用opacity: 0规则,以便在需要时使按钮平滑淡入。
  • 一旦页面滚动超过200px,我们将通过将opacity设置为1opacity: 1)来编程地使按钮淡入,并将visibility属性设置为visible,以便我们的淡入效果可以平滑运行。
  • 最后,当我们需要隐藏按钮时,比如当页面位于顶部时,我们将恢复visibilityopacity属性分别为hidden0,以获得漂亮的淡出效果。

为了稍微优化一下,我们将声明一个新的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 apply opacity: 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 an opacity of 1 (opacity: 1) and also to set the visibility property to visible 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 the opacity properties to hidden and 0 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(&#39;btnScrollToTop&#39;),
  scrollFunction = () =&gt; toTopBtn.classList[document.documentElement.scrollTop &gt; 200 ? &#39;add&#39; : &#39;remove&#39;](&#39;visible&#39;);

window.addEventListener(&#39;scroll&#39;, scrollFunction);

toTopBtn.addEventListener(&#39;click&#39;, () =&gt; window.scrollTo({
  top: 0,
  left: 0,
  behavior: &#39;smooth&#39;
}));

<!-- 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 &quot;0&quot; 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 -->

&lt;button id=&quot;btnScrollToTop&quot;&gt;Top&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月26日 23:46:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定