轮播图有着许多实现方式,自己尝试用vue实现轮播图
上代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
            background-color: black;
        }
        html{
            height: 100%;
        }
        body{
            height: 100%;
            display: flex;
            justify-content: center;
        }
        .app{
            height: 100%;
            width: 100%;
            display: flex;
            justify-content: center;
        }
        img{
            width: 500px;
            height: 300px;
        }
        li{
            width: 500px;
            height: 300px;
            border: 5px solid white;
            color: white;
            position: absolute;
        }
        ul{
            width: 510px;
            display: flex;
            justify-content: center;
            align-items: center;
            overflow: hidden;
            position: relative;
        }
        .nav{
            background-color: #c3c3c3;
            color: white;
            margin-left: 10px;
            font-size: 20px;
            border-radius: 50%;
           padding: 0 8px;
            margin-bottom: 10px;
            opacity: 0.5;
        }
        .active{
            opacity: 1;
        }
        li div{
            margin-top: -30px;
        }
        .image-enter-active {
            transition: all 1.5s linear
        }
        .image-leave-active {
            transition: all 1.5s linear;
        }
        .image-enter-to {
            transform: translateX(0);
        }
        .image-enter {
            transform: translateX(100%);
        }
        .image-leave {
            transform: translateX(0);
        }
        .image-leave-to {
            transform: translateX(-100%);
        }
    </style>
</head>
<body>
<div id="app">
    <div  v-on:mouseover="stop()" v-on:mouseout="move()" class="app">
        <transition-group tag="ul" name="image">
            <li v-for="(items,index) in list" v-show="index===num" :key="index" >
                <img :src="items" alt="">
                <div>
                <span v-for="(items,index) in data" class="nav" @click="change(index)" :class="{'active':index==num}">{{items}}</span>
                </div>
            </li>
        </transition-group>
    </div>
</div>
<script src="vue.js"></script>

<script>

    new Vue({
        el:'#app',
        data(){
            return{
                data:['1','2','3'],
                time:'',
                num:0,
                list:['https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1534136662314&di=af7ec227db5e118a5626a94ab97026f3&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F4bed2e738bd4b31ccda81d7a8bd6277f9f2ff85f.jpg',
                    "https://ss3.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=4b22ec31ddc8a786a12a4c0e5708c9c7/5bafa40f4bfbfbedc5597ab474f0f736aec31ffc.jpg",
                    "https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=87d6daed02f41bd5c553eef461d881a0/f9198618367adab4b025268587d4b31c8601e47b.jpg"],
            }
        },
        methods:{
            play(){
                this.time=setInterval(()=>{
                    this.num++;
                    if(this.num==3){
                        this.num=0
                    }

                },2000)
            },
            change(i){
                this.num=i
            },
            stop(){
                clearInterval(this.time)
            },
            move(){
                this.play();
            }
        },
        created(){
            this.play();
        },

    })
</script>
</body>

</html>
Logo

前往低代码交流专区

更多推荐