前端动画:别再用 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

【前端高频面试题】 - TypeScript 篇,零基础入门到精通,收藏这篇就够了

【前端高频面试题】 - TypeScript 篇 1. 请解释 TypeScript 是什么?它与 JavaScript 的核心区别是什么? 面试回答需突出 TS 的核心价值(类型安全)和与 JS 的关键差异,结构清晰: * TypeScript 定义:TS 是 JavaScript 的超集(Superset),在 JS 语法基础上增加了静态类型系统,最终会编译为纯 JS 运行(支持所有 JS 环境),核心目标是提升代码可维护性、减少运行时错误。 * 与 JavaScript 的核心区别(分点对比): 1. 类型系统:TS 有静态类型(编译阶段检查类型,变量声明时需指定/推断类型);JS 是动态类型(

【前端实战】Axios 错误处理的设计与进阶封装,实现网络层面的数据与状态解耦

【前端实战】Axios 错误处理的设计与进阶封装,实现网络层面的数据与状态解耦

目录 【前端实战】Axios 错误处理的设计与进阶封装,实现网络层面的数据与状态解耦 一、为什么网络错误处理一定要下沉到 Axios 层 二、Axios 拦截器 interceptors 1、拦截器的基础应用 2、错误分级和策略映射的设计 3、错误对象标准化 三、结语         作者:watermelo37         ZEEKLOG优质创作者、华为云云享专家、阿里云专家博主、腾讯云“创作之星”特邀作者、火山KOL、支付宝合作作者,全平台博客昵称watermelo37。         一个假装是giser的coder,做不只专注于业务逻辑的前端工程师,Java、Docker、Python、LLM均有涉猎。 --------------------------------------------------------------------- 温柔地对待温柔的人,包容的三观就是最大的温柔。 --------------------------------------------------------------------- 【前

手把手教你:用AI高效辅助编写测试用例

手把手教你:用AI高效辅助编写测试用例

大家好。在AI辅助测试这条路上,我走了2年多了。从最初的怀疑、试探,到如今它已成为我工作中不可或缺的“高效助理”。 刚开始,我也是觉得AI生成的用例挺“不靠谱”的,要么过于宽泛,要么就脱离实际业务。但随着不断对大型语言模型的使用和对提示词的不断优化,我发现,问题不在于AI,而在于我们是否“会提问”。 就拿一个最直观的改变来说吧:过去,一个中等复杂度的功能模块(例如,电商的商品发布流程),我可能需要花费一个下午(约4小时)来梳理逻辑、分析产品需求、编写用例;现在,通过借助AI,就能在15分钟内生成一份相当全面的用例初稿,再花大约45分钟进行精修和补充,总耗时1小时左右,效率提升了差不多75%。 这节省下来的3个小时,可以让我进行更有价值的探索性测试和自动化脚本的预研回归。 这不仅仅是时间的节省。更重要的是,AI能模拟一个就像“不知疲倦、知识广博”的测试工程师,能穷举出很多我们因自我的思维定式或时间的压力而容易忽略的边界条件和异常测试场景。在我负责的一个项目中,通过AI辅助设计的测试用例,我们提前发现了3个由第三方接口超时可能引发的数据不一致性缺陷,避免了上线后的潜在风险。 接下

webdav-server 终极指南:轻量级WebDAV服务器完整教程

在现代数字化办公环境中,文件共享和远程访问已成为日常工作的重要需求。webdav-server作为一个轻量级WebDAV服务器实现,提供了简单而强大的文件共享解决方案。本文将为您全面解析webdav-server的核心功能、部署方法和实战应用技巧。 【免费下载链接】webdavSimple Go WebDAV server. 项目地址: https://gitcode.com/gh_mirrors/we/webdav 为什么选择webdav-server?核心价值解析 webdav-server是一个基于Go语言开发的独立WebDAV服务器,具有以下核心优势: 🚀 轻量高效:单二进制文件部署,资源占用极低 🔒 安全可靠:支持TLS加密传输和多种认证方式 📁 跨平台兼容:支持Windows、Linux、macOS等主流操作系统 👥 权限精细控制:可配置用户级权限和目录访问规则 与传统的FTP或Samba共享相比,WebDAV协议提供了更丰富的文件操作功能和更好的集成性,特别适合需要Web界面访问或与办公软件集成的场景。 3步快速部署webdav-server 步