用vue组件做轮播图


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        
        .menu {
            display: flex;
            position: absolute;
            bottom: 10px;
        }
        
        .menu li {
            padding: 3px 20px;
            cursor: pointer;
            user-select: none;
            background-color: black;
            margin: 0 10px;
        }
        
        .menu li.active {
            background-color: red;
        }
        
        .container {
            height: 300px;
            width: 700px;
            margin: 100px auto;
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .arrow {
            position: absolute;
            width: 30px;
            height: 30px;
            background-color: red;
            cursor: pointer;
        }
        
        .arrow.left {
            left: -50px;
        }
        
        .arrow.right {
            right: -50px;
        }
    </style>
</head>

<body>
    <div id="app">
        <showlist :pic='imgs'></showlist>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.js"></script>
    <script>
        let vm = new Vue({
            el: "#app",
            components: {
                showlist: {
                    props: ['pic'],
                    template: `
                    <div class="container">
            <div class="arrow left" @click='prev'></div>
            <div class="arrow right" @click='next'></div>
            <img :src="img" v-for='(img,i) in pic' v-show='i===activeIndex' alt="" style="width: 700px;height: 300px;">
            <ul class="menu">
                <li v-for='(city,i) in pic' @click='hover(i)' :class="{active:i===activeIndex}">
                </li>
            </ul>
        </div>`,
                    data() {
                        return {
                            activeIndex: 0,
                            timer: 0
                        }
                    },
                    mounted() {
                        this.run()
                    },
                    methods: {

                        run() {
                            this.timer = setInterval(() => {
                                if (++this.activeIndex === this.pic.length) this.activeIndex = 0
                            }, 2000);
                        },
                        hover(i) {
                            clearInterval(this.timer)
                            this.activeIndex = i
                            this.run()
                        },
                        prev() {
                            clearInterval(this.timer)
                            if (--this.activeIndex < 0) this.activeIndex = this.pic.length - 1
                            this.run()

                        },
                        next() {
                            clearInterval(this.timer)
                            if (++this.activeIndex === this.pic.length) this.activeIndex = 0
                            this.run()

                        }
                    }
                }
            },
            data() {
                return {
                    imgs: [
                        'http://p1.music.126.net/YJrFfebnzoe71WP2owqSpA==/109951165502113989.jpg?imageView&quality=89',
                        'http://p1.music.126.net/1hyoMLrqiMMIQcIrIRAPSA==/109951165501908216.jpg?imageView&quality=89',
                        'http://p1.music.126.net/0oqFGJpDWbodGfre9HO-7g==/109951165502844529.jpg?imageView&quality=89',
                        'http://p1.music.126.net/phtxahH9iEo1R8qL1kIxdA==/109951165501694969.jpg?imageView&quality=89'
                    ],

                }
            },
        })
    </script>
</body>

</html>
上一篇:Linux学习89 文件共享服务和ftp基础入门


下一篇:第89天:NumPy 排序和筛选函数