Vue之分页器手写插件

2020-02-22  本文已影响0人  弦生_a3a3

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

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

    <title>Document</title>

</head>

<style>

    * {

        margin: 0;

        padding: 0;

        list-style: none;

    }

    .pager {

        display: flex;

    }

    .pager li {

        margin: 5px;

        width: 40px;

        line-height: 30px;

        text-align: center;

        border: 1px solid #222;

        height: 30px;

    }

    .pager li:hover {

        background: lightblue;

        opacity: .8px;

        border: 1px solid lightblue;

        cursor: pointer;

        color: #fff;

        user-select: none;

    }

    .pager li.item {

        background: #ccc;

        border: 1px solid #ccc;

        cursor: no-drop;

        color: #555;

    }

    .pager li.active {

        background: seagreen;

        opacity: .8px;

        border: 1px solid seagreen;

        cursor: pointer;

        color: #fff;

        user-select: none;

    }

</style>

<body>

    <div id="app">

        <div style=" width: 800px; height: 40px;border: 1px solid #aaa;margin: 200px auto;display: flex;justify-content: center;">

            <pager :count=count :value=1></pager>

        </div>

    </div>

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

    <script>

        Vue.component('pager', {

            props: ['count', 'value'],

            data() {

                return {

                    pagerIndex: this.value

                }

            },

            watch: {

                pagerIndex(val) {

                    if (val < 1) this.pagerIndex = 1;

                    if (val > this.count) this.pagerIndex = this.count;

                    this.$emit('closeMain', val)

                }

            },

            template: `

            <ul class="pager">

                <li @click="pagerIndex=1" :class="{item:pagerIndex==1}">首页</li>

                <li @click="pagerIndex--" :class="{item:pagerIndex==1}">《</li>

                <li v-for="item in count" @click="pagerIndex=item" :class="{active:pagerIndex===item}">

                {{item}}

                </li>

                <li @click="pagerIndex++" :class="{item:pagerIndex==count}">》</li>

                <li @click="pagerIndex=count":class="{item:pagerIndex==count}">尾页</li>

            </ul>

            `,

            mounted() {

                console.log(this.pagerIndex)

            },

        })

        let vm = new Vue({

            el: '#app',

            data() {

                return {

                    count: 10

                }

            },

            methods: {

            }

        })

    </script>

</body>

</html>

---------------------------------------------写的不好,仅供参考

上一篇 下一篇

猜你喜欢

热点阅读