【问题标题】:Javascipt smooth scrolling experienceJavascript流畅的滚动体验
【发布时间】:2020-06-16 08:35:50
【问题描述】:

几天来,我一直在尝试重新创建此页面的平滑滚动:

http://thibaudallie.com/

并且没有成功。我在 awwwards 上到处都能看到这种效果,但我从未找到正确的解决方案。

这是我目前最好的曲目,但我认为这不是正确的...

我想要在“wheel”期间触发的东西。

<script>
        const body = document.body,
            scrollWrap = document.getElementsByClassName("smooth-scroll-wrapper")[0],
            height = scrollWrap.getBoundingClientRect().height - 1,
            speed = 0.04;

        let offset = 0;

        body.style.height = Math.floor(height) + "px";

        function smoothScroll() {
            offset += (window.pageYOffset - offset) * speed;

            let scroll = `translate3d(0px, ${offset * -1}px, 0)`;
            scrollWrap.style.transform = scroll;

            callScroll = requestAnimationFrame(smoothScroll);
        }

        smoothScroll();
    </script>

【问题讨论】:

  • 您好,欢迎来到 SO。请发布您尝试过的完整内容,并解释您面临的确切问题。
  • 你好,我的问题是大多数时候这种滚动(thibaudallie.com)效果会在控制台中返回详细信息。我怎样才能避开她。

标签: javascript html scroll mouseevent smooth-scrolling


【解决方案1】:

我在 codepen Smooth Page Scroll找到了这个

var html = document.documentElement;
var body = document.body;

var scroller = {
    target: document.querySelector("#scroll-container"),
    ease: 0.05, // <= scroll speed
    endY: 0,
    y: 0,
    resizeRequest: 1,
    scrollRequest: 0,
};

var requestId = null;

TweenLite.set(scroller.target, {
    rotation: 0.01,
    force3D: true
});

window.addEventListener("load", onLoad);

function onLoad() {    
    updateScroller();  
    window.focus();
    window.addEventListener("resize", onResize);
    document.addEventListener("scroll", onScroll); 
}

function updateScroller() {

    var resized = scroller.resizeRequest > 0;

    if (resized) {    
        var height = scroller.target.clientHeight;
        body.style.height = height + "px";
        scroller.resizeRequest = 0;
    }

    var scrollY = window.pageYOffset || html.scrollTop || body.scrollTop || 0;

    scroller.endY = scrollY;
    scroller.y += (scrollY - scroller.y) * scroller.ease;

    if (Math.abs(scrollY - scroller.y) < 0.05 || resized) {
        scroller.y = scrollY;
        scroller.scrollRequest = 0;
    }

    TweenLite.set(scroller.target, { 
        y: -scroller.y 
    });

    requestId = scroller.scrollRequest > 0 ? requestAnimationFrame(updateScroller) : null;
}

function onScroll() {
    scroller.scrollRequest++;
    if (!requestId) {
        requestId = requestAnimationFrame(updateScroller);
    }
}

function onResize() {
    scroller.resizeRequest++;
    if (!requestId) {
        requestId = requestAnimationFrame(updateScroller);
    }
}

【讨论】:

  • 感谢您的回答,这就是我要查找的内容,但控制台返回详细信息:“[Violation] 执行 JavaScript 时强制重排耗时 ms
  • [Violation] 执行 JavaScript 时强制重排耗时 ms
  • 谢谢,所以我将侦听器事件“滚动”更改为“轮子”,一切正常,但在某些时候会失去流动性,我不知道它来自哪里,“滚动在某个点跳跃“
  • 这不使用某种称为“TweenLite”的框架还是我弄错了?
猜你喜欢
  • 2014-12-06
  • 2018-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多