gpt4 book ai didi

javascript - DevEd 教程中的滚动功能不起作用

转载 作者:行者123 更新时间:2023-12-04 08:17:54 25 4
gpt4 key购买 nike

我看过这个教程 https://www.youtube.com/watch?v=oUSvlrDTLi4我在做和这里一样的事情,我只是用 ES6 标准替换了 var 来让。

function scroll(target, duration = 1000) {
target = document.querySelector(target) + window.scrollY;
let targetPos = terget.getBoundingClientRect().top;
let startPos = window.pageYOffset;
let distance = targetPos - startPos;
let startTime = null;

function animation(currentTime) {
if (startTime === null) startTime = currentTime;
let timeElapsed = currentTime - startTime;
let run = ease(timeElapsed, startPos, distance, duration);
window.scrollTo(0, run);
if (timeElapsed < duration) requestAnimationFrame(animation);
}

function ease(t, b, c, d) {
t /= d / 2;
if (t < 1) return c / 2 * t * t + b;
t--;
return -c / 2 * (t * (t - 2) - 1) + b;
}

requestAnimationFrame(animation);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

.a {
width: 100%;
height: 100vh;
background: green;
}

.b {
width: 100%;
height: 100vh;
background: yellow;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<div class="a">
<h1 onclick="scroll('.b', 1000)">Scroll</h1>
</div>
<div class="b">
<h1 onclick="scroll('.a', 1000)">Scroll</h1>
</div>
</body>

</html>

但是,它似乎不起作用。

最佳答案

  • 把函数名改成不与window.scroll冲突.
  • target应该是元素本身或 document.querySelector(target) .
  • target在函数的第二行拼写错误。

  • function myScroll(target, duration = 1000) {
    target = document.querySelector(target);
    let targetPos = target.getBoundingClientRect().top;
    let startPos = window.pageYOffset;
    let distance = targetPos - startPos;
    let startTime = null;

    function animation(currentTime) {
    if (startTime === null) startTime = currentTime;
    let timeElapsed = currentTime - startTime;
    let run = ease(timeElapsed, startPos, distance, duration);
    window.scrollTo(0, run);
    if (timeElapsed < duration) requestAnimationFrame(animation);
    }

    function ease(t, b, c, d) {
    t /= d / 2;
    if (t < 1) return c / 2 * t * t + b;
    t--;
    return -c / 2 * (t * (t - 2) - 1) + b;
    }

    requestAnimationFrame(animation);
    }
    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
    * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    }

    .a {
    width: 100%;
    height: 100vh;
    background: green;
    }

    .b {
    width: 100%;
    height: 100vh;
    background: yellow;
    }
    </style>
    </head>

    <body>
    <div class="a">
    <h1 onclick="myScroll('.b', 1000)">Scroll</h1>
    </div>
    <div class="b">
    <h1 onclick="myScroll('.a', 1000)">Scroll</h1>
    </div>
    </body>

    </html>

    关于javascript - DevEd 教程中的滚动功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65631642/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com