改造微信小程序Swiper组件,自定义切换动画

这篇具有很好参考价值的文章主要介绍了改造微信小程序Swiper组件,自定义切换动画。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

index.tsx

import { FC, memo, PropsWithChildren, useEffect, useState, useRef, useMemo } from 'react'
import { Swiper as BaseSwiper, SwiperItem, View } from '@tarojs/components'
import { Button } from '@wmeimob/taro-design'
import classNames from 'classnames'
import styles from './index.module.less'
import Taro from '@tarojs/taro'
import { Flex } from '~/components'
import { vibrateShort } from '~/utils/util'
import { SwiperProps } from './const'
// import { getBarHeight } from '~/pages/service/util'

/**
 * Swiper
 * @param props
 * @returns
 */
const Component: FC<PropsWithChildren<SwiperProps>> = ({ current, modes, isDot, onMode, children }) => {
  const [currentIndex, setCurrentIndex] = useState(current ?? 0)
  // const [dx, setDx] = useState(0)
  const currentRef = useRef<number>(0)

  useEffect(() => {
    setCurrentIndex(current)
    return () => {
      Taro.setStorageSync('U1_LASTTIME_MODE_CURRENT', currentRef.current)
    }
  }, [current])
 
  // const getScale = useMemo(() => {
  //   const { windowWidth } = Taro.getSystemInfoSync()
  //   return Math.min(Math.max(dx / (windowWidth - 20), 0.88), 1)
  // }, [dx])
  // console.log('Taro', Taro.getSystemInfoSync().windowWidth, getScale)

  return (
    <View className={styles.container}>
      <BaseSwiper
        // autoplay
        // interval={2500}
        easingFunction='easeOutCubic'
        circular
        duration={1000}
        previousMargin='8px'
        nextMargin='12px'
        current={currentIndex}
        className={styles.swiperBox}
        onChange={(event) => {
          setCurrentIndex(event.detail.current)
          currentRef.current = event.detail.current
          vibrateShort()
        }}
        // style={{
        //   height: Taro.getSystemInfoSync().windowHeight - getBarHeight() - 44 - 34
        // }}
        // onTransition={(event) => {
        //   console.log('onTransition', event.detail.dx)
        //   setDx(event.detail.dx)
        // }}
      >
        {modes.map((mode, index) => {
          return (
            <SwiperItem key={`key-${index}`}>
              <Flex
                column
                style={{
                  backgroundImage: `url(${mode?.bgImg})`
                  // transform: index === currentIndex ? ` scale(${getScale})` : undefined
                }}
                className={classNames(styles.swiperItem,{
                  [styles.swiperItemActive]: index === currentIndex,
                  [styles.swiperItemOutRight]: index > currentIndex,
                  [styles.swiperItemOutLeft]: index < currentIndex
                })}
              >
                <Button
                    className={styles.button}
                    type="main"
                    text='开始护肤'
                    onClick={(event) => {
                      event.stopPropagation()
                      onMode?.(mode)
                    }}
                   />
              </Flex>
            </SwiperItem>
          )
        })}
      </BaseSwiper>
      {isDot && (
        <Flex alignCenter className={styles.dots}>
          {modes.map((_, index) => {
            return (
              <View
                key={`key-${index}`}
                className={classNames(styles.dot, {
                  [styles.dotActive]: index === currentIndex
                })}
              />
            )
          })}
        </Flex>
      )}
      {children}
    </View>
  )
}

const Swiper = memo(Component)
export default Swiper

index.less

.container {
  position: relative;
}

.swiperBox {
  position: relative;
  width: 100vw;
  height: calc(100vw * 638 / 375);

  .swiperItem {
    position: relative;
    width: calc(100% - 5px);
    height: 100%;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
    border-radius: 8px;
    margin-left: 5px;
    padding: 15px;

    .button {
      position: absolute;
      top: calc(100vw * 175 / 375);
      
      width: 130px;
      height: 42px;
      
      border-radius: 42px;
      background: #EFD7B1;
      font-weight: 500;
    }
  }

  .swiperItemActive {
    transform: scale(1);
    transition: all 0.8s ease 0s;
  }

  .swiperItemOutLeft {
    transition: all 0.8s ease-out 0.1s;
    transform: translate(20px, 38px) scale(0.88);
  }

  .swiperItemOutRight {
    transition: all 0.8s ease-out 0.1s;
    transform: translate(-20px, 38px) scale(0.88);
  }
}

.dots {
  position: absolute;
  bottom: -15px;
  // gap: 5px; // 有些手机型号无效
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;

  .dot {
    width: 12px;
    height: 2px;
    background-color: #5B5C5C;
    transition: all 0.5s;
    margin-right: 5px;
  }

  .dotActive {
    background-color: #fff;
  }
}

conts.ts

export interface SwiperProps {
  current: number
  modes: xxxx[] // xxx是需要定义的inteface结构
  isDot?: boolean
  onMode?: (plan: CareModelDTO) => void
}

