1
0

index.jsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React from 'react';
  2. // import { Spring, animated, config } from 'react-spring/renderprops';
  3. import { Animation, presets } from '@douyinfe/semi-animation-react';
  4. import './style.scss';
  5. const COLORS = ['crimson', 'teal', 'coral', 'hotpink', 'skyblue', 'salmon', 'seagreen', 'peachpuff'];
  6. export default class App extends React.Component {
  7. state = { y: 0 };
  8. el = React.createRef();
  9. spring = React.createRef();
  10. setY = () => {
  11. console.log('setY');
  12. this.setState({ y: Math.round(Math.random() * 750) + 50 });
  13. };
  14. doScroll = (node, y) => {
  15. // eslint-disable-next-line eqeqeq
  16. if (node && y != null) {
  17. node.scrollTop = y;
  18. }
  19. };
  20. // User interaction should stop animation in order to prevent scroll-hijacking
  21. // Doing this on onWheel isn't enough, but just to illustrate ...
  22. stop = () => this.spring.current.stop();
  23. render() {
  24. const y = this.el.current ? this.el.current.scrollTop : 0;
  25. return (
  26. <div className="semi-animation-react-demo-scroll">
  27. <div className="scrolltop-main">
  28. <Animation
  29. ref={this.spring}
  30. reset
  31. from={{ y }}
  32. to={{ y: this.state.y }}
  33. config={presets.slow}
  34. onFrame={({ y }) => {
  35. this.doScroll(this.el.current, y);
  36. }}
  37. />
  38. <div className="scrolltop-c" ref={this.el} onWheel={this.stop}>
  39. {COLORS.map(c => (
  40. <div key={c} style={{ height: 200, background: c }} />
  41. ))}
  42. </div>
  43. </div>
  44. <div className="scrolltop-b" onClick={this.setY} />
  45. </div>
  46. );
  47. }
  48. }