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

How to change scroll behavior (e.g. speed, acceleration) on website?

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

很遗憾,我完全没有基础可以给你提供代码,希望你仍然能帮助我。也许你可以给我推荐一些js插件?

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

您有两种方法可以实现。您可以使用 CSS

html { scroll-behavior: smooth; }

或者您可以使用 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' 
})

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

您只需使用 jquery 即可。

实际上是css 属性造成了延迟,所以你可以用它来调整延迟。

  transition-duration: 500ms;

参考:

The transition-duration CSS property sets the length of time a transition animation should take to complete. By default, the value is 0s, meaning that no animation will occur.

$(document).on('mousemove', (event) => {
  $('.cursor').css({
    top: event.clientY,
    left: event.clientX,
  });
});
.cursor {
  transition-timing-function: ease-out;
  background-color: red;
  border-radius: 5px;
  height: 10px;
  transition-duration: 500ms;
  transform: translateX(-50%) translateY(-50%);
  position: fixed;
  width: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cursor"></div>

你可以试试这个:Nicescroll jQuery Plugin.

这个库实现了加速滚动。下载并导入脚本,然后添加以下内容:

$("body").niceScroll();

使用 jQuery,从按钮 (class="to-id") 滚动到标题 (id="myID")。

<input class="to-id" name="contact_owner" value="Contact" />

<h1 id="myID">


<!-- scroll to ID -->
<script>            
    jQuery(document).ready(function() {
    
        jQuery('.to-id').click(function(event) {
            event.preventDefault();
            $('html, body').animate({scrollTop:$('#myID').position().top}, 1000);
            return false;
        })
    });
</script>