Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { render } from 'react-dom'
  2. import React, { useRef } from 'react'
  3. import clamp from 'lodash-es/clamp'
  4. import { useSprings, animated } from 'react-spring'
  5. import { useGesture } from 'react-use-gesture'
  6. import './Instagram2.css'
  7.  
  8.  
  9.  
  10. const pages = [
  11.   'https://images.pexels.com/photos/62689/pexels-photo-62689.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
  12.   'https://images.pexels.com/photos/296878/pexels-photo-296878.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
  13.   'https://images.pexels.com/photos/1509428/pexels-photo-1509428.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
  14.   'https://images.pexels.com/photos/351265/pexels-photo-351265.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
  15.   'https://images.pexels.com/photos/924675/pexels-photo-924675.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260'
  16. ]
  17.  
  18.  
  19.  
  20. function Viewpager() {
  21.   const index = useRef(0)
  22.   const [props, set] = useSprings(pages.length, i => ({ x: i * window.innerWidth, sc: 1, display: 'block' }))
  23.   const bind = useGesture(({ down, delta: [xDelta], direction: [xDir], distance, cancel }) => {
  24.     if (down && distance > window.innerWidth / 2)
  25.       cancel((index.current = clamp(index.current + (xDir > 0 ? -1 : 1), 0, pages.length - 1)))
  26.     set(i => {
  27.       if (i < index.current - 1 || i > index.current + 1) return { display: 'none' }
  28.       const x = (i - index.current) * window.innerWidth + (down ? xDelta : 0)
  29.       const sc = down ? 1 - distance / window.innerWidth / 2 : 1
  30.       return { x, sc, display: 'block' }
  31.     })
  32.   })
  33.   return (
  34.     <div className="spring-container">
  35.       {props.map(({ x, display, sc }, i) => (
  36.         <animated.div {...bind()} key={i} style={{ display, transform: x.interpolate(x => `translate3d(${x}px,0,0)`) }}>
  37.           <animated.div style={{ transform: sc.interpolate(s => `scale(${s})`), backgroundImage: `url(${pages[i]})` }} />
  38.         </animated.div>
  39.       ))}
  40.     </div>
  41.   )
  42. }
  43.  
  44.  
  45. export default Viewpager;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement