Vue 轮播图代码示例:使用按钮切换图片
<p>"<template>\n <div class="carousel">\n <img :src="currentImage" alt="carousel image" />\n <div class="buttons">\n <button @click="previousImage">Previous</button>\n <button @click="nextImage">Next</button>\n </div>\n </div>\n</template>\n\n<script>\n export default {\n data() {\n return {\n images: [\n "image1.jpg",\n "image2.jpg",\n "image3.jpg"\n ],\n currentIndex: 0\n }\n },\n computed: {\n currentImage() {\n return this.images[this.currentIndex];\n }\n },\n methods: {\n previousImage() {\n this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length;\n },\n nextImage() {\n this.currentIndex = (this.currentIndex + 1) % this.images.length;\n }\n }\n}\n</script>\n\n<style>\n.carousel {\n text-align: center;\n}\n\n.buttons {\n margin-top: 10px;\n}\n\nbutton {\n margin: 5px;\n}\n</style>\n"\n\n在上面的示例中,<code>images</code>数组包含要显示的图片的路径。<code>currentIndex</code>表示当前显示的图片的索引。<code>currentImage</code>是一个计算属性,根据<code>currentIndex</code>获取当前显示的图片路径。\n\n<code>previousImage</code>方法将<code>currentIndex</code>减1,并使用模运算确保索引循环到数组的末尾。<code>nextImage</code>方法将<code>currentIndex</code>加1,并使用模运算确保索引循环到数组的开头。\n\n在模板中,使用<code><img></code>标签显示当前图片,使用两个按钮分别调用<code>previousImage</code>和<code>nextImage</code>方法来切换图片。</p>
原文地址: https://www.cveoy.top/t/topic/pDRE 著作权归作者所有。请勿转载和采集!