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

pywebview:用Python+Web技术打造轻量级桌面应用!

pywebview:用Python+Web技术打造轻量级桌面应用!

✍️作者:唐叔在学习 💡专栏:唐叔学python ✨关键词:Python桌面开发、pywebview教程、WebView应用、前后端分离、JS与Python交互、桌面应用打包、Electron替代方案、Python GUI 大家好,我是唐叔。今天我们来聊聊一个非常轻量且强大的Python库——pywebview。如果你曾经为开发一个简单的桌面应用而纠结于Electron的笨重、PyQt的复杂,或是Tkinter的界面简陋,那pywebview或许正是你一直在找的解决方案。 文章目录 * 一、介绍 * 二、安装 * 安装全量版本 * 安装指定环境版本 * 三、使用入门 * 3.1 基本使用 * 3.2 应用程序架构 * 纯网络服务架构 * 无服务器架构 * 3.3 JS与Python交互 * 四、应用打包 * 五、常见使用场景 * 5.1 文件操作 * 文件下载

前端 SSG:别让你的网站加载速度慢得像蜗牛

前端 SSG:别让你的网站加载速度慢得像蜗牛 毒舌时刻 这网站加载速度慢得能让我泡杯咖啡回来还没好。 各位前端同行,咱们今天聊聊前端 SSG(静态站点生成)。别告诉我你还在使用纯客户端渲染,那感觉就像在没有预加载的情况下开车——能开,但起步慢得要命。 为什么你需要 SSG 最近看到一个项目,每次加载都要重新获取数据,用户体验差。我就想问:你是在做网站还是在做实时应用? 反面教材 // 反面教材:纯客户端渲染 // App.jsx import React, { useState, useEffect } from 'react'; function App() { const [posts, setPosts] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { async function fetchPosts() { setLoading(

Android 集成 WebRTC VAD:从原理到实践的声音活动检测指南

快速体验 在开始今天关于 Android 集成 WebRTC VAD:从原理到实践的声音活动检测指南 的探讨之前,我想先分享一个最近让我觉得很有意思的全栈技术挑战。 我们常说 AI 是未来,但作为开发者,如何将大模型(LLM)真正落地为一个低延迟、可交互的实时系统,而不仅仅是调个 API? 这里有一个非常硬核的动手实验:基于火山引擎豆包大模型,从零搭建一个实时语音通话应用。它不是简单的问答,而是需要你亲手打通 ASR(语音识别)→ LLM(大脑思考)→ TTS(语音合成)的完整 WebSocket 链路。对于想要掌握 AI 原生应用架构的同学来说,这是个绝佳的练手项目。 从0到1构建生产级别应用,脱离Demo,点击打开 从0打造个人豆包实时通话AI动手实验 Android 集成 WebRTC VAD:从原理到实践的声音活动检测指南 在移动应用开发中,实时语音处理能力已经成为许多场景的刚需。无论是语音助手、视频会议还是智能家居控制,

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

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