基于Vue.js, iview的全屏图片、视频浏览组件

先上效果图

图片描述

再上源代码

<template>
    <transition name="fade">
        <div class="media-wrapper" v-if="seeMedia">
            <Button type="text" class="media-close" shape="circle" icon="close" @click="close"></Button>
            <div class="media-controller">
                <Button-group shape='circle'>
                    <Button size="large" type="ghost" icon="ios-skipbackward" @click.prevent="prev"></Button>
                    <Button size="large" type="ghost" icon="ios-skipforward" @click.prevent="next"></Button>
                </Button-group>
            </div>
            <div class="media-content">
                <div v-for="(item,index) in data" :key="index" :class="type(index)">
                    <img :src='item' v-if="isImg(item)" @click="toggle(index)">
                    <video :src="item" v-else controls="controls" @click="toggle(index)">
                    </video>
                </div>
            </div>
        </div>
    </transition>
</template>

<script>
export default {
    name: 'cjMedia',
    data: function () {
        return {
            nowIndex: 0,
            data: [
                '/src/test/media/movie.ogg', '/src/test/media/1.jpg', '/src/test/media/2.jpg'
            ]
        }
    },
    props: {
        // data:{
        //     type:Array
        // }
    },
    methods: {
        next() {
            if (this.nowIndex == this.data.length - 1) {
                this.$Message.warning('已到达最后一张');
            } else {
                this.nowIndex++;
            }
        },
        prev() {
            if (this.nowIndex == 0) {
                this.$Message.warning('已到达第一张');
            } else {
                this.nowIndex--;
            }
        },
        type(index) {
            if (index == this.nowIndex) {
                return 'media-center'
            } else if (index - this.nowIndex == 1) {
                return 'media-right'
            } else if (index - this.nowIndex == -1) {
                return 'media-left'
            } else {
                return 'media-hide'
            }
        },
        isImg(item) {
            var ext = item.substr(item.length - 3, 3);
            var flag = ext == ('jpg' || 'png' || 'gif') ? true : false;
            return flag;
        },
        toggle(index) {
            if (index - this.nowIndex == 1) {
                this.nowIndex++;
            } else if (index - this.nowIndex == -1) {
                this.nowIndex--;
            }
        },
        close() {
            this.$store.commit('SET_MEDIA', false);
            this.nowIndex = 0;
        }
    },
    computed: {
        seeMedia() {
            return this.$store.state.seeMedia;
        }
    }
}
</script>

<style lang="scss">
.media-wrapper {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.52);
    z-index: 1010;
    i {
        color: #fff
    }
    .media-controller {
        position: absolute;
        left: 50%;
        bottom: 30px;
        transform: translateX(-50%)
    }
    .media-close {
        position: absolute;
        right: 5px;
        top: 5px;
        i {
            font-size: 30px;
        }
    }
    .media-content {
        div {
            position: absolute;
            top: 50%; // background: green;
            color: #fff;
            text-align: center;
            font-size: 30px;
            transition: all .56s ease;
            img {
                max-width: 100%;
                max-height: 100%
            }
            video {
                width: 100%;
            }
        }
        .media-center {
            height: 50%;
            width: 40%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 1011;
        }
        .media-left,
        .media-right {
            width: 25%;
            height: 35%;
            filter: grayscale(90%);
        }
        .media-right {
            left: 100%;
            transform: translate(-105%, -50%);
        }
        .media-left {
            left: 0;
            transform: translate(5%, -50%);
        }
        .media-hide {
            height: 0;
            width: 0;
            left: 50%;
            z-index: 1010;
            opacity: 0;
        }
    }
}
</style>

data传入媒体路径即可。纯原创,转载请标注来源。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值