前端动画:别再用 jQuery animate 了

前端动画:别再用 jQuery animate 了

毒舌时刻

这动画效果做得跟幻灯片似的,一点都不流畅。

各位前端同行,咱们今天聊聊前端动画。别告诉我你还在使用 jQuery animate,那感觉就像在没有减震器的情况下开车——能开,但颠簸得要命。

为什么你需要现代前端动画

最近看到一个项目,动画效果卡顿,代码复杂难以维护。我就想问:你是在做动画还是在做卡顿展示?

反面教材

// 反面教材:使用 jQuery animate // index.html <!DOCTYPE html> <html> <head> <title>jQuery Animation</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <style> .box { width: 100px; height: 100px; background-color: red; position: relative; } </style> </head> <body> <div></div> <button>动画</button> <script src="app.js"></script> </body> </html> // app.js $(document).ready(function() { $('#animate').click(function() { $('.box').animate({ left: '200px', opacity: '0.5', height: '200px', width: '200px' }, 1000); }); }); 

毒舌点评:这动画,就像在看幻灯片,一点都不流畅。

正确姿势

1. CSS 动画

/* 正确姿势:CSS 动画 */ /* styles.css */ .box { width: 100px; height: 100px; background-color: red; position: relative; transition: all 0.3s ease; } .box:hover { transform: scale(1.2); background-color: blue; } .box.animate { animation: move 1s ease forwards; } @keyframes move { from { left: 0; opacity: 1; height: 100px; width: 100px; } to { left: 200px; opacity: 0.5; height: 200px; width: 200px; } } /* index.html */ <!DOCTYPE html> <html> <head> <title>CSS Animation</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div></div> <button>动画</button> <script src="app.js"></script> </body> </html> /* app.js */ document.getElementById('animate').addEventListener('click', function() { const box = document.querySelector('.box'); box.classList.add('animate'); setTimeout(() => { box.classList.remove('animate'); }, 1000); }); 

2. Framer Motion

// 正确姿势:Framer Motion // 1. 安装依赖 // npm install framer-motion // 2. 基本使用 import React, { useState } from 'react'; import { motion } from 'framer-motion'; function App() { const [animate, setAnimate] = useState(false); return ( <div> <motion.div className="box" initial={{ x: 0, opacity: 1, height: 100, width: 100 }} animate={animate ? { x: 200, opacity: 0.5, height: 200, width: 200 } : {}} transition={{ duration: 1 }} whileHover={{ scale: 1.1, backgroundColor: 'blue' }} /> <button onClick={() => setAnimate(!animate)}> {animate ? '重置' : '动画'} </button> </div> ); } export default App; // styles.css .box { background-color: red; position: relative; } 

3. GreenSock Animation Platform (GSAP)

// 正确姿势:GSAP // 1. 安装依赖 // npm install gsap // 2. 基本使用 import React, { useRef, useEffect } from 'react'; import gsap from 'gsap'; function App() { const boxRef = useRef(null); const handleAnimate = () => { gsap.to(boxRef.current, { x: 200, opacity: 0.5, height: 200, width: 200, duration: 1, ease: 'power2.out' }); }; const handleReset = () => { gsap.to(boxRef.current, { x: 0, opacity: 1, height: 100, width: 100, duration: 1, ease: 'power2.out' }); }; return ( <div> <div ref={boxRef} className="box"></div> <button onClick={handleAnimate}>动画</button> <button onClick={handleReset}>重置</button> </div> ); } export default App; // styles.css .box { width: 100px; height: 100px; background-color: red; position: relative; } 

4. React Spring

// 正确姿势:React Spring // 1. 安装依赖 // npm install @react-spring/web // 2. 基本使用 import React, { useState } from 'react'; import { useSpring, animated } from '@react-spring/web'; function App() { const [animate, setAnimate] = useState(false); const styles = useSpring({ x: animate ? 200 : 0, opacity: animate ? 0.5 : 1, height: animate ? 200 : 100, width: animate ? 200 : 100, config: { duration: 1000 } }); return ( <div> <animated.div className="box" style={styles} /> <button onClick={() => setAnimate(!animate)}> {animate ? '重置' : '动画'} </button> </div> ); } export default App; // styles.css .box { background-color: red; position: relative; } 

毒舌点评:这才叫现代前端动画,流畅自然,代码简洁,再也不用担心动画卡顿和维护困难了。

Read more

实战Pi0机器人控制中心:轻松实现机器人智能操控

实战Pi0机器人控制中心:轻松实现机器人智能操控 1. 项目概述:重新定义机器人控制体验 Pi0机器人控制中心是一个基于先进视觉-语言-动作模型的智能操控平台,它彻底改变了传统机器人控制的复杂方式。这个项目将多视角视觉感知、自然语言理解和精准动作控制完美融合,让机器人操控变得像与人对话一样简单直观。 想象一下,你只需要对机器人说"捡起那个红色方块",它就能准确理解并执行相应动作。这就是Pi0控制中心带来的革命性体验——无需编写复杂的控制代码,无需记忆繁琐的操作指令,用最自然的方式与机器人进行交互。 这个控制中心采用全屏Web界面设计,界面简洁现代,操作流程直观。无论你是机器人技术爱好者、研究人员,还是教育工作者,都能快速上手使用,专注于机器人应用开发而不是底层技术实现。 2. 核心功能详解:智能操控的四大支柱 2.1 多视角视觉感知系统 Pi0控制中心支持同时输入三个不同角度的环境图像:主视角、侧视角和俯视角。这种多视角设计模拟了人类观察环境的自然方式,为机器人提供了全面的环境感知能力。 * 主视角摄像头:提供机器人正前方的视野,用于识别主要操作对象 * 侧视角

ROS1机器人SLAM系列(四):Gmapping算法详解与实战

ROS1机器人SLAM系列(四):Gmapping算法详解与实战 本文将深入讲解Gmapping算法的原理,并通过实战演示如何使用Gmapping进行2D激光SLAM建图。 1. Gmapping算法简介 1.1 什么是Gmapping? Gmapping是一种基于**粒子滤波(Rao-Blackwellized Particle Filter, RBPF)**的2D激光SLAM算法。它由Giorgio Grisetti等人于2007年提出,是ROS中最经典、应用最广泛的SLAM算法之一。 主要特点: * 基于粒子滤波的概率框架 * 适用于2D激光雷达 * 需要里程计信息 * 实现成熟,稳定可靠 * 适合中小规模室内环境 1.2 算法流程概述 Gmapping算法流程 里程计数据 运动预测 Motion Model 粒子集合更新 激光雷达数据 扫描匹配 Scan Matching 观测更新 Sensor Model 粒子权重计算 重采样 Resample 地图更新 2. 核心算法原理

OpenClaw 飞书机器人搭建流程

OpenClaw 飞书机器人搭建流程

OpenClaw 飞书机器人搭建流程 手把手教你搭建属于自己的飞书 AI 机器人! 一、创建企业自建应用 首先进入飞书开发者后台: 👉 https://open.feishu.cn/app 填写应用名称和描述,直接点击创建即可。 创建完成后,会自动生成 App ID 和 App Secret,这两个凭证后面配置 OpenClaw 时会用到,先记下来。 二、添加机器人能力 在应用详情页左侧菜单找到「机器人」,点击添加。 添加成功后,机器人就可以在飞书中被搜索和使用了。 三、开通消息权限 进入「权限管理」,找到 im: 相关权限,全部勾选。 ⚠️ 注意:以下这个权限建议不要勾选: 获取群组中所有消息(im:message.group_msg) 否则群里所有消息机器人都会收到并响应,会造成不必要的干扰。