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

小白入门:前端前端调用 AI 接口全流程(附具体案例)

很多前端新手在调用 AI 接口时会犯怵:不知道 “怎么怎么传参数?”“流式响应怎么处理?”“不同功能(润色 / 扩写)调用方式不一样吗?” 其实很简单!本文以 “智能文本处理工具” 为例,手把手教你从 0 到 1 调用 AI 接口,包含润色、扩写等功能,看完就能上手。 准备工作:先看懂这 3 个核心文件 在开始前,我们需要明确项目中 3 个关键文件的作用(这些文件你可能已经有了,只是不知道怎么用): * vite.config.js:配置后端接口代理,解决跨域问题 * apiClient.js:封装好的 HTTP 请求工具,帮你发请求 * aiService.js:封装好的 AI 功能函数(

前端响应式布局实现方案

前端响应式布局实现方案

一、 什么是响应式布局 响应式布局是一种面向多终端的网页设计与实现方法,其核心目标是使网页能够根据访问设备的屏幕物理尺寸、分辨率、屏幕方向及视口宽度等关键参数,自动调整页面的布局结构、元素尺寸、内容排版及交互组件的展示形态。 该方法通过统一的代码基座,确保网页在桌面端、平板端、移动端等不同终端上均能提供一致性、可用性与适配性俱佳的用户体验,无需为各终端单独设计和维护独立的网页版本,从而降低开发与迭代成本,提升跨终端访问的兼容性与稳定性。 二、 响应式布局的核心特点 1. 多终端自适应 基于设备的屏幕尺寸、分辨率、方向等参数自动调整页面结构与样式,无需为不同终端开发独立版本,实现一套代码适配全场景。 2. 弹性化元素设计 页面元素采用相对单位(如百分比、rem、vw/vh)替代固定像素值,可随容器或视口大小按比例缩放,保证在不同尺寸屏幕下的显示协调性。 3. 断点式样式切换 通过 CSS 媒体查询技术设定关键断点,在不同断点区间加载对应的样式规则,使页面布局在特定屏幕尺寸下发生合理变化,匹配设备的交互习惯。 4. 内容优先级适配 根据终端屏幕大小智能调整内容的展

Webots R2023b 完整安装配置教程

Webots R2023b 完整安装配置教程 声明:本教程由豆包、ChatGPT等AI工具协助完成。 本教程讲解如何安装 Python3、包管理器 Micromamba、必要依赖包(如 opencv-python),以及 Webots 仿真软件,并完成 Micromamba Python 环境与 MATLAB 地址的配置,适用于 Windows、macOS 双系统。 一、前置说明 1. 适用场景:需要使用 Webots 进行仿真开发,同时依赖 Python 进行脚本编写、OpenCV 进行图像处理,通过 Micromamba 管理 Python 环境,并关联 MATLAB 路径用于联合开发。 2. 版本约定(兼容性最优): * Python:

【前端实战】构建 Vue 全局错误处理体系,实现业务与错误的清晰解耦

【前端实战】构建 Vue 全局错误处理体系,实现业务与错误的清晰解耦

目录 【前端实战】构建 Vue 全局错误处理体系,实现业务与错误的清晰解耦 一、为什么要做全局错误处理? 1、将业务逻辑与错误处理解耦 2、为监控和埋点提供统一入口 二、Vue 中的基础全局错误处理方式 1、Vue 中全局错误处理写法 2、它会捕获哪些错误? 3、它不会捕获哪些错误? 4、errorHandler 的参数含义 三、全局错误处理的进阶设计 1、定义“可识别的业务错误” 2、在 errorHandler 中做真正的“分类处理” 3、补齐 Promise reject 的捕获能力 4、错误处理的策略化封装 四、结语         作者:watermelo37         ZEEKLOG优质创作者、华为云云享专家、阿里云专家博主、腾讯云“