React Native【实战范例】同步跟随滚动

最终效果

实现原理

主动滚动区触发滚动事件,原生监听滚动值的变化,并用动画的方式实时同步到跟随滚动区

技术要点

  • 使用 Animated.ScrollView

  • 使用动画变量

    c 复制代码
    const scrollY = useRef(new Animated.Value(0)).current;
  • 主动滚动触发 onScroll,用 Animated.event 实现原生绑定

    c 复制代码
              onScroll={Animated.event(
                [
                  {
                    nativeEvent: {
                      contentOffset: { y: scrollY },
                    },
                  },
                ],
                { useNativeDriver: true }
              )}
  • 跟随滚动

    c 复制代码
    transform: [{ translateY: Animated.multiply(-1, scrollY) }],

范例代码

c 复制代码
import React, { useRef } from "react";
import { Animated, StyleSheet, View } from "react-native";
const colors = ["red", "green", "blue", "yellow", "orange"];
export default function Demo() {
  const scrollY = useRef(new Animated.Value(0)).current;
  const viewList = () => {
    const array = [
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
    ];
    return (
      <>
        {array.map((item, index) => (
          <View
            key={item}
            style={{
              width: 60,
              height: 100,
              backgroundColor: colors[index % 5],
            }}
          />
        ))}
      </>
    );
  };
  return (
    <View style={styles.root}>
      <View style={styles.leftLayout}>
        <Animated.View
          style={{
            width: 60,
            transform: [{ translateY: Animated.multiply(-1, scrollY) }],
          }}
        >
          {viewList()}
        </Animated.View>
      </View>
      <View style={styles.rightLayout}>
        <Animated.ScrollView
          showsVerticalScrollIndicator={false}
          onScroll={Animated.event(
            [
              {
                nativeEvent: {
                  contentOffset: { y: scrollY },
                },
              },
            ],
            { useNativeDriver: true }
          )}
        >
          {viewList()}
        </Animated.ScrollView>
      </View>
    </View>
  );
}
const styles = StyleSheet.create({
  root: {
    width: "100%",
    height: "100%",
    flexDirection: "row",
    justifyContent: "center",
  },
  leftLayout: {
    width: 60,
    backgroundColor: "#00FF0030",
    flexDirection: "column",
  },
  rightLayout: {
    width: 60,
    height: "100%",
    backgroundColor: "#0000FF30",
    marginLeft: 100,
  },
});
相关推荐
●VON9 小时前
React Native for OpenHarmony:2048 小游戏的开发与跨平台适配实践
javascript·学习·react native·react.js·von
●VON12 小时前
CANN推理引擎:从云端到边缘的极致加速与部署实战
学习·react native
lbb 小魔仙14 小时前
【HarmonyOS实战】React Native 鸿蒙版实战:Calendar 日历组件完全指南
react native·react.js·harmonyos
LYFlied16 小时前
从 Vue 到 React,再到 React Native:资深前端开发者的平滑过渡指南
vue.js·react native·react.js
lbb 小魔仙20 小时前
【HarmonyOS实战】OpenHarmony + RN:自定义 useFormik 表单处理
react native·harmonyos
果粒蹬i1 天前
【HarmonyOS】DAY8:React Native for OpenHarmony 实战:多端响应式布局与高可用交互设计
react native·交互·harmonyos
摘星编程1 天前
React Native鸿蒙版:Image图片占位符
react native·react.js·harmonyos
飞羽殇情2 天前
基于React Native鸿蒙跨平台开发构建完整电商预售系统数据模型,完成参与预售、支付尾款、商品信息展示等
react native·react.js·华为·harmonyos
摘星编程2 天前
React Native + OpenHarmony:ImageSVG图片渲染
javascript·react native·react.js
摘星编程2 天前
OpenHarmony + RN:Text文本书写模式
javascript·react native·react.js