GithubHelp home page GithubHelp logo

vue-swiper's Introduction

npm npm GitHub release GitHub issues GitHub stars

NPM

vue-swiper

Swiper component. Easy to use.

Examples

basic demo

Install

npm i vue-swiper -S

Usage

import Vue from 'vue'
import Swiper from 'vue-swiper'

new Vue({
    el: 'body',
    components: {Swiper},
    methods: {
        onSlideChangeStart (currentPage) {
            console.log('onSlideChangeStart', currentPage);
        },
        onSlideChangeEnd (currentPage) {
            console.log('onSlideChangeEnd', currentPage);
        }
    }
});
<swiper v-ref:swiper
        direction="horizontal"
        :mousewheel-control="true"
        :performance-mode="false"
        :pagination-visible="true"
        :pagination-clickable="true"
        :loop="true"
        @slide-change-start="onSlideChangeStart"
        @slide-change-end="onSlideChangeEnd">
    <div>Page 1</div>
    <div>Page 2</div>
    <div>Page 3</div>
</swiper>

Api

Properties

Name Type Default Description
direction String "vertical" Could be 'horizontal' or 'vertical' (for vertical slider).
mousewheel-control Boolean true Set to true to enable navigation through slides using mouse wheel.
pagination-visible Boolean false Toggle (hide/true) pagination container visibility when click on Slider's container
pagination-clickable Boolean false If true then clicking on pagination button will cause transition to appropriate slide.
performace-mode Boolean false Disable advance effect for better performance.
loop Boolean false Set to true to enable continuous loop mode
==================== ========= ============ ===================

Methods

Method Description
next() Go next page.
prev() Go previous page.
setPage(Number) Set current page number.

Events

Name Parameters Description
slide-change-start pageNumber Fire in the beginning of animation to other slide (next or previous).
slide-change-end pageNumber Will be fired after animation to other slide (next or previous).
slide-revert-start pageNumber Fire in the beginning of animation to revert slide (no change).
slide-revert-end pageNumber Will be fired after animation to revert slide (no change).
slider-move offset Callback function, will be executed when user touch and move finger over Swiper and move it. Receives swiper instance and 'touchmove' event as an arguments.
================== ================ ============================

vue-swiper's People

Contributors

coolskull avatar gaoxiongv587 avatar maoziliang avatar ontokrat avatar valorlin 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

vue-swiper's Issues

Vue2.0 变成ref以后 _createLoop用不了了

请问为什么改成ref以后,

 this.$nextTick(function () {
    this.setPage(this.currentPage, true)
    this.slideEls = this.$refs.swiperWrap.children;
    this._createLoop()
  })
<div class="vswiper-wrap" ref="swiperWrap" :style="{
        'transform' : 'translate3d(' + translateX + 'px,' + translateY + 'px, 0)',
        'transition-duration': transitionDuration + 'ms'
     }" @transitionend="_onTransitionEnd">
    <slot></slot>
</div>
_createLoop () {
  var propName = this.isHorizontal() ? 'clientWidth' : 'clientHeight'
  var swiperWrapEl = this.$refs.swiperWrap
  if(swiperWrapEl[0]){
    var duplicateFirstChild = swiperWrapEl[0].cloneNode(true)
    var duplicateLastChild = swiperWrapEl.lastElementChild.cloneNode(true)
    swiperWrapEl.insertBefore(duplicateLastChild, swiperWrapEl.firstElementChild)
    swiperWrapEl.appendChild(duplicateFirstChild)
    this.translateOffset = -duplicateLastChild[propName]
  }
}

loop 为true就用不了了呢?

No animation on setPage

Would it be possible to remove the animation when I use the setPage() method? Would be great to view right away page(x) instead of seeing the Swiper scrolls very fast to the set page.

Thanks!

使用 vue cli 3报错

