徘徊时,如何减慢变换并防止元素的转移?

发布于 2025-02-08 17:15:18 字数 1968 浏览 1 评论 0原文

我正在尝试创建一个悬停时会减慢的选选。

我设法使“放慢脚步”分类,但是由于某种原因,徘徊时的元素会发生变化。

我四处搜索并了解它可能与变换原点有关,但无法弄清楚什么。

  • 预期行为:元素不会改变,并且会优雅地减慢(如果有道理)。

提前致谢!

<div class="wrapper">
<div class="track">
    <div class="content">
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
    </div>
</div>

CSS

<style lang="scss">
    :root {
        --animationTiming: 100s;
    }

    .wrapper {
        position: relative;
        width: 100vw;
        max-width: 100%;
        height: 200px;
        overflow: hidden;
    }

    .track {
        position: absolute;
        white-space: nowrap;
        will-change: transform;
        animation: marquee var(--animationTiming) linear infinite;

        &:hover {
            --animationTiming: 120s;
            transition: cubic-bezier(0.47, 0, 0.745, 0.715);
            transition-duration: 3s;
            transition: transform 500ms ease-in-out 25ms;
            transform-origin: top right;
        }
    }

    @keyframes marquee {
        from {
            transform: translateX(0);
        }
        to {
            transform: translateX(-50%);
        }
    }

    .content {
        display: flex;
    }

    .box {
        width: 500px;
        height: 500px;
        background-color: red;
        border: 1px solid #000;
    }
</style>

I'm trying to create a Marquee that slows down when hovered on.

I managed to get the "slowing down" sorted, but for some reason, the elements shift when hovered on.

I searched around and understand it probably has something to do with transform origin, but just can't figure out what.

  • Expected behavior: elements do not shift, and gracefully slows down (if that makes sense).

Thanks in advance!

<div class="wrapper">
<div class="track">
    <div class="content">
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
        <div class="box" />
    </div>
</div>

CSS

<style lang="scss">
    :root {
        --animationTiming: 100s;
    }

    .wrapper {
        position: relative;
        width: 100vw;
        max-width: 100%;
        height: 200px;
        overflow: hidden;
    }

    .track {
        position: absolute;
        white-space: nowrap;
        will-change: transform;
        animation: marquee var(--animationTiming) linear infinite;

        &:hover {
            --animationTiming: 120s;
            transition: cubic-bezier(0.47, 0, 0.745, 0.715);
            transition-duration: 3s;
            transition: transform 500ms ease-in-out 25ms;
            transform-origin: top right;
        }
    }

    @keyframes marquee {
        from {
            transform: translateX(0);
        }
        to {
            transform: translateX(-50%);
        }
    }

    .content {
        display: flex;
    }

    .box {
        width: 500px;
        height: 500px;
        background-color: red;
        border: 1px solid #000;
    }
</style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文