Sed*_*rei 4 reactjs react-native
我在中有3 View
个ScrollView
,如何View
在Scroll Down上向右或向左淡入淡出?
<ScrollView >
<View>
<Text>Fade Right View 1</Text>
</View>
<View>
<Text>Fade Right View 2</Text>
</View>
<View>
<Text>Fade Right View 3</Text>
</View>
</ScrollView >
Run Code Online (Sandbox Code Playgroud)
像这样:元素淡入滚动(https://codepen.io/annalarson/pen/GesqK)
我为您创建了一个小示例,但是您当然需要对其进行微调以使其完全发挥作用。
演示版
说明
我的示例包括两个部分。Fade组件和实际的ScrollView。淡入淡出组件可处理动画。通过滚动ScrollView触发动画淡入(请参见handleScroll函数)。
渐变成分
class Fade extends Component {
constructor(props) {
super(props);
this.state = {
visible: props.visible,
visibility: new Animated.Value(props.visible ? 1 : 0),
};
};
componentWillReceiveProps(nextProps) {
if (nextProps.visible) {
this.setState({ visible: true });
}
Animated.timing(this.state.visibility, {
toValue: nextProps.visible ? 1 : 0,
duration: 500,
}).start(() => {
this.setState({ visible: nextProps.visible });
});
}
render() {
const { style} = this.props;
const containerStyle = {
opacity: this.state.visibility.interpolate({
inputRange: [0, 1],
outputRange: [0, 1],
}), // interpolate opacity
transform: [
{
translateX: this.state.visibility.interpolate({
inputRange: [0, 1],
outputRange: [-20, 0],
}), // interpolate translateX to create a fade in left/right
},
],
};
const combinedStyle = [containerStyle, style];
return (
<Animated.View style={this.state.visible ? combinedStyle : containerStyle} />
);
}
}
Run Code Online (Sandbox Code Playgroud)
ScrollView代码段
handleScroll(e) {
if (e.nativeEvent.contentOffset.y > 50) { // you need to fine tune this value
this.setState({ visible: true });
}
}
<ScrollView onScroll={(e) => this.handleScroll(e) } scrollEventThrottle={8}>
<View style={{ backgroundColor: 'yellow', height: 200, marginTop: 10 }}/>
<View style={{ backgroundColor: 'yellow', height: 200, marginTop: 10 }}/>
<View style={{ backgroundColor: 'yellow', height: 200, marginTop: 10 }}/>
<Fade style={{ backgroundColor: 'red', height: 200, marginTop: 10 }} visible={this.state.visible} />
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
我希望我的示例可以使您了解如何实现预期的行为。
归档时间: |
|
查看次数: |
749 次 |
最近记录: |