我的要求是当ViewPager点击下一个图像并拖动图像时,前一个图像应该降低每次拖动时的不透明度。这是我的码箱代码。
这个图像是我需求的一个很小的例子。

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
}}
/>
))
}我需要相同的功能,如码箱代码和减少不透明度时,拖动卡到其他卡。如果在其他库中有任何其他解决方案,比如framer-motion。这也很有帮助。
发布于 2020-06-20 08:27:04
您应该将x值内插到不透明度。X值介于0和负innerWitdh / 2之间,您应该将它转换为1到0之间的值。
这是工作公式。
opacity: x.to(x => 1 + x / (window.innerWidth / 2)),下面是示例:https://codesandbox.io/s/card-slider-jwf3f
边注:
你可以检查,公式很容易,如果你显示在屏幕上。您所要做的就是将其作为animated.div的文本。例如:
<animated.div>{x.to(x => 1 - Math.abs(x / (window.innerWidth / 2)))}</animated.div>https://stackoverflow.com/questions/62474966
复制相似问题