vue.runtime.esm.js?2b0e:619 [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

Ported over to work with Vue 2.0

Just wanted to post this here, thank you for the wonderful component needed to use it in a 2.0 project so ported it over (some deprecated functions).

<template>
    <div class="slider"
         :class="[direction, {'dragging': dragging}]"
         @touchstart="_onTouchStart"
         @mousedown="_onTouchStart"
         @wheel="_onWheel">
        <div class="slider-wrap"
             ref="sliderWrap"
             :style="{'transform' : 'translate3d(' + translateX + 'px,' + translateY + 'px, 0)'}"
             @transitionend="_onTransitionEnd">
            <slot></slot>
        </div>
        <div class="slider-pagination"
             v-show="paginationVisible">
            <span class="slider-pagination-bullet"
                  :class="{'active': (Number(index) + 1) === currentPage}"
                  v-for="(slide, index) in slideEls"
                  @click="paginationClickable && setPage(Number(index) + 1)"></span>
        </div>
    </div>
</template>

<script type="text/babel">
    const VERTICAL = 'vertical';
    const HORIZONTAL = 'horizontal';

    export default {
        props: {
            direction: {
                type: String,
                default: VERTICAL,
                validator: (value) => [VERTICAL, HORIZONTAL].indexOf(value) > -1
            },
            mousewheelControl: {
                type: Boolean,
                default: true
            },
            performanceMode: {
                type: Boolean,
                default: false
            },
            paginationVisible: {
                type: Boolean,
                default: false
            },
            paginationClickable: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                currentPage: 1,
                lastPage: 1,
                translateX: 0,
                translateY: 0,
                startTranslateX: 0,
                startTranslateY: 0,
                delta: 0,
                dragging: false,
                startPos: null,
                transitioning: false,
                slideEls: []
            };
        },
        mounted() {
            this._onTouchMove = this._onTouchMove.bind(this);
            this._onTouchEnd = this._onTouchEnd.bind(this);

            console.log ('this.$refs.sliderWrap', this.$refs.sliderWrap);
            this.slideEls = this.$refs.sliderWrap.children;
        },
        methods: {
            next() {
                var page = this.currentPage;
                if (page < this.slideEls.length) {
                    page++;
                    this.setPage(page);
                } else {
                    this._revert();
                }
            },
            prev() {
                var page = this.currentPage;
                if (page > 1) {
                    page--;
                    this.setPage(page);
                } else {
                    this._revert();
                }
            },
            setPage(page) {
                var propName, translateName;
                this.lastPage = this.currentPage;
                this.currentPage = page;

                if (this.isHorizontal()) {
                    propName = 'clientWidth';
                    translateName = 'translateX';
                } else {
                    propName = 'clientHeight';
                    translateName = 'translateY';
                }
                this[translateName] = -[].reduce.call(this.slideEls, function (total, el, i) {
                    return i > page - 2 ? total : total + el[propName];
                }, 0);
                this._onTransitionStart();
            },
            isHorizontal() {
                return this.direction === HORIZONTAL;
            },
            isVertical() {
                return this.direction === VERTICAL;
            },
            _onTouchStart(e) {
                this.startPos = this._getTouchPos(e);
                this.delta = 0;
                this.startTranslateX = this.translateX;
                this.startTranslateY = this.translateY;
                this.startTime = new Date().getTime();
                this.dragging = true;

                document.addEventListener('touchmove', this._onTouchMove, false);
                document.addEventListener('touchend', this._onTouchEnd, false);
                document.addEventListener('mousemove', this._onTouchMove, false);
                document.addEventListener('mouseup', this._onTouchEnd, false);
            },
            _onTouchMove(e) {
                this.delta = this._getTouchPos(e) - this.startPos;

                if (!this.performanceMode) {
                    if (this.isHorizontal()) {
                        this.translateX = this.startTranslateX + this.delta;
                        this.$emit('slider-move', this.translateX);
                    } else {
                        this.translateY = this.startTranslateY + this.delta;
                        this.$emit('slider-move', this.translateY);
                    }
                }

                if (this.isVertical() || this.isHorizontal() && Math.abs(this.delta) > 0) {
                    e.preventDefault();
                }
            },
            _onTouchEnd(e) {
                this.dragging = false;
                var isQuickAction = new Date().getTime() - this.startTime < 1000;
                if (this.delta < -100 || (isQuickAction && this.delta < -15)) {
                    this.next();
                } else if (this.delta > 100 || (isQuickAction && this.delta > 15)) {
                    this.prev();
                } else {
                    this._revert();
                }

                document.removeEventListener('touchmove', this._onTouchMove);
                document.removeEventListener('touchend', this._onTouchEnd);
                document.removeEventListener('mousemove', this._onTouchMove);
                document.removeEventListener('mouseup', this._onTouchEnd);
            },
            _onWheel(e) {
                if (this.mousewheelControl) {
                    // TODO Support apple magic mouse and trackpad.
                    if (!this.transitioning) {
                        if (e.deltaY > 0) {
                            this.next();
                        } else {
                            this.prev();
                        }
                    }

                    if (this._isPageChanged()) e.preventDefault();

                }
            },
            _revert() {
                this.setPage(this.currentPage);
            },
            _getTouchPos(e) {
                var key = this.isHorizontal() ? 'pageX' : 'pageY';
                return e.changedTouches ? e.changedTouches[0][key] : e[key];
            },
            _onTransitionStart() {
                this.transitioning = true;
                if (this._isPageChanged()) {
                    this.$emit('slide-change-start', this.currentPage);
                } else {
                    this.$emit('slide-revert-start', this.currentPage);
                }
            },
            _onTransitionEnd() {
                this.transitioning = false;
                if (this._isPageChanged()) {
                    this.$emit('slide-change-end', this.currentPage);
                } else {
                    this.$emit('slide-revert-end', this.currentPage);
                }
            },
            _isPageChanged() {
                return this.lastPage !== this.currentPage;
            }
        }
    };
