GithubHelp home page GithubHelp logo

younth / react-swipes Goto Github PK

View Code? Open in Web Editor NEW
185.0 8.0 40.0 4.97 MB

:fire: 基于React的移动端卡片滑动组件

License: MIT License

JavaScript 100.00%
react swiper swipe slider react-swipes

react-swipes's Introduction

react-swipes

打造最好用的React 移动端卡片滑动 组件。已支持卡片自动播放,配置加入 autoPlay 参数即可。

为什么要造轮子

目前react component里面 基于移动端轮播/幻灯片 组件,最熟悉应该是react-swipe这个库了。且看这个组件的构成:

  • react-swipe: 引入swipe-js-iso,创建react组件
  • swipe-js-iso: 基于swipe.js的一个Pull Request

也就是说,整个组件是基于几年前的swipe.js的,这个库三四年没更新了,看提问,作者在12年说要发个swipe2,结果不了了之,导致bug修复很慢,功能支持不全。比如我想做这个效果:

vip

newuser


尴尬啊,react-swipe现在的能力根本支持不了(因为swipe是针对焦点图设计的,写死了子元素宽度是父级的宽度)

解决方案

不用react的情况下,swiper.js 是个不错的选择,但是考虑这个库太大了(5000行+),为一个卡片滑动实在不值得。最后还是自己搞了个react-swipes,为的就是快速方便的实现上面这种卡片切换效果。

安装

npm i react-swipes --save

使用

原理

swipes不依赖任何css,不会去改变子item的样式,也就是说,css完全控制在自己的代码中。所以有必要了解卡片滑动的原理:

// 三层滑动原理,动的是第二层

// 第一层设置固定宽度 ,超过部分设置为不显示 overflow: hidden;  
<div>  
    //第二层设置为实际需要的宽度,即子div的n倍,有间距需要算上间距
    <div>  
        //第三层,实际item 内容的宽度     
        <div></div>  
    </div>  
</div>

这是滑动的基础布局,最终的滑动也就是改变第二层div的translateX值。再加上transition 过渡效果,即可实现整个区域的运动,而最外层元素设置了溢出隐藏,这样整体效果就是单张卡片在运动了。

具体使用

    import ReactSwipe from 'react-swipes'
    
    // swipes 的配置
    let opt = {
    distance: 620, // 每次移动的距离,卡片的真实宽度
    currentPoint: 1,// 初始位置,默认从0即第一个元素开始
    autoPlay: true, // 是否开启自动播放
    swTouchstart: (ev) => {

    },
    swTouchmove: (ev) => {

    },
    swTouchend: (ev) => {
        let data = {
            moved: ev.moved,
            originalPoint: ev.originalPoint,
            newPoint: ev.newPoint,
            cancelled: ev.cancelled
        }
        console.log(data);
        this.setState({
            curCard: ev.newPoint
        })
    }
    }
    
    // dom 部分
    // 第一层div
    <div className="card-swipe" >
        // 第二层div  react-swipes生成一个className为 card-slide的div
        <ReactSwipe className="card-slide" options={opt}>
            // 第三层,即本身的item
            this.state.card.length && this.state.card.map((item, index) => <div className="item" key={index}> </div>
        </ReactSwipe>
    </div>

demo

sandbox demo

todo

  • 现在把css完全暴露给使用者了,这样第二层div的宽度(第三层元素的宽度和)需要大家在代码中计算,不是很方便,后续会把这块逻辑放到组件里面去,开发者只需要配置卡片基础属性即可。
  • 卡片滑动过程想实现类似上面的缩放效果,目前需要在 swTouchmove swTouchend 钩子里面自己去实现,后面会把这个效果做到组件里面去,开发者选择是否开启。

react-swipes's People

Contributors

younth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-swipes's Issues

使用rem的情况下,怎么让设置里的distance值和item的宽度一致

现在我使用的是rem,主要来写微信公众号,现在想实现你放的第一张图的效果,滚动时显示的卡片居中,两边有前后卡片的一部分显示出来,现在就这个设置里的distance值该如何设置呢。

我设置的根fontSize是window.innerWidth/7.5 + 'px';,item的宽度是6rem。

自动播放不能循环

let opt = {
distance:document.getElementsByTagName('html')[0].style.fontSize.split('px')[0]*6.8, // 每次移动的距离,卡片的真实宽度
currentPoint: 0,// 初始位置,默认从0即第一个元素开始
autoPlay: true,
loop: true,
swTouchstart: (ev) => {

        },
        swTouchmove: (ev) => {
            
        },
        swTouchend: (ev) => {
            let data = {
                moved: ev.moved,
                originalPoint: ev.originalPoint,
                newPoint: ev.newPoint,
                cancelled: ev.cancelled
            }
            console.log(ev);
            this.setState({
                curCard: ev.newPoint
            })
        }
    }

用在react移动端项目中,不能循环播放

可否加入自动且循环播放

强烈求助加入自动播放,这个不会自动播放有点怪怪的,目前遇到的交互都是希望可以自动播放,且可以循环播放

卡片滑动过程想实现类似上面的缩放效果实现

卡片滑动过程想实现类似上面的缩放效果,目前需要在 swTouchmove swTouchend 钩子里面自己去实现,后面会把这个效果做到组件里面去,开发者选择是否开启。

这个用css样式实现就好了啊,在当前选中的Item上面加一个样式

transform: scale(1.2);

搞定

image

refs 问题

你的 sanbox demo 报错 refs 问题 , 能解决一下 吗?
使用这个包在我自己的项目里面报错。 错误信息如下

invariant.js: 42 Uncaught Error: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's render method, or you have multiple copies of React loaded (details: https://fb.me/react-refs-must-have-owner).

希望能尽快解决下。

建议加入无限循环

求助加入无限循环播放,比如左滑到最后一张的时候,可以继续左滑出现第一张,第二张...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.