This repository was archived by the owner on Dec 20, 2020. It is now read-only.
forked from eberhara/react-bidirectional-infinite-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertical.js
74 lines (65 loc) · 1.9 KB
/
vertical.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, { Component } from 'react'
import InfiniteScroll from '../../../src'
export default class VerticalExample extends Component {
state = {
items: []
}
componentDidMount() {
this.setState({
items: [].concat(this.getItems())
})
}
getItems() {
const itemsLength = this.state.items.length
const frequency = 0.3
const red = Math.round(Math.sin(frequency * itemsLength + 0) * 127 + 128)
const green = Math.round(Math.sin(frequency * itemsLength + 2) * 127 + 128)
const blue = Math.round(Math.sin(frequency * itemsLength + 4) * 127 + 128)
return (
<div
key={+new Date()}
style={{
height: '300px',
padding: '10px',
marginBottom: '10px',
fontFamily: '"Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif',
background: `rgba(${red}, ${green}, ${blue}, 1)`
}}>
Example {new Date().toLocaleString()}
</div>
)
}
handleScrollUp = () => {
const items = [].concat(this.getItems()).concat(this.state.items)
setTimeout(() => { this.setState({ items }) }, 500)
}
handleScrollDown = () => {
const items = this.state.items.concat(this.getItems())
setTimeout(() => { this.setState({ items }) }, 500)
}
handleOnScroll = (position, previousPosition) => {
const diffScroll = position - previousPosition
const direction = diffScroll > 0
? 'down'
: 'up'
console.log(`Scroll ${direction} to ${position}`)
}
render() {
return (
<div
style={{
height: '200px',
width: '300px',
WebkitOverflowScrolling: 'touch'
}}>
<InfiniteScroll
onReachBottom={this.handleScrollDown}
onReachTop={this.handleScrollUp}
onScroll={this.handleOnScroll}
position={50}>
{this.state.items}
</InfiniteScroll>
</div>
)
}
}