首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当ViewPager移动到下一张幻灯片时,前一张幻灯片将降低每次移动的不透明度值。

当ViewPager移动到下一张幻灯片时,前一张幻灯片将降低每次移动的不透明度值。
EN

Stack Overflow用户
提问于 2020-06-19 16:48:11
回答 1查看 169关注 0票数 1

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

这个图像是我需求的一个很小的例子。

代码语言:javascript
复制
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。这也很有帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-20 08:27:04

您应该将x值内插到不透明度。X值介于0和负innerWitdh / 2之间,您应该将它转换为1到0之间的值。

这是工作公式。

代码语言:javascript
复制
opacity: x.to(x => 1 + x / (window.innerWidth / 2)),

下面是示例:https://codesandbox.io/s/card-slider-jwf3f

边注:

你可以检查,公式很容易,如果你显示在屏幕上。您所要做的就是将其作为animated.div的文本。例如:

代码语言:javascript
复制
 <animated.div>{x.to(x => 1 - Math.abs(x / (window.innerWidth / 2)))}</animated.div>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62474966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档