【问题标题】:How to slow down scroll to top speed?如何减慢滚动到最高速度?
【发布时间】:2020-10-03 04:25:04
【问题描述】:

我有以下从 w3schools 获得的简单 scrollTop() 函数。我遇到的问题是设置滚动时间。不同的人给出了不同的方法,每个人都从以下代码中删除了一行或所有行。我正在等待一个可以添加来设置滚动速度并且不会删除其他文本的功能。这是代码笔工作https://codepen.io/vkdatta27/pen/zYqQbmM

var mybutton = document.getElementById("myBtn");

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

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    mybutton.style.display = "block";
  } else {
    mybutton.style.display = "none";
  }
}

// When the user clicks on the button, scroll to the top of the document
function topFunction() {
  document.body.scrollTop = 0;
  document.documentElement.scrollTop = 0;
}
body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 20px;
}
#myBtn {
  display: none;
  position: fixed;
  bottom: 20px;
  right: 30px;
  z-index: 99;
  font-size: 18px;
  border: none;
  outline: none;
  background-color: red;
  color: white;
  cursor: pointer;
  padding: 15px;
  border-radius: 4px;
}
#myBtn:hover {
  background-color: #555;
}
html {
  scroll-behavior: smooth
}
<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
<div style="background-color:black;color:white;padding:30px">Scroll Down</div>
<div style="background-color:lightgrey;padding:30px 30px 2500px">This example demonstrates how to create a "scroll to top" button that becomes visible
  <strong>when the user starts to scroll the page</strong></div>

【问题讨论】:

  • 查看scrollTo函数,设置行为:'smooth',你需要确认你需要的浏览器兼容性:developer.mozilla.org/en-US/docs/Web/API/Element/scrollTo
  • @LahiruTM,我已经看到了。但我已经提到我只想在以下代码中添加内容。 Cam 你在下面的代码中添加了一些东西吗?
  • @emiliokyp, scroll behavior: smooth 已添加到 css 中。我想控制它的时间。看,例如,滚动顶部功能需要在 8 秒内发生
  • 在脚本部分加载 Jquery。然后在顶部函数中注释两行并将其放入。 $('html,body').animate({ scrollTop: 0 }, 'slow');

标签: javascript html jquery css


【解决方案1】:

这是一个纯 Javascript 解决方案。您可能需要删除 scroll-behavior: smooth 样式,因为这会中断慢速滚动。在 javascript scrollTo 函数中提供 milliseconds 中的第二个参数,函数将花费大量时间滚动到顶部。

从答案@https://stackoverflow.com/a/23844067引用的JS代码

var mybutton = document.getElementById("myBtn");
window.onscroll = function() {
  scrollFunction()
};

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    mybutton.style.display = "block";
  } else {
    mybutton.style.display = "none";
  }
}
// Bind your button click, scroll direction and effect speed
document.getElementById("myBtn").onclick = function() {
  scrollTo(0, 8000); // it will take 8 seconds to reach to top.

}

// Element to move, time in ms to animate
function scrollTo(element, duration) {
  var e = document.documentElement;
  if (e.scrollTop === 0) {
    var t = e.scrollTop;
    ++e.scrollTop;
    e = t + 1 === e.scrollTop-- ? e : document.body;
  }
  scrollToC(e, e.scrollTop, element, duration);
}

// Element to move, element or px from, element or px to, time in ms to animate
function scrollToC(element, from, to, duration) {
  if (duration <= 0) return;
  if (typeof from === "object") from = from.offsetTop;
  if (typeof to === "object") to = to.offsetTop;

  scrollToX(element, from, to, 0, 1 / duration, 20, easeOutCuaic);
}

function scrollToX(element, xFrom, xTo, t01, speed, step, motion) {
  if (t01 < 0 || t01 > 1 || speed <= 0) {
    element.scrollTop = xTo;
    return;
  }
  element.scrollTop = xFrom - (xFrom - xTo) * motion(t01);
  t01 += speed * step;
  debugger;
  setTimeout(function() {
    scrollToX(element, xFrom, xTo, t01, speed, step, motion);
  }, step);
}

function easeOutCuaic(t) {
  t--;
  return t * t * t + 1;
}
body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 20px;
}

#myBtn {
  display: none;
  position: fixed;
  bottom: 20px;
  right: 30px;
  z-index: 99;
  font-size: 18px;
  border: none;
  outline: none;
  background-color: red;
  color: white;
  cursor: pointer;
  padding: 15px;
  border-radius: 4px;
}

#myBtn:hover {
  background-color: #555;
}
<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
<div style="background-color:black;color:white;padding:30px">Scroll Down</div>
<div style="background-color:lightgrey;padding:30px 30px 2500px">This example demonstrates how to create a "scroll to top" button that becomes visible
  <strong>when the user starts to scroll the page</strong></div>

【讨论】:

    【解决方案2】:

    您可以简单地使用添加平滑滚动

    html {
        scroll-behavior: smooth;
    }
    

    请注意,Safari 尚不支持它:/ (check here)

    还有Smooth ScrollFerdinandi 的 GitHub 存储库,我认为它有帮助,看看它和它的功能。

    【讨论】:

      【解决方案3】:

      动画滚动到 它可以找到。

      https://www.npmjs.com/package/animated-scroll-to

      animateScrollTo(el as Element, {
          elementToScroll: elContainer,
          speed: 100,
      }).then();
      

      【讨论】:

        猜你喜欢
        • 2011-08-01
        • 2018-04-25
        • 1970-01-01
        • 2023-02-06
        • 2012-04-24
        • 1970-01-01
        • 1970-01-01
        • 2017-02-20
        • 1970-01-01
        相关资源
        最近更新 更多