Taro 微信小程序轮播图 Swiper 组件缩放效果实现

效果预览

请添加图片描述

预备知识

前边距&后边距

image.png

SwiperItem

  1. SwiperItem 之间是没有间隔的,它们是连接在一起的。
  2. SwiperItem 的宽度无法更改,只能默认 100%。

计算 previousMargin & nextMargin

数值单位都是 rpx

标题
原始图片宽度600屏幕总宽度750
原始图片两侧剩余宽度750 - 600 = 150单侧剩余宽度150 / 2 = 75
previousMargin & nextMargin75

image.png

完整代码

const banners = [
  {
    src: 'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',
    text: '轮播图标题 1'
  },
  {
    src: 'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',
    text: '轮播图标题 2'
  },
  {
    src: 'https://fastly.jsdelivr.net/npm/@vant/assets/apple-3.jpeg',
    text: '轮播图标题 3'
  },
  { src: 'https://fastly.jsdelivr.net/npm/@vant/assets/apple-4.jpeg', text: '轮播图标题 4' }
];

export const CustomSwiper = () => {
  const [currentIdx, setCurrentIdx] = useState(0);
  const handleSwiperChange = e => {
    setCurrentIdx(e.detail.current);
  };

  return (
    <Swiper
      className="swiper"
      autoplay
      circular
      previousMargin="75rpx"
      nextMargin="75rpx"
      onChange={handleSwiperChange}
    >
      {banners.map((banner, index) => {
        return (
          <SwiperItem key={banner.src}>
            <View className={clsx('swiper__wrap', { active: currentIdx === index })}>
              <Image
                className="swiper__img"
                src={banner.src}
                onTap={() => {
                  console.log(`点击了第${index}张轮播图`);
                }}
              />
              <View className="swiper__text">{banner.text}</View>
            </View>
          </SwiperItem>
        );
      })}
    </Swiper>
  );
};
.swiper {
  width: 100%;
  height: 338px;
  position: relative;

  &__wrap {
    width: auto;
    height: inherit;
    border-radius: 20px;
    transform: scale(0.9);
    transition: all 0.5s ease;

    &.active {
      transform: scale(1);
    }
  }

  &__img {
    width: 600px;
    height: inherit;
    border-radius: 20px;
  }

  &__text {
    position: absolute;
    bottom: 0;
    padding-left: 20px;
    box-sizing: border-box;
    width: 100%;
    height: 78px;
    line-height: 78px;
    font-weight: 500;
    font-size: 32px;
    color: #fff;
    border-radius: 0px 0px 20px 20px;
    background: linear-gradient(180deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.9) 100%);
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值