美文网首页
vue实现默认icon和选中icon的切换,类似tabbar

vue实现默认icon和选中icon的切换,类似tabbar

作者: 似朝朝我心 | 来源:发表于2022-03-03 23:12 被阅读0次

    效果展示:


    方法1:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <title></title>
    </head>
    <style>
        .img1 {
            width: 40px;
            height: 40px;
            cursor: pointer;
        }
        .img2 {
            width: 40px;
            height: 40px;
            cursor: pointer;
        }
        
    </style>
    <body>
        <div id="app">
            <div @click="showImg">
                <div v-if="!showSearch">
                    <img class="img1" src="d.png" />
                    <p>收藏</p>
                </div>
                <div v-if="showSearch">
                    <img class="img2" src="s.png" />
                    <p style="color: green;">收藏</p>
                </div>
            </div>
    
        </div>
        <script type="text/javascript">
            var app = new Vue({
                el: '#app',
                data: {
                    showSearch: false,
                },
                methods: {
                    showImg() {
                        this.showSearch = !this.showSearch;
                    },
                },
            })
        </script>
    </body>
    
    </html>
    

    方法2:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <title></title>
    </head>
    <style>
        .box1 {
            display: block;
        }
        .box2 {
            display: none;
        }
    </style>
    
    <body>
        <div id="app">
            <div class="box1" ref="box1" @click="change">
                <img src="d.png" alt="">
            </div>
            <div class="box2" ref="box2" @click="change2">
                <img src="s.png" alt="">
                收藏
            </div>
        </div>
        <script type="text/javascript">
            var app = new Vue({
                el: '#app',
                data: {
                },
                methods: {
                    change() {
                        this.$refs.box1.style.display = 'none'
                        this.$refs.box2.style.display = 'block'
                    },
                    change2() {
                        this.$refs.box2.style.display = 'none'
                        this.$refs.box1.style.display = 'block'
                    },
                }
    
            })
        </script>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:vue实现默认icon和选中icon的切换,类似tabbar

          本文链接:https://www.haomeiwen.com/subject/dicprrtx.html