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

Whisper.Unity:在Unity中实现本地语音转文字的革命性方案

Whisper.Unity是一个突破性的开源项目,它将OpenAI的Whisper语音识别模型无缝集成到Unity3D环境中。基于whisper.cpp实现,这个项目支持约60种语言的语音转录和跨语言翻译,完全离线运行且免费开源,为开发者提供了前所未有的本地语音转文字解决方案。 【免费下载链接】whisper.unityRunning speech to text model (whisper.cpp) in Unity3d on your local machine. 项目地址: https://gitcode.com/gh_mirrors/wh/whisper.unity 项目核心技术亮点 完全离线的智能语音识别 想象一下,你的应用能够听懂用户说话,却不需要连接互联网!✨ Whisper.Unity最大的魅力在于它实现了完全离线的语音识别功能。这不仅保护了用户隐私,还大大降低了使用成本,让语音交互变得既安全又高效。 多平台无缝兼容能力 从Windows到MacOS,从Linux到移动端的iOS和Android,甚至是最新的VisionOS平台,Whisper.Unit

对于VScode中Copilot插件使用卡顿问题的解决办法

copilot卡顿主要是网络和内存占用原因。 VScode内存优化解决办法: 结合链接和我补充的基本都可以解决。 解决VSCode无缘无故卡顿的问题_vscode卡顿-ZEEKLOG博客 在VScode中打开setting.json文件,打开方法ctrl+shift+p,输入Preferences: Open User Settings (JSON), 然后添加如下代码: { "search.followSymlinks": false, "git.autorefresh": false, "editor.formatOnSave": false } 结合链接和我补充的基本都可以解决。 VScode代理问题: vscode copilot长时间没反应_vscode中copilot总是卡住-ZEEKLOG博客 配置代理的话两种方法,上面是一种,推荐两种结合起来用(不冲突) 还是在setting.json文件中,添加如下代码: { "http.proxy": "http://127.

国产复旦微FPGA+DSP 6U VPX处理板

国产复旦微FPGA+DSP 6U VPX处理板

* 概述 国产复旦微FPGA+DSP 6U VPX处理板,是一款标准6U VPX高性能信号处理板,板上FPGA选用复旦微公司JFM7VX690T36-FFG1761。该FPGA外挂2组四片DDR3 SDRAM,单组存储容量2GB;该芯片有丰富的I/O接口和资源,包括36对GTH等高速传输接口。 DSP处理器采用1片国防科大FT-6678处理器。FT-6678是目前业内处理能力最强大的专用处理器。单片最大处理能力为160 GFLOP,外挂4片DDR3 SDRAM,存储容量2GB,FT6678外部接口丰富,有包括RapidI/O、PCIe、I2C、SPI、UART等快慢速接口。 * 技术指标 1. DSP外挂4片DDR3 SDRAM ,4片组成64bit位宽,传输速率1066MT/S。 2. DSP程序加载模式使用SPI模式,flash芯片选用MT25QU256ABA8ESF-0SIT。 3. DSP外挂1片 SPI NOR FLASH,芯片选用MT25QU256ABA8ESF-0SIT,片选信号连接到DSP的SPICS1,用于存储数据。 4. DSP的时钟和上电复位等

小米智能家居接入Home Assistant深度技术指南:从通信协议到边缘部署

小米智能家居接入Home Assistant深度技术指南:从通信协议到边缘部署 【免费下载链接】ha_xiaomi_homeXiaomi Home Integration for Home Assistant 项目地址: https://gitcode.com/GitHub_Trending/ha/ha_xiaomi_home 引言:破解智能家居接入的三大痛点 在智能家居生态构建过程中,用户常面临以下核心挑战:如何解决设备控制延迟超过500ms的问题?为何部分设备频繁出现"离线-在线"状态波动?怎样实现跨品牌设备的统一状态同步?本文将从技术原理、问题诊断到方案优化三个维度,提供系统化解决方案,帮助用户构建稳定、高效的智能家居控制体系。 一、技术原理:智能家居通信架构解析 1.1 通信协议深度对比(基础) 智能家居设备与Home Assistant的通信主要依赖以下三种协议,其性能特征直接影响用户体验: 协议类型传输延迟带宽占用穿透能力安全性适用场景MQTT300-500ms低弱高云端控制本地MQTT50-150ms低弱中局域网设备CoAP100-200ms极低中低