如何更改网站上的滚动行为(例如速度、加速度)?

jey*_*eyy 6 javascript scroll behavior motion parallax

网站上修改后的滚动行为是如何制作的?我想完成加速滚动行为,如您在示例中所见。所以你以一定的速度滚动,当你放手后,页面会自动滚动一点,减慢并停止。

不幸的是,我绝对没有为您提供代码的基础,我希望您仍然可以帮助我。也许你可以推荐我一些js插件?

https://p2mv.studio/case/sony-music-france

小智 5

您有两种方法可以实现这一目标。你可以使用CSS

html { scroll-behavior: smooth; }
Run Code Online (Sandbox Code Playgroud)

或者你可以使用 JavaScript:

// Scroll to specific values
// scrollTo is the same
window.scroll({
  top: 2500, 
  left: 0, 
  behavior: 'smooth'
});

// Scroll certain amounts from current position 
window.scrollBy({ 
  top: 100, // could be negative value
  left: 0, 
  behavior: 'smooth' 
});

// Scroll to a certain element
document.querySelector('.hello').scrollIntoView({ 
  behavior: 'smooth' 
})
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读更多内容并找到更多示例:https ://css-tricks.com/snippets/jquery/smooth-scrolling/

  • 这并没有回答OP关于速度和加速度的问题 (9认同)
  • 请注意,滚动函数中的设置行为受 jQuery 支持,而不是本机 Javascript。 (5认同)
  • 那么……只有2个选择吗?即时且“流畅”? (4认同)