要让ScrollView滚动到最左边,可以使用scrollTo方法,并将x参数设置为0。以下是一个示例代码:

import React, { useRef } from 'react';
import { ScrollView, Button } from 'react-native';

const MyScrollView = () => {
  const scrollViewRef = useRef();

  const scrollToStart = () => {
    scrollViewRef.current.scrollTo({ x: 0, y: 0, animated: true });
  };

  return (
    <>
      <ScrollView ref={scrollViewRef}>
        {/* 内容 */}
      </ScrollView>
      <Button title="滚动到最左边" onPress={scrollToStart} />
    </>
  );
};

export default MyScrollView;

在上面的代码中,我们创建了一个MyScrollView组件,其中包含一个ScrollView和一个按钮。当按钮被点击时,scrollToStart函数会被调用,它会使用scrollTo方法将ScrollView滚动到最左边。


原文地址: https://www.cveoy.top/t/topic/iaBH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录