Coder Social home page Coder Social logo

we-swiper's Introduction

we-swiper

微信小程序触摸内容滑动解决方案,API设计细节及命名参考于swiper.js.

为什么要开发这款插件

官方swiper组件:

  • 支持的事件回调很单一
  • 从文档上看只是能支持横向滑动(官方swiper组件实际是支持纵向滚动的,设置属性vertical="true"即可,但并未在官方文档提及,感谢@pangz1指正)
  • 拓展性不强

we-swiper插件:

  • 丰富的事件回调
  • 丰富的属性
  • 支持横、纵向滑动
  • 强拓展(可在原插件基础上二次开发)

ScreenShots

横向滚动

Alt text

纵向滚动

Alt text

使用方式

克隆项目至你的目录

cd my-project

git clone https://github.com/dlhandsome/we-swiper.git

在项目文件引入 dist/weSwiper.js进行开发

es6 module
import weSwiper from 'dist/weSwiper'
commonjs
var weSwiper = require('dist/weSwiper')

示例

example.wxml

 <view class="we-container {{directionClass}}">
  <view class="we-wrapper"
    bindtouchstart="touchstart"
    bindtouchmove="touchmove"
    bindtouchend="touchend"
    animation="{{animationData}}">
    <view class="we-slide">slide 1</view>
    <view class="we-slide">slide 2</view>
    <view class="we-slide">slide 3</view>
  </view>
</view>

example.js

import weSwiper from '../dist/weSwiper'

const option = {
  touchstart (e) {
    this.weswiper.touchstart(e)
  },
  touchmove (e) {
    this.weswiper.touchmove(e)
  },
  touchend (e) {
    this.weswiper.touchend(e)
  },
  onLoad () {
    new weSwiper({
      animationViewName: 'animationData',
      slideLength: 3,
      initialSlide: 0,
      /**
       * swiper初始化后执行
       * @param weswiper
       */
      onInit (weswiper) {

      },
      /**
       * 手指碰触slide时执行
       * @param weswiper
       * @param event
       */
      onTouchStart (weswiper, event) {

      },
      /**
       * 手指碰触slide并且滑动时执行
       * @param weswiper
       * @param event
       */
      onTouchMove (weswiper, event) {

      },
      /**
       * 手指离开slide时执行
       * @param weswiper
       * @param event
       */
      onTouchEnd (weswiper, event) {

      },
      /**
       *  slide达到过渡条件时执行
       */
      onSlideChangeStart (weswiper) {

      },
      /**
       *  weswiper从一个slide过渡到另一个slide结束时执行
       */
      onSlideChangeEnd (weswiper) {

      },
      /**
       *  过渡时触发
       */
      onTransitionStart (weswiper) {

      },
      /**
       *  过渡结束时执行
       */
      onTransitionEnd (weswiper) {

      },
      /**
       *  手指触碰weswiper并且拖动slide时执行
       */
      onSlideMove (weswiper) {

      },
      /**
       * slide达到过渡条件 且规定了方向 向前(右、下)切换时执行
       */
      onSlideNextStart (weswiper) {

      },
      /**
       *  slide达到过渡条件 且规定了方向 向前(右、下)切换结束时执行
       */
      onSlideNextEnd (weswiper) {

      },
      /**
       *  slide达到过渡条件 且规定了方向 向前(左、上)切换时执行
       */
      onSlidePrevStart (swiper) {

      },
      /**
       *  slide达到过渡条件 且规定了方向 向前(左、上)切换结束时执行
       */
      onSlidePrevEnd (weswiper) {

      }
    })
  }
}
Page(option)

we-swiper初始化

weSwiper在onLoad方法中实例化

onLoad () {
  new weSwiper({
    slideLength: 3  // 必填,由于目前无法直接获取slide页数,目前只能通过参数写入
  }) 
}

可通过this.weswiper在Page的钩子函数中访问实例

touchstart (e) {
  this.weswiper.touchstart(e)
}

WXML结构配置

<view class="we-container {{directionClass}}">
  <view class="we-wrapper"
    bindtouchstart="touchstart"
    bindtouchmove="touchmove"
    bindtouchend="touchend"
    animation="{{animationData}}">
    <view class="we-slide">slide 1</view>
    <view class="we-slide">slide 2</view>
    <view class="we-slide">slide 3</view>
  </view>
</view>

小提示:WXML中的字段不需要在Page的data中给出默认值,we-swiper内部有同步视图机制。

构造器参数

必填项

slideLength

  • Type: Number
  • Default: 0

表示slide的页数

可选项

width

  • Type: Number
  • Default: device.windowWidth

设定slide的宽度(横向滑动时slide滑动间隔距离会根据其值计算)

height

  • Type: Number
  • Default: device.windowHeight

设定slide的高度(纵向滑动时slide滑动间隔距离会根据其值计算)

direction

  • Type: String
  • Default: horizontal
  • Option:
    • horizontal: slide水平方向滑动
    • vertical: slide垂直方向滑动

设定slide滑动方向

initialSlide

  • Type: Number
  • Default: 0

设定初始化时slide的索引

speed

  • Type: Number
  • Default: 300

设定slide过渡时长

timingFunction

  • Type: String
  • Default: ease
  • Option:
    • linear: slide水平方向滑动
    • ease: slide垂直方向滑动
    • ease-in: slide垂直方向滑动
    • ease-in-out: slide垂直方向滑动
    • ease-out: slide垂直方向滑动
    • step-start: slide垂直方向滑动
    • step-end: slide垂直方向滑动

