首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >反应-当本机样式粘住时,它是一个粘稠的标题。

反应-当本机样式粘住时,它是一个粘稠的标题。
EN

Stack Overflow用户
提问于 2017-07-02 19:32:08
回答 2查看 6.6K关注 0票数 6

我有一个简短的问题:在滚动视图中,如何将不同的样式应用于滚动视图中的粘性标题?

我想在它粘住的时候增加一些阴影/高度。

谢谢您:)

环境

  • 反应-原生: 0.45.0
EN

回答 2

Stack Overflow用户

发布于 2019-03-24 15:42:53

目前,React Native ScrollView组件具有一个名为stickyHeaderIndices的属性,即使在0.45版本中也是如此。您可以使用它传递应该是粘性的标头子索引。在此之后,您可以使用onScroll事件获取当前滚动位置,并在达到页眉大小时添加带有阴影的自定义样式。参见这里的示例:

https://snack.expo.io/@fabiodamasceno/scroll-sticky-styled

或者如果你喜欢:

代码语言:javascript
运行
复制
import * as React from 'react';
import { Text, View, ScrollView } from 'react-native';

const HEADER_HEIGHT = 20;
const headerStyle = {
  backgroundColor: '#e5e5e5',
  height: HEADER_HEIGHT
}
const myShadowStyle = {
  elevation: 3,
  shadowOpacity: 0.2,
  shadowRadius: 6,
  shadowOffset: {
    height: 3,
    width: 0,
  },
};

export default class App extends React.Component {
  state = {
     headerStyle : {
     ...headerStyle
    }
  }
  render() {
    return (
     <View style={{marginTop: HEADER_HEIGHT, height: 150}}>
      <ScrollView 
        stickyHeaderIndices={[0]} 
          onScroll={event => {
            const y = event.nativeEvent.contentOffset.y;
            if(y >= HEADER_HEIGHT)
              this.setState({
                headerStyle:{
                    ...headerStyle,
                    ...myShadowStyle
                }
              })
            else
              this.setState({
                  headerStyle:{
                      ...headerStyle,
                  }
                })
          }}
      >
        <View style={this.state.headerStyle}>
          <Text>My Header Title</Text>
        </View>
        <Text>Item 1</Text>
        <Text>Item 2</Text>
        <Text>Item 3</Text>
        <Text>Item 4</Text>
        <Text>Item 5</Text>
        <Text>Item 6</Text>
        <Text>Item 7</Text>  
        <Text>Item 8</Text>
        <Text>Item 9</Text>
        <Text>Item 10</Text>
        <Text>Item 11</Text>
        <Text>Item 12</Text>
        <Text>Item 13</Text>
        <Text>Item 14</Text>
      </ScrollView>
    </View>
    );
  }
}

票数 4
EN

Stack Overflow用户

发布于 2022-09-08 09:26:03

等会儿再给我..!最后的代码应该是这样的。这是最基本的例子,我们可以深入使用反应-本机动画。

代码语言:javascript
运行
复制
import React, { useRef } from "react";
import {
  SafeAreaView,
  StyleSheet,
  Text,
  Dimensions,
  Animated,
  View,
} from "react-native";
import Colors from "../../../../config/color/color";

const App = () => {
  const scrollPosition = useRef(new Animated.Value(0)).current;
  const minHeaderHeight = 70;
  const maxHeaderHeight = 200;

  const headerHeight = scrollPosition.interpolate({
    inputRange: [0, 500],
    outputRange: [maxHeaderHeight, minHeaderHeight],
    extrapolate: "clamp",
  });
  const opacity = scrollPosition.interpolate({
    inputRange: [0, 100, 200],
    outputRange: [1, 0.5, 0],
    extrapolate: "clamp",
  });
  const size = scrollPosition.interpolate({
    inputRange: [0, 100, 200, 300, 400],
    outputRange: [20, 17, 15, 13, 11],
    extrapolate: "clamp",
  });
  const imageHeight = scrollPosition.interpolate({
    inputRange: [0, 400],
    outputRange: [100, 50],
    extrapolateLeft: "identity",
    extrapolateRight: "clamp",
  });
  const imagePosition = scrollPosition.interpolate({
    inputRange: [0, 400],
    outputRange: [(37 * Dimensions.get("window").width) / 100, 0],
    extrapolateLeft: "identity",
    extrapolateRight: "clamp",
  });
  return (
    <SafeAreaView>
      <View>
        <Animated.View
          style={{
            // position: 'absolute',
            top: 0,
            left: 0,
            right: 0,
            zIndex: 10,
            height: headerHeight,
            backgroundColor: "lightblue",
          }}
        >
          <Animated.Text
            style={{
              opacity: opacity,
              fontSize: size,
            }}
          >
            Header
          </Animated.Text>
          <Animated.Image
            style={{
              height: imageHeight,
              width: imageHeight,
              borderRadius: imageHeight,
              transform: [{ translateX: imagePosition }],
            }}
            source={{
              uri: "https://www.talkwalker.com/images/2020/blog-headers/image-analysis.png",
            }}
          />
        </Animated.View>
        <Animated.ScrollView
          onScroll={Animated.event(
            [{ nativeEvent: { contentOffset: { y: scrollPosition } } }],
            { useNativeDriver: false }
          )}
          contentInsetAdjustmentBehavior="automatic"
          style={[styles.scrollView]}
        >
          {Array.from(Array(100), (e, key) => {
            return <Text key={key}>Item {key}</Text>;
          })}
        </Animated.ScrollView>
      </View>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: Colors.lighter,
  },
});

export default App;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44874469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档