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

2022-03-03  本文已影响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>
上一篇下一篇

猜你喜欢

热点阅读