设定slide过渡动画速度曲线

autoplay

  • Type: Number
  • Default: 0

设定slide自动播放间隔时长,设置为0时不自动播放

directionViewName

  • Type: String
  • Default: directionClass

对应视图中direction类名

animationViewName

  • Type: String
  • Default: animationData

对应视图中animation属性名

属性

weswiper.activeIndex

返回当前活动块(激活块)的索引

weswiper.previousIndex

返回上一个活动块的索引

weswiper.width

返回swiper容器的宽度

weswiper.height

返回swiper容器的高度

weswiper.isBeginning

如果swiper处于最初始位置,返回true

weswiper.isEnd

如果swiper处于最末尾位置,返回true

weswiper.speed

返回当前swiper的过渡时长

方法

weswiper.slideNext(runCallbacks, speed)

滑动到后一个slide

  • Params:
    • runCallbacks: 可选,设为false不触发onSlideChange回调函数
    • speed: 可选,切换速度

weswiper.slidePrev(runCallbacks, speed)

滑动到前一个slide。

  • Params:
    • runCallbacks: 可选,设为false不触发onSlideChange回调函数
    • speed: 可选,切换速度

weswiper.slideTo(index, speed, runCallbacks)

切换到指定slide。

  • Params:
    • index: 必选,num,指定将要切换到的slide的索引
    • speed: 可选,切换速度
    • runCallbacks: 可选,设为false不触发onSlideChange回调函数

事件回调

onInit (weswiper)

回调函数,初始化后执行。 可选weswiper实例作为参数。

onTouchStart (weswiper, event)

回调函数,当碰触到slider时执行。可选weswiper和touchstart事件作为参数

onTouchMove (weswiper, event)

回调函数,手指触碰weswiper并滑动(手指)时执行。此时slide不一定会发生移动,比如你手指的移动方向和weswiper的切换方向垂直时

onTouchEnd (weswiper, event)

回调函数,当释放slider时执行。(释放即执行)

onSlideChangeStart (weswiper)

回调函数,weswiper从当前slide开始过渡到另一个slide时执行。触摸情况下,如果释放slide时没有达到过渡条件而回弹时不会触发这个函数,此时可用onTransitionStart。

可接受weswiper实例作为参数,输出的activeIndex是过渡后的slide索引

onSlideChangeEnd (weswiper)

回调函数,weswiper从一个slide过渡到另一个slide结束时执行。

可接受swiper实例作为参数。

onTransitionStart (weswiper)

回调函数,过渡开始时触发,接受weswiper实例作为参数。

onTransitionEnd (weswiper)

回调函数,过渡结束时触发,接收weswiper实例作为参数。

onSlideMove (weswiper)

回调函数,手指触碰weswiper并拖动slide时执行。

onSlideNextStart (weswiper)

回调函数,滑块释放时如果触发slider向前(右、下)切换则执行。类似于onSlideChangeStart,但规定了方向。

onSlideNextEnd (weswiper)

回调函数,slider向前(右、下)切换结束时执行。类似于onSlideChangeEnd,但规定了方向。

onSlidePrevStart (weswiper)

回调函数,滑块释放时如果触发slider向后(左、上)切换则执行。类似于onSlideChangeStart,但规定了方向。

onSlidePrevEnd (weswiper)

回调函数,slider向后(左、上)切换结束时执行。类似于onSlideChangeEnd,但规定了方向。

TODO

  • 增加切换效果:fade,flip等

License

The MIT License(http://opensource.org/licenses/MIT)

请自由地享受和参与开源

贡献

如果你有好的意见或建议,欢迎给我提issue或pull request。

we-swiper's People

Contributors

dlhandsome 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

we-swiper's Issues

自动播放时手滑

设置自动轮播后,在手指触摸时应停止自动播放,拿开之后继续自动播放

动态删除slide选项

您好 我想问下 您有开发动态删除slide的选项的计划么? 类似于 swiper.js的.removeSlide(0);和mySwiper.appendSlide(slides)

支持在Component中调用

需求描述

从小程序基础库版本 1.6.3 开始,小程序支持简洁的组件化编程 - 小程序自定义组件,但在Component中引用weSwiper会出现动画失效的问题。

通过阅读源码发现:

weSwiper通过动态修改view中的animation属性来实现动画效果,数据传递是通过page.setData来实现的。这样就导致了在Component里引用weSwiper时,无法得到更新的animation值来实现动画。

image

具体实现

为了实现在Component中引用weSwiper,添加了context参数,使得数据传递是通过context.setData(这里的context是component)来实现的,具体代码如下:

image

讨论

还有没有别方案实现该需求:通过page.setData来改变page的animation属性,再将page中的animation属性传递给component,实现动画。但是破坏了component的封装性。

欢迎讨论,再次感谢你们贡献了这个优秀的组件!

能否提供一个真实案例,说明这个组件的优势

我的需求是实现一个类似今日头条图集的图片轮播功能,我用小程序内置的Swiper只可以勉强实现多触点放大缩小图片,会和下一页切换冲突。我感觉这个需求挺强的,目前没有找到好的解决方案。
这个插件让我看到了希望。

怎么解决swiper中添加视频滑动?

你好,官方滑动不能添加视频,用了这个插件可以在调试上滑动视频,但是真机IOS上却层级不够。video使用cover-view的事件也只能是tap能触发。我的需求类似「美拍」小程序的滑动视频,如果有好的方法请告知,谢谢

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.