当前位置: 首页 > news >正文

React Native【实战范例】弹跳动画菜单导航

最终效果

在这里插入图片描述

范例代码

import React, { useEffect, useRef, useState } from "react";
import {Animated,Easing,StyleSheet,Text,TouchableOpacity,View,
} from "react-native";
export default function Demo() {const width1 = useRef(new Animated.Value(200)).current;const width2 = useRef(new Animated.Value(64)).current;const width3 = useRef(new Animated.Value(64)).current;const width4 = useRef(new Animated.Value(64)).current;const [index, setIndex] = useState(0);useEffect(() => {anim1(index === 0);anim2(index === 1);anim3(index === 2);anim4(index === 3);}, [index]);const anim1 = (isOpen) => {Animated.timing(width1, {toValue: isOpen ? 200 : 64,duration: isOpen ? 500 : 300,easing: isOpen ? Easing.elastic(3) : Easing.ease,useNativeDriver: false,}).start();};const anim2 = (isOpen) => {Animated.timing(width2, {toValue: isOpen ? 200 : 64,duration: isOpen ? 500 : 300,easing: isOpen ? Easing.elastic(3) : Easing.ease,useNativeDriver: false,}).start();};const anim3 = (isOpen) => {Animated.timing(width3, {toValue: isOpen ? 200 : 64,duration: isOpen ? 500 : 300,easing: isOpen ? Easing.elastic(3) : Easing.ease,useNativeDriver: false,}).start();};const anim4 = (isOpen) => {Animated.timing(width4, {toValue: isOpen ? 200 : 64,duration: isOpen ? 500 : 300,easing: isOpen ? Easing.elastic(3) : Easing.ease,useNativeDriver: false,}).start();};return (<View style={styles.root}><TouchableOpacityactiveOpacity={0.8}onPress={() => {setIndex(0);}}><Animated.Viewstyle={[styles.view,{ width: width1, opacity: index === 0 ? 1 : 0.75 },]}><Text style={styles.txt} numberOfLines={1} ellipsizeMode="clip">首页推荐</Text><View style={styles.dot} /></Animated.View></TouchableOpacity><TouchableOpacityactiveOpacity={0.8}onPress={() => {setIndex(1);}}><Animated.Viewstyle={[styles.view,{ width: width2, opacity: index === 1 ? 1 : 0.75 },]}><Text style={styles.txt} numberOfLines={1} ellipsizeMode="clip">热门直播</Text><View style={styles.dot} /></Animated.View></TouchableOpacity><TouchableOpacityactiveOpacity={0.8}onPress={() => {setIndex(2);}}><Animated.Viewstyle={[styles.view,{ width: width3, opacity: index === 2 ? 1 : 0.75 },]}><Text style={styles.txt} numberOfLines={1} ellipsizeMode="clip">我的礼物</Text><View style={styles.dot} /></Animated.View></TouchableOpacity><TouchableOpacityactiveOpacity={0.8}onPress={() => {setIndex(3);}}><Animated.Viewstyle={[styles.view,{ width: width4, opacity: index === 3 ? 1 : 0.75 },]}><Text style={styles.txt} numberOfLines={1} ellipsizeMode="clip">个人信息</Text><View style={styles.dot} /></Animated.View></TouchableOpacity></View>);
}
const styles = StyleSheet.create({root: {width: "100%",height: "100%",backgroundColor: "white",flexDirection: "column",justifyContent: "center",},view: {height: 60,flexDirection: "row",alignItems: "center",marginVertical: 16,borderTopRightRadius: 28,borderBottomRightRadius: 28,backgroundColor: "#2030ff",paddingLeft: 16,overflow: "hidden",},img: {width: 32,height: 32,tintColor: "white",},txt: {fontSize: 18,color: "#ffffffD0",marginLeft: 16,},dot: {width: 10,height: 10,backgroundColor: "#20f020",marginLeft: 28,borderRadius: 5,},
});
http://www.lryc.cn/news/572627.html

相关文章:

  • 2025-06-20 VLC 查看视频时候是如何知道 RTP 图像包是通过 TCP 还是 UDP 协议传输的呢?
  • cusor资源管理器缩进调整与工具条竖着摆放
  • 【Java学习笔记】线程基础
  • C++实例化对象与初始化的区别:深入解析与最佳实践
  • EfficientVLA:面向视觉-语言-动作模型无训练的加速与压缩
  • 准备开始适配高德Flutter的鸿蒙版了
  • 观远ChatBI:加速零售消费企业数据驱动的敏捷决策
  • 以太坊节点搭建私链(POA)
  • 【秒杀系统设计】
  • Vue3+TypeScript+ Element Plus 从Excel文件导入数据,无后端(点击按钮,选择Excel文件,由前端解析数据)
  • 拓客软件有哪些?
  • AI Agent开发与安全
  • 企业级文档搜索系统架构设计与实践指南
  • 巧用云平台API实现开源模型免费调用的实战教程
  • 数据库从零开始:MySQL 中的 DDL 库操作详解【Linux版】
  • 从生活场景学透 JavaScript 原型与原型链
  • 链接过程使用链接器将该目标文件与其他目标文件、库文件、启动文件等链接起来生成可执行文件。附加的目标文件包括静态连接库和动态连接库。其中的启动文件是什么意思?
  • 【内存】Linux 内核优化实战 - vm.max_map_count
  • Spring AOP @AfterReturning (返回通知)的使用场景
  • MySQL 分页查询列表;Explain ;深度分页 ;管理系统,筛选系统
  • AR 眼镜之-条形码识别-实现方案
  • 【AI时代速通QT】第二节:Qt SDK 的目录介绍和第一个Qt Creator项目
  • AI人工智能与LLM大语言模型有什么区别
  • Node.js 在前端开发中的作用与 npm 的核心理解
  • 1.22Node.js 中操作 Redis
  • Kafka线上集群部署方案:从环境选型到资源规划思考
  • 源易信息:领先GEO供应商的市场布局与服务优势
  • 【生活点滴】车辆过户、新车挂牌
  • 变幻莫测:CoreData 中 Transformable 类型面面俱到(五)
  • 学习华为 ensp 的学习心得体会