学习小结,vue实现图片切换。图片地址用数组保存,通过下表访问。v-bind指令设置元素属性src,v-show设置显示状态。

<body>
    <div id="app">
        <img v-bind:src="imgArr[index]">
        <button @click="prev" v-show="index!=0">上一张</button>
        <button @click="next" v-show="index<imgArr.length-1">下一张</button>
    </div>
    <script>
        var app = new Vue({
            el:"#app",
            data:{
                imgArr:["https://ci.xiaohongshu.com/881ae53d-2f84-4257-86c3-10e9805ad19f","https://public-cdn.mokahr.com/e6be3979-69fa-452d-88f0-b015880afe18.png","https://careers.hellobike.com/img/footer-icon.e24f3d8c.png"],
                index:0
            },
            methods:{
                prev:function(){
                    this.index--
                },
                next:function(){
                    this.index++
                }
            }
        })
    </script>
</body>

页面效果如下: 

Logo

前往低代码交流专区

更多推荐