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();
相关推荐
有点笨的蛋几秒前
JavaScript 中的面向对象编程:从基础到继承
前端·javascript
2509_94088022几秒前
Spring Cloud GateWay搭建
android·前端·后端
一千柯橘3 分钟前
Three.js 中的调试助手 OrbitControls + GUI
前端
一 乐3 分钟前
购物商城|基于SprinBoot+vue的购物商城系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端
izx8883 分钟前
ES6+ 核心语法精讲:让 JavaScript 更优雅、更强大
javascript
玥浛3 分钟前
ELK.js 实战:大规模图布局性能优化方案
前端
特级业务专家4 分钟前
React Fiber 和时间切片
前端
BlackWolfSky5 分钟前
React Native学习路径与资源推荐
javascript·学习·react native
z***D6486 分钟前
SpringBoot3+Springdoc:v3api-docs可以访问,html无法访问的解决方法
前端·html
www_stdio6 分钟前
JavaScript 面向对象编程:从原型到 Class 的演进
前端·javascript