web之利用延迟实现复杂动画、animation

文章目录


效果图


html

html 复制代码
<div class="container">
    <div class="ball"></div>
    <input type="range" min="0" max="1" step="0.01" />
</div>

style

css 复制代码
body {
    display: flex;
    justify-content: center;
}

.container {
    margin-top: 30px;
}

.ball {
    --delay: 0s;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background-color: #ff0000;
    margin-bottom: 50px;
    animation: move 1s var(--delay) linear forwards paused;
    /* animation-play-state: running; */
    /* animation-play-state: paused; */
}

@keyframes move {
    50% {
        transform: translate(100px) scale(1.5);
    }

    100% {
        transform: translate(200px) scale(1);
    }
}

JavaScript

javascript 复制代码
// js方式实现(不考虑此方案)
// let inp = document.querySelector('input');
// inp.oninput = function () {
//     console.log(inp.value);
// }

// 方式二(配合css实现)
let inp = document.querySelector('input'),
    ball = document.querySelector('.ball'),
    cal = () => ball.style.setProperty('--delay', `-${inp.value}s`);

inp.oninput = cal;
cal();
相关推荐
Penge66637 分钟前
JavaScript:BOM 详解
前端
Penge6661 小时前
Vite vs Webpack:前端构建工具原理与实战
前端
Penge6661 小时前
前端包管理工具:npm、Yarn、pnpm 快速入门
前端
Penge6662 小时前
Sass 了解
前端
Penge6662 小时前
Webpack 打包从 0 到 1 实战笔记
前端
Penge6662 小时前
Webpack 入门:是什么、为什么需要、怎么使用?
前端
Penge6662 小时前
Ant Design(Antd)了解
前端
wangruofeng3 小时前
同一行代码,百种渲染:Asuka、React Native 与 Flutter 跨平台渲染机制深度拆解
前端·github
爱学习的程序媛3 小时前
Python常用框架梳理
开发语言·后端·python·ai·web·后端框架·python框架
Penge6663 小时前
JavaScript 核心语法速通
前端