一段代码论述一个轮播思绪

一段代码论述一个轮播思绪

不BB,直接上代码:

1. if (direction === 'next') {
2.     this.index = nextIndex;
3.     /*假如是下一个,那就先吧下个的dom设置成none来挪动位置,防备用户看到*/
4.     nextEl.style.display = 'none';
5.     nextEl.style.webkitTransition = '';
6.     currentEl.style.display = 'block';
7.     currentEl.style.webkitTransition = '-webkit-transform ' + this.speed + 'ms ease-in-out';
8.     setTimeout(()=> {
9.         /*将下个放到指定的位置*/
10.         nextEl.style.webkitTransform = this.toward === 'horizontal' ? `translate3d(100%, 0, 0)` : `translate3d(0, 100%, 0)`;
11.         nextEl.style.display = 'block';
12.         nextEl.style.webkitTransition = '-webkit-transform ' + this.speed + 'ms ease-in-out';
13.         setTimeout(() => {
14.             /*最先轮播*/
15.             currentEl.style.webkitTransform = this.toward === 'horizontal' ? `translate3d(-100%, 0, 0)` : `translate3d(0, -100%, 0)`;
16.             nextEl.style.webkitTransform = `translate3d(0, 0, 0)`;
17.         }, 20);
18.     }, 10);
19. } 

注重:以上只是主思绪代码,完全的代码解读完以后会贴出来供人人参考。

代码解读:思绪是先推断转动方向,假如是向下一个节点那我就预备下一个节点到指定位置(仅合适界面一次展现一个轮播节点),假如直接挪动到指定位置肯定是不合理的,由于节点直接挪动会让用户在浏览器界面看到,然则节点是肯定要挪动啊,思绪很简单,那我给节点穿一件隐身衣(display:none),以后再挪动到指定位置用户不就看不到了,然则注重这里存在一个题目,假如直接设置设置display:none以后立马就挪动位置的话会存在一个题目,挪动的结果用户依然能看的到,这是由于非异步的JS代码在实行的过程当中是会阻挠浏览器的画图和重绘的,所以为了绕开浏览器这个机制那就运用异步好了(setTimeout),下一个节点的位置如今已预备好了,如今假如照样直接挪动的话,又会存在一个题目下一个节点居然看不到挪动,这是由于我们在预备节点的节点的时刻已将它隐蔽了,所以为了让用户看到就不得不把隐身衣脱了(display:block)才行,如许又得加一个异步(setTimeout)才见效了,如今就能够放心的实行当前节点和早已停当的下一个节点了。

完全代码以下:

下面的轮播JS部份的代码是vue写的,主要功能包含了垂直轮播,程度轮播,手势轮播等几个轮播的主要功能