</script>

嵌套swiper两个都是loop的时候,父级swiper循环到第一周子swiper会卡住(子swiper在子component里面)

data() { return { dataHomeIndex: 0, swiperOption: { notNextTick: true, loop: true, autoplay: 3000, grabCursor: true, setWrapperSize: true, autoHeight: true, paginationClickable: true, mousewheelControl: true, observeParents: true, observer: true, debugger: true, onTransitionStart: (swiper) => { // console.log(swiper) this.dataHomeIndex = swiper.realIndex console.log(this.dataHomeIndex) }, onSlideChange: () => { // console.log(123132132) } } } }, computed: { swiper() { return this.$refs.mySwiper.swiper } },

为何不会自动滚动?

Loop我都设置了true。还是不会自动滚动?看了版主的demo,好像是不支持自动滚动的。但是为何loop说可以自动滚动?

如何在单独的vue视图中使用这个组件

由于接触vue不久,想在vue单独文件中做一个轮播视图,但是总报错vue.esm.js?7a34:434 [Vue warn]: Failed to resolve directive: el, 而且这个轮播图的确是不能滑动的,单独的vue组件代码如下:

<template>
  <div class="home">
    <swiper :pagination-visible="true" direction="horizontal">
        <div><img src="../assets/pagebg1.jpg"></div>
        <div><img src="../assets/pagebg2.jpg"></div>
        <div><img src="../assets/pagebg3.jpg"></div>
    </swiper>
  </div>
</template>

<script>
import Swiper from 'vue-swiper'

export default {
  name: 'Home',
  components: {
    Swiper
  }
}
</script>

<style>
.home img {
  height: 32rem;
  width: 90%;
}
</style>

如果可以的话,希望帮我解惑一下,我不知道在这个vue文件中应该如何初始化el

Undocumented and confusing behavior of setPage method

First of all, let me thank you for creating this wonderful component. It works great and gives a native feel on mobile devices.

I just wanted to let you know I found it confusing that, when trying to set the swiper to the first element, you have to set it using setPage(1) instead of setPage(0). When using setPage(0), it's not clear it doesn't work like that, because it actually scrolls to the first item. It's upon swiping you notice there's something wrong when you have to swipe twice to get to the next slide. It would've saved me a lot of time if this was either:

  • Implemented like most people expect it to be (setPage(0)) where the first item is represented by 0, we're programmers, right?
  • If this confusing behavior was documented.

Just my two cents.

Thanks again for the wonderful component!

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.