vue实现swiper轮播图,(有拖拽有滑动效果)

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

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

    <style>

        .imgs {

            width: 480px;

            height: 300px;

        }

        .big {

            border: 1px solid red;

            width: auto;

            height: 500px;

        }

        .left {

            margin-left: 260px;

        }

        .right {

            margin-left: 520px;

        }

        .div {

            z-index: -10px;

            display: inline-block;

        }

        .swiper-container {

            width: 480px;

            margin: 0;

            padding: 0;

        }

        .swiper-wrapper {

            height: 200px;

        }

        .swiper-slide img {

            max-width: 100%;

        }

        .swiper-slide {

            text-align: center;

            background: #fff;

            /* Center slide text vertically */

            display: -webkit-box;

            display: -ms-flexbox;

            display: -webkit-flex;

            display: flex;

            -webkit-box-pack: center;

            -ms-flex-pack: center;

            -webkit-justify-content: center;

            justify-content: center;

            -webkit-box-align: center;

            -ms-flex-align: center;

            -webkit-align-items: center;

            align-items: center;

        }

    </style>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.2/css/swiper.css">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.2/js/swiper.js"></script>

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

</head>

<body>

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

        <div class="swiper-container">

            <div class="swiper-wrapper">

                <div v-for="item in images" class="swiper-slide">

                    <img :src="item.imgSrc" alt="" @click="imgHandler" class="imgs">

                </div>

            </div>

            <div class="swiper-button-prev" slot="button-prev"></div>

            <div class="swiper-button-next" slot="button-next"></div>

            <!-- <div class="button pre-button none" id="pre-button">左</div>

            <div class="button next-button" id="next-button">右</div> -->

        </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 //一开始设置为 0

                }

            },

            // 对象内容

            methods: {

                // 下一张

                right(e) {

                    console.log(e);

                    this.currentIndex++;

                    //更改图片地址

                    if (this.currentIndex == 3) {

                        this.currentIndex = 0;

                    }

                },

                // 上一张

                left(e) {

                    console.log(e);

                    this.currentIndex--;

                    //更改图片地址

                    if (this.currentIndex < 0) {

                        this.currentIndex = 2;

                    }

                },

                imgHandler(e) {

                    console.log(e.target);

                    console.log(this);

                }

            },

            

            mounted() {

                var mySwiper = new Swiper('.swiper-container', {

                    // autoplay: 3000,

                    speed: 300,

                    loop: true,

                    navigation: {

                        nextEl: '.swiper-button-next',

                        prevEl: '.swiper-button-prev'

                    },

                    // 不要拖拽效果

                    allowTouchMove: false,

                })

            }

        })

    </script>

</body>

</html>

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

猜你喜欢

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