vue实现轮播图(不可滑动)

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>轮播图</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

    <style>

        *{

            background-color:ghostwhite;

        }

        .imgs {

            width: 480px;

            height: 300px;

            margin-left: 315px;

            margin-top: 100px;

        }

        .big {           

            height: 400px;

            border: 1px solid olive;

            background-color: cornsilk;

        }

        .left {

            margin-left: 270px;

            color: crimson;

        }

        .right {

            margin-left: 520px;

            color: crimson;    

        }

        .div {

            z-index: 20px;

            margin-bottom: 200px;

        }

    </style>

</head>

<body>

    <div id="app" class="big">

        <img :src="images[currentIndex].imgSrc" alt="" @click="hander" class="imgs" id="myImg">

        <br>

        <div class="div">

            <button @click="left" class="left">&lt;</button>

            <button @click="right" class="right">&gt;</button>

        </div>

    </div>

    <script>

        let app = new Vue({

            el: "#app",

            data() {

                return {

                    images: [

                        { id: 1, imgSrc: "1.jpg" },

                        { id: 2, imgSrc: "2.jpg" },

                        { id: 3, imgSrc: "3.jpg" },

                    ],

                    //开始currentIndex设置为 0

                    currentIndex: 0 

                }

            },     

            methods: {

                // 上一张

                left(e) {

                    console.log(e);

                    this.currentIndex--;

                    

                    if (this.currentIndex < 0) {

                        this.currentIndex = 2;

                    }

                },

                // 下一张

                right(e) {

                    console.log(e);

                    this.currentIndex++;

                   

                    if (this.currentIndex == 3) {

                        this.currentIndex = 0;

                    }

                },

                hander(e) {

                    console.log(e.target);

                    console.log(this);

                }

            },

        })

    </script>

</body>

</html>

发布了28 篇原创文章 · 获赞 0 · 访问量 150

猜你喜欢

转载自blog.csdn.net/weixin_41813243/article/details/104831618