├── LICENSE
├── README.md
├── index.js
├── scroll-vertical.gif
└── scrollVertical.js
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 QuietNight
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # react-native-scrollVertical
2 | 纯JavaScript实现仿京东app首页 京东快报 上下滚屏效果
3 |
4 | 
5 |
6 | ### Installation
7 | ```
8 | npm install --save https://github.com/accforgit/react-native-scrollVertical.git
9 | ```
10 |
11 | ### Basic Usage
12 | ```
13 | import React, { Component } from 'react'
14 |
15 | import ScrollVertical from './scrollVertical'
16 |
17 | export default class Index extends Component {
18 | constructor(props) {
19 | super(props)
20 | }
21 |
22 | render() {
23 | // 测试数据
24 | let data = []
25 | for(let i=4; i--; ) {
26 | data.push({content: 'animatedScroll ' + i})
27 | }
28 | return (
29 |
36 | )
37 | }
38 | }
39 | ```
40 | ### Properties
41 |
42 | |Props|Default|Type|Description|
43 | |---|---|---|---|
44 | |data|[]|array(object{content})|上下滚屏的数据条目|
45 | |delay|500|number|每一次滚动切换之前延迟的时间,单位为 ms|
46 | |duration|500|number|每一次滚动切换的持续时间,单位为 ms|
47 | |scrollHeight|32|number|滚屏高度|
48 | |scrollStyle|null|style|滚屏样式|
49 | |textStyle|null|style|滚屏文字样式|
50 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from 'react'
2 |
3 | import ScrollVertical from './scrollVertical'
4 |
5 | export default class Index extends Component {
6 | constructor(props) {
7 | super(props)
8 | }
9 |
10 | render() {
11 | // 测试数据
12 | let data = []
13 | for(let i=4; i--; ) {
14 | data.push({content: 'animatedScroll ' + i})
15 | }
16 | return (
17 |
24 | )
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/scroll-vertical.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/accforgit/react-native-scrollVertical/b74dc0d16b3b1fb7d49d66594556ce235b755963/scroll-vertical.gif
--------------------------------------------------------------------------------
/scrollVertical.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from 'react'
2 | import {
3 | Text,
4 | View,
5 | Animated,
6 | Easing,
7 | StyleSheet,
8 | } from 'react-native'
9 |
10 | export default class ScrollVertical extends Component {
11 | constructor(props) {
12 | super(props)
13 | this.state ={
14 | translateValue: new Animated.ValueXY({x:0, y:0}),
15 | // 滚屏高度
16 | scrollHeight: this.props.scrollHeight || 32,
17 | // 滚屏内容
18 | kb_content: [],
19 | // Animated.View 滚动到的 y轴坐标
20 | kb_tempValue: 0,
21 | // 最大偏移量
22 | kb_contentOffsetY: 0,
23 | // 每一次滚动切换之前延迟的时间
24 | delay: this.props.delay || 500,
25 | // 每一次滚动切换的持续时间
26 | duration: this.props.duration || 500
27 | }
28 | }
29 | render() {
30 | return (
31 |
32 | {
33 | this.state.kb_content.length !==0 ?
34 |
41 | {this.state.kb_content.map(this._createKbItem.bind(this))}
42 | : null
43 | }
44 |
45 | )
46 | }
47 |
48 | componentDidMount() {
49 | let content = this.props.data || []
50 | if(content.length !== 0) {
51 | let h = (content.length+1) * this.state.scrollHeight
52 | this.setState({
53 | kb_content: content.concat(content[0]),
54 | kb_contentOffsetY: h
55 | })
56 |
57 | // 开始动画
58 | this._startAnimation()
59 | }
60 | }
61 |
62 | _createKbItem(kbItem, index) {
63 | return (
64 |
66 | {kbItem.content}
67 |
68 | )
69 | }
70 |
71 | _startAnimation() {
72 | this.state.kb_tempValue -= this.state.scrollHeight
73 | Animated.sequence([
74 | Animated.delay(this.state.delay),
75 | Animated.timing(
76 | this.state.translateValue,
77 | {
78 | toValue:{x:0,y:this.state.kb_tempValue},
79 | duration: this.state.duration, // 动画持续的时间(单位是毫秒),默认为500
80 | easing: Easing.linear
81 | }
82 | ),
83 | ])
84 | .start(()=>{
85 | // 无缝切换
86 | if(this.state.kb_tempValue-this.state.scrollHeight === -this.state.kb_contentOffsetY) {
87 | // 快速拉回到初始状态
88 | this.state.translateValue.setValue({x:0,y:0})
89 | this.state.kb_tempValue = 0
90 | }
91 | this._startAnimation()
92 | })
93 | }
94 | }
95 |
96 | const styles = StyleSheet.create({
97 | kbContainer:{
98 | // 必须要有一个背景或者一个border,否则本身高度将不起作用
99 | backgroundColor:'transparent'
100 | },
101 | kb_text_c:{
102 | fontSize: 18,
103 | color:'#181818',
104 | }
105 | })
--------------------------------------------------------------------------------