export default {
    name: 'swipe',
    props: {
        /*默许是程度转动*/
        toward: {
            type: String,
            default: 'vertical'
        },
        /*是不是展现指导标志*/
        showIndicators: {
            type: Boolean,
            default: true
        },
        /*转动速率*/
        speed: {
            type: Number,
            default: 300
        },
        /*默许下标从哪儿最先*/
        defaultIndex: {
            type: Number,
            default: 0
        },
        /*自动转动距离*/
        interval: {
            type: Number,
            default: 3000
        }
    },
    data() {
        return {
            dots: [],    // 页面标记圆点
            index: this.defaultIndex,   //当前页面的下标
            pageEls: [],   // 一切的子轮播项鸠合
            timer: null, //轮播定时器对象
            noSwipe: false,  //制止转动
            startPosition: '',  //touch的肇端位置
            currentPosition: '',  //touch的现在位置
            translatePosition: '',  //touch位移的位置
            distance: 10,    //touch的位移大于10的时刻见效
        }
    },
    watch: {},
    beforeMount: function () {
    },
    mounted(){
        this.initSwipe();
        this.swipeTimeout();
        this.initTouchEvent();
    },
    computed: {
        getNum(){
            return this.pageEls.length;
        },
    },
    methods: {
        /*手动上一张*/
        prev(){
            window.clearTimeout(this.timer);
            this.swipeTimeout('prev');
        },
        /*手动下一张*/
        next(){
            window.clearTimeout(this.timer);
            this.swipeTimeout('next');
        },
        initTouchEvent(){
            let dom = this.$el.querySelector('.swipe-warp');
            dom.addEventListener('touchstart', this.handleTouchStart);
            dom.addEventListener('touchmove', this.handleTouchMove);
            dom.addEventListener('touchend', this.handleTouchEnd);
            console.log(dom);
        },
        /*初始化轮播节点*/
        initSwipe(){
            var children = this.$children;
            if (children.length === 1) {
                this.noSwipe = false;
                return;
            }
            children.forEach((child, _index)=> {
                this.pageEls.push(child.$el);
            });
        },
        /*定时轮播*/
        swipeTimeout(toward = 'next'){
            this.translate(toward, ()=> {
                this.timer = window.setTimeout(()=> {
                    this.swipeTimeout('next');
                }, this.interval);
            });
        },
        /*转动要领*/
        translate(direction = 'next', cb){
            let length = this.pageEls.length;
            let currentIndex = this.index;
            let currentEl = this.pageEls[currentIndex];
            let nextIndex = ((this.index + 1) > (length - 1)) ? 0 : (this.index + 1);
            let nextEl = this.pageEls[nextIndex];
            let pervIndex = (this.index - 1) < 0 ? (length - 1) : (this.index - 1);
            let pervEl = this.pageEls[pervIndex];
            if (direction === 'next') {
                this.index = nextIndex;
                /*假如是下一个,那就先吧下个的dom设置成none来挪动位置,防备用户看到*/
                nextEl.style.display = 'none';
                nextEl.style.webkitTransition = '';
                currentEl.style.display = 'block';
                currentEl.style.webkitTransition = '-webkit-transform ' + this.speed + 'ms ease-in-out';
                setTimeout(()=> {
                    /*将下个放到指定的位置*/
                    nextEl.style.webkitTransform = this.toward === 'horizontal' ? `translate3d(100%, 0, 0)` : `translate3d(0, 100%, 0)`;
                    nextEl.style.display = 'block';
                    nextEl.style.webkitTransition = '-webkit-transform ' + this.speed + 'ms ease-in-out';
                    setTimeout(() => {
                        /*最先轮播*/
                        currentEl.style.webkitTransform = this.toward === 'horizontal' ? `translate3d(-100%, 0, 0)` : `translate3d(0, -100%, 0)`;
                        nextEl.style.webkitTransform = `translate3d(0, 0, 0)`;
                    }, 20);
                }, 10);
            } else {
                this.index = pervIndex;
                /*假如是上一个,那就先吧上个的dom设置成none来挪动位置,防备用户看到*/
                pervEl.style.display = 'none';
                pervEl.style.webkitTransition = '';
                currentEl.style.display = 'block';
                currentEl.style.webkitTransition = '-webkit-transform ' + this.speed + 'ms ease-in-out';
                setTimeout(()=> {
                    /*将上个放到指定的位置*/
                    pervEl.style.webkitTransform = this.toward === 'horizontal' ? `translate3d(-100%, 0, 0)` : `translate3d(0, -100%, 0)`;
                    pervEl.style.display = 'block';
                    pervEl.style.webkitTransition = '-webkit-transform ' + this.speed + 'ms ease-in-out';
                    setTimeout(() => {
                        /*最先轮播*/
                        currentEl.style.webkitTransform = this.toward === 'horizontal' ? `translate3d(100%, 0, 0)` : `translate3d(0, 100%, 0)`;
                        pervEl.style.webkitTransform = `translate3d(0, 0, 0)`;
                    }, 20);
                }, 10);
            }
            this.once(currentEl, 'webkitTransitionEnd', ()=> {
                cb && cb();
            });
        },
        handleTouchStart($evnet){
            this.startPosition = this.toward === 'horizontal' ? $evnet.touches[0].clientX : $evnet.touches[0].clientY;
        },
        handleTouchMove($evnet){
            this.currentPosition = this.toward === 'horizontal' ? $evnet.touches[0].clientX : $evnet.touches[0].clientY;
            this.translatePosition = this.currentPosition - this.startPosition;
            this.translatePosition !== 0 && window.clearTimeout(this.timer);
            $evnet.preventDefault();
            $evnet.stopPropagation();
        },
        handleTouchEnd($evnet){
            this.translatePosition < 0 && this.translatePosition < -this.distance && this.swipeTimeout('next');
            this.translatePosition > 0 && this.translatePosition > this.distance && this.swipeTimeout('prev');
        },
        /*治理事宜*/
        once(el, event, fn) {
            var that = this;
            var listener = function () {
                if (fn) {
                    fn.apply(this, arguments);
                }
                that.off(el, event, listener);
            };
            this.on(el, event, listener);
        },
        /*监听事宜*/
        on(element, event, handler){
            if (element && event && handler) {
                element.addEventListener(event, handler, false);
            }
        },
        /*移除事宜*/
        off(element, event, handler) {
            if (element && event) {
                element.removeEventListener(event, handler, false);
            }
        }
    }
};
    原文作者:夜里的太阳
    原文地址: https://segmentfault.com/a/1190000010199876
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