我的要求是,当ViewPager单击并拖动下一个图像时,上一个图像应为每次拖动降低不透明度。这是我的codesandbox代码。
此图像是我要求的一个小示例。
import React, { useRef } from 'react'
import clamp from 'lodash-es/clamp'
import { useSprings, animated } from 'react-spring'
import { useDrag } from 'react-use-gesture'
import './styles.css'
function Viewpager() {
const index = useRef(0)
const [props, set] = useSprings(alphabets.length, i => ({
x: i * window.innerWidth,
scale: 1,
opacity: 1,
display: 'block'
}))
const bind = useDrag(({ active, down, movement: [mx], direction: [xDir], distance, cancel }) => {
set(i => {
if (i < index.current - 1 || i > index.current + 1) return { display: 'none' }
const x = (i - index.current) * window.innerWidth + (down ? mx : 0)
const scale = down ? 1 - distance / window.innerWidth / 2 : 1
const opacity = active ? distance * 0.01 : 1
return { x, scale, opacity, display: 'block' }
})
})
return props.map(({ x, display, scale, opacity }, i) => (
<animated.div
{...bind()}
key={i}
style={{
opacity,
display,
x
}}
/>
))
}
我需要相同的功能,例如codeandbox代码,并在将卡拖到其他卡上时减少不透明度的并行性。如果任何人在其他库中有其他解决方案,例如framer-motion
。这也是有帮助的。
您应该将x值插值到不透明度。x值介于0到负的innerWitdh / 2之间。您应该将其传输以获得1到0之间的值。
这是工作公式。
opacity: x.to(x => 1 + x / (window.innerWidth / 2)),
这是示例:https : //codesandbox.io/s/card-slider-jwf3f
边注:
如果在屏幕上显示该公式,则可以轻松地对其进行检查。您所要做的只是将其作为animation.div的文本。例如:
<animated.div>{x.to(x => 1 - Math.abs(x / (window.innerWidth / 2)))}</animated.div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句