卡片式轮播

<template>
  <transition name="fade">
    <div @touchend='end' @touchstart='start' @touchmove='move' class="swiper">
      <div class="item" @click="chooseItem(item,index)" v-for="(item, index) in imgs" :style="config5[index]" :key="item.id">
        <img :src="item.imgSrc" style="position:absolute;">
      </div>
    </div>
  </transition>
</template>
<script>
// import { ce } from 'migu-sdk';
export default {
    name: 'zt',
    data() {
        return {
            activityId: 'MAC_ZP_KF_MGHZJRQJXPX2019',
            stage: '', // 当前活动状态
            voteInfo: '', //当前所有票数列表
            loading: true,
            initCenterIndez: 3, // 初始化中间的index(img数组中)
            currentIndex: 3, //当前中间imgs数组中index
            centerInfo: '', // 当前中间信息
            startX: '',
            endX: '',
            imgs: [
                {
                    name: '巴耶克',
                    imgSrc:
                        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1578753283849&di=926ab993242f5124613b7cb61134ff75&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F82c9e6899e7f33bf867f207e871d61e24f473834.jpg',
                    index: 0
                },
                {
                    name: '马拉卡',
                    imgSrc:
                        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1579348146&di=5548595a45061454b4b5491f65a74a0a&imgtype=jpg&er=1&src=http%3A%2F%2Fimg23.aspzz.cn%2Fuploads%2Fallimg%2Fc190116%2F154KD32553610-1044U.jpg',
                    index: 1
                },
                {
                    name: '艾吉奥',
                    imgSrc:
                        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1579348251&di=7ca30312f94789c03154db45b176b599&imgtype=jpg&er=1&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2Fff1a45aeb6126ba1d0bd6c9e75d192ec83b9fb1a.jpg',
                    index: 2
                },
                {
                    name: '巴耶克白色海报',
                    imgSrc:
                        'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1259084891,2660179829&fm=26&gp=0.jpg',
                    index: 3
                },
                {
                    name: '巴耶克沙漠',
                    imgSrc:
                        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1578753631199&di=c7033bf418e21b926a33e102de7a23d5&imgtype=jpg&src=http%3A%2F%2Fimg0.imgtn.bdimg.com%2Fit%2Fu%3D2242226280%2C1896230013%26fm%3D214%26gp%3D0.jpg',
                    index: 4
                }
            ],
            previous: 0,
            config5: [
                {
                    id: '-A',
                    position: 'absolute',
                    width: '22%',
                    height: '72%',
                    top: '16.6%',
                    left: '-22%',
                    opacity: 0,
                    zIndex: 0,
                    transition: '.2s'
                },
                {
                    id: 'A',
                    position: 'absolute',
                    width: '32%',
                    height: '72%',
                    top: '16.6%',
                    left: '0%',
                    opacity: 1,
                    zIndex: 1,
                    transition: '.2s'
                },
                {
                    id: 'B',
                    position: 'absolute',
                    width: '40%',
                    height: '82%',
                    top: '9%',
                    left: '13%',
                    opacity: 1,
                    zIndex: 2,
                    transition: '.2s'
                },
                {
                    id: 'center',
                    position: 'absolute',
                    width: '45%',
                    height: '100%',
                    top: '0px',
                    left: '50%',
                    marginLeft: '-22.5%',
                    opacity: 1,
                    zIndex: 4,
                    transition: '.2s'
                },
                {
                    id: 'D',
                    position: 'absolute',
                    width: '40%',
                    height: '82%',
                    top: '9%',
                    left: '48%',
                    opacity: 1,
                    zIndex: 2,
                    transition: '.2s'
                },
                {
                    id: 'E',
                    position: 'absolute',
                    width: '32%',
                    height: '72%',
                    top: '16.6%',
                    left: '68%',
                    opacity: 1,
                    zIndex: 1,
                    transition: '.2s'
                },
                {
                    id: 'E+',
                    position: 'absolute',
                    width: '22%',
                    height: '72%',
                    top: '16.6%',
                    left: '100%',
                    opacity: 0,
                    zIndex: 0,
                    transition: '.2s'
                }
            ],
            itemNormal: 'itemNormal',
            item: 'item',
            itemSelect: 'itemSelect'
        };
    },
    methods: {
        // 获取数据
        async getData() {
            await this.getSingerColumn(); // 获取投票歌手信息
            this.$nextTick(() => {
                this.loading = false;
            });
        },
        // 获取参赛选手栏目
        async getSingerColumn() {
            // this.imgs.sort(() => Math.random() - 0.5);
            // this.imgs = res.nodes;
            // this.imgs.length = 8;
        },

        // 滑动上一个
        prev(index) {
            console.log('上一个');
            this.config5.push(this.config5.shift());
            this.currentIndex = this.currentIndex - 1;
            if (this.currentIndex < 0) {
                this.currentIndex = this.imgs.length - 1;
            }
            this.centerCard();
            this.centerIndex('prev');
        },
        // 滑动下一个
        next() {
            console.log('下一个');
            this.config5.unshift(this.config5.pop());
            this.currentIndex = this.currentIndex + 1;
            if (this.currentIndex > this.imgs.length - 1) {
                this.currentIndex = 0;
            }
            this.centerCard();
            this.centerIndex('next');
            // console.log(this.currentIndex);
        },
        // 开始移动端滑动屏幕
        start(event) {
            this.startX = event.changedTouches[0].clientX;
            this.startY = event.changedTouches[0].clientY;
        },
        // 连续滑动放开注释
        move(event) {
            this.endY = event.changedTouches[0].clientY;
            this.endX = event.changedTouches[0].clientX;
            this.stopDefault(event);
            this.interval = this.endX - this.startX;
            if (this.interval > 40) {
                this.startX = this.endX;
                this.prev();
            }
            if (this.interval < -40) {
                this.startX = this.endX;
                this.next();
            }
        },
        // 滑动放开注释
        end(event) {
            // this.endY = event.changedTouches[0].clientY;
            // this.endX = event.changedTouches[0].clientX;
            //this.stopDefault(event)
            // this.formatSwiper();
        },
        formatSwiper() {
            if (this.startX > this.endX) {
                console.log('左边滑动');
                if (this.startX > this.endX + 40) {
                    this.next();
                }
            } else {
                console.log('右边滑动');
                if (this.endX > this.startX + 40) {
                    this.prev();
                }
            }
        },
        // 阻止touchmove的横向默认事件(ios快捷操作会关闭页面)
        stopDefault(event) {
            let differenceY = this.endY - this.startY;
            let differenceX = this.endX - this.startX;
            if (Math.abs(differenceX) > Math.abs(differenceY)) {
                event.preventDefault();
            }
        },
        // 当前imgs在位置上的index(并非img数组的index)
        centerIndex(val) {
            if (val == 'prev') {
                for (let val of this.imgs) {
                    if (val.index == this.imgs.length - 1) {
                        val.index = 0;
                    } else {
                        val.index = val.index + 1;
                    }
                }
            } else {
                for (let val of this.imgs) {
                    if (val.index == 0) {
                        val.index = this.imgs.length - 1;
                    } else {
                        val.index = val.index - 1;
                    }
                }
            }
        },
        // 点击
        chooseItem(item, index) {
            console.log(item);
            console.log(this.initCenterIndez);
            let cycles = item.index;
            if (item.index < this.initCenterIndez) {
                for (let i = 0; i < this.initCenterIndez - cycles; i++) {
                    console.log(item.index);
                    this.prev();
                }
            } else if (item.index > this.initCenterIndez) {
                for (let i = -1; i < item.index - this.initCenterIndez; i++) {
                    this.next();
                }
            } else if (item.index == this.initCenterIndez) {
            }
        },
        // 计算中间卡片信息
        centerCard() {
            this.centerInfo = this.imgs[this.currentIndex];
            this.$emit('centerInfo', this.centerInfo);
        },
        // 加入样式位置的index
        addCardStyle() {
            if (this.imgs.length >= 7) {
                this.initCenterIndez = 3;
                let addtime = this.imgs.length - 7;
                for (let i = 0; i < addtime; i++) {
                    this.config5.push({
                        id: 'other',
                        position: 'absolute',
                        width: '45%',
                        height: '100%',
                        top: '0px',
                        left: '50%',
                        marginLeft: '-22.5%',
                        opacity: 0,
                        transition: '.1s'
                    });
                }
            } else {
                this.config5.pop();
                this.config5.shift();
                this.currentIndex = 2;
                this.initCenterIndez = 2;
            }
        }
    },
    async created() {
        await this.getData();
        await this.addCardStyle(); // 加入样式位置的index
        await this.centerCard(); // 获取中间卡片信息
    }
};
</script>

<style lang="less" scoped>
.swiper {
    width: 100%;
    // border: 1px blue solid;
    height: 100%;
    min-height: 400px;
    position: relative;
    overflow: hidden;

    .item {
        opacity: 0;
        border: 5px #fec473 solid;
        border-radius: 10px;
        background-size: 100% 100%;
        // background-image: url('../imgs/cardBack.png');
        position: relative;
        img {
            height: 100%;
            width: 100%;
            border-radius: 10px;
        }
    }
}
</style>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,941评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,397评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,345评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,851评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,868评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,688评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,414评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,319评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,775评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,945评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,096评论 1 350
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,789评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,437评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,993评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,107评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,308评论 3 372
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,037评论 2 355

推荐阅读更多精彩内容