ImageChange.vue 980 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <template>
  2. <div class="hello">
  3. <div class="image-switcher">
  4. <img :src="images[currentImageIndex]" alt="Switchable image" />
  5. <div class="controls">
  6. <button @click="switchImage">切换图片</button>
  7. </div>
  8. </div>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'ImageChange',
  14. data() {
  15. return {
  16. images: [require('@/assets/1.jpg'), require('@/assets/2.png')],
  17. currentImageIndex: 0,
  18. };
  19. },
  20. methods: {
  21. switchImage() {
  22. this.currentImageIndex = (this.currentImageIndex + 1) % this.images.length;
  23. },
  24. },
  25. };
  26. </script>
  27. <style scoped>
  28. .image-switcher {
  29. margin: 20px 0;
  30. text-align: center;
  31. }
  32. .image-switcher img {
  33. width: 300px;
  34. max-width: 100%;
  35. height: 300px;
  36. margin-bottom: 10px;
  37. }
  38. .controls button {
  39. padding: 8px 16px;
  40. color: white;
  41. cursor: pointer;
  42. background-color: #42b983;
  43. border: none;
  44. border-radius: 4px;
  45. }
  46. .controls button:hover {
  47. background-color: #3aa876;
  48. }
  49. </style>