改造Swiper组件,符合业务的设计和动画效果

改造微信小程序Swiper组件,自定义切换动画,Taro,前端,javascript,react.js文章来源地址https://www.toymoban.com/news/detail-851935.html

到了这里,关于改造微信小程序Swiper组件,自定义切换动画的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【微信小程序】swiper和swiper-item组件的基本使用

    ✅作者简介:CSDN内容合伙人、阿里云专家博主、51CTO专家博主🏆 📃个人主页:hacker707的csdn博客 🔥系列专栏:微信小程序 💬个人格言:不断的翻越一座又一座的高山,那样的人生才是我想要的。这一马平川,一眼见底的活,我不想要,我的人生,我自己书写,余生很长,请

    2024年02月09日
    浏览(32)
  • 【微信小程序】swiper自定义样式:指示点样式 wx-swiper-dot

    调试基础库:2.26.0 .wx-swiper-dots : 指示点容器样式 .wx-swiper-dots-horizontal : 水平滑动的指示点容器样式,其在 .wx-swiper-dots 内。 .wx-swiper-dot :指示点样式 .wx-swiper-dot-active : 当前指示点样式 默认指示点颜色 改变指示点颜色 默认指示点形状 改变指示点形状 默认指示点位置贴近

    2024年02月12日
    浏览(26)
  • 微信小程序swiper 视频中间大,两边小,轮播滑到中间视频自动播放组件教程

    静态效果:  进入下面小程序可以体验效果 ,点击底部 看剧 栏目   

    2024年02月20日
    浏览(34)
  • 【微信小程序】-- 常用视图容器类组件介绍 -- view、scroll-view和swiper(六)

    💌 所属专栏:【微信小程序开发教程】 😀 作  者:我是夜阑的狗🐶 🚀 个人简介:一个正在努力学技术的CV工程师,专注基础和实战分享 ,欢迎咨询! 💖 欢迎大家:这里是CSDN,我总结知识的地方,喜欢的话请三连,有问题请私信 😘 😘 😘   大家好,又见面了,

    2024年01月18日
    浏览(36)
  • 微信小程序常用组件的简单使用 view,scroll-view,swiper,swiper-item,text,rich-text,button,image

    view组件就类似于html中的div标签 list.wxml list.wxss scroll-view组件就是滚动的视窗,使用scroll-view组件时,要想横向滚动或者纵向滚动时,需要在scroll-view组件上添加对应的属性 scroll-x 或 scroll-y,然后需要注意的是,纵向滚动需要给scroll-view组件限定高度,横向滚动则需要给scroll-vi

    2024年02月15日
    浏览(26)
  • 微信小程序自定义tabbar切换延迟以及切换闪烁问题

    首先,吐槽一番,官方bug,好多年了,一直不解决....那我们就自己解决.. 切换延迟就是点击tabbar时要点击两次icon才能正确选中,比如说首页要跳转到工单页面,要点击两次工单的图标才被激活; 解决: 在对应的要跳转的页面的show生命周期里面加上以下代码即可,selected是custom-tab-bar里

    2024年01月21日
    浏览(35)
  • 微信小程序在调用swiper组件时如果出现[渲染层错误] Uncaught TypeError: Cannot read property ‘$$‘ of undefined

    报错:TypeError: Cannot read property ‘$$’ of undefined 还需设置current = 0, 并且current和swiperList不能在一个this.setData中设置, 要先setData swiperList 然后在setData current

    2024年02月13日
    浏览(37)
  • 在微信小程序中如何使用Loading组件显示载入动画

    Loading组件是微信小程序中常用的一种UI组件,用于在数据加载过程中显示载入动画,提高用户体验。本文将详细介绍如何在微信小程序中使用Loading组件,并提供相应的源代码示例。 创建Loading组件 首先,在小程序的组件文件夹中创建一个名为\\\"loading\\\"的文件夹,并在该文件夹下

    2024年02月03日
    浏览(41)
  • 微信小程序+vant组件 侧边导航栏切换显示

    设计页面时希望效果:左侧侧边导航栏,右侧内容。点击左侧导航栏的不同块,右侧显示不同内容。 采用了vant组件中侧边导航栏van-sidebar,van-sidebar子标签包括多个van-sidebar-item 实现方法: van-sidebar中设置 bind:change=\\\"onChange\\\": 随后在js文件中编写onChange(event),其中event.deta

    2024年02月13日
    浏览(33)
  • 【微信小程序】自定义组件(二)

    🎁 写在前面: 观众老爷们好呀,这里是前端小刘不怕牛牛频道,小程序系列文章又更新了呀。 上文我们讲解了微信小程序自定义组件的入门知识,那么今天牛牛就来讲讲自定义组件的进阶知识吧,赶紧拿起小本本做笔记呀! 自定义组件的数据和方法在使用上,和 Vue 的组件

    2024年02月02日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包