screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> </head> <body> <div id="app"> <h2>Vue Js Change Image Every 5 Seconds Example</h2> <img :src="currentImage" alt="Image" /> </div> <script type="module"> const app = Vue.createApp({ data() { return { images: [ "https://www.sarkarinaukriexams.com/images/import/sne4446407201.png", "https://www.sarkarinaukriexams.com/images/import/sne86811832.png", "https://www.sarkarinaukriexams.com/images/import/sne10272423583.png", "https://www.sarkarinaukriexams.com/images/import/sne1586776004.png", "https://www.sarkarinaukriexams.com/images/import/sne20464172895.png", // Add more image URLs as needed ], currentIndex: 0, intervalId: null, }; }, computed: { currentImage() { return this.images[this.currentIndex]; }, }, methods: { startImageRotation() { this.intervalId = setInterval(this.changeImage, 5000); // 5000 milliseconds = 5 seconds }, stopImageRotation() { clearInterval(this.intervalId); }, changeImage() { this.currentIndex = (this.currentIndex + 1) % this.images.length; }, }, created() { this.startImageRotation(); // Start the image rotation when the component is created }, beforeDestroy() { this.stopImageRotation(); // Stop the interval when the component is destroyed }, }); app.mount("#app"); </script> <style> #app { margin: 0 auto; width: 500px; text-align: center; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); padding: 20px; } img { max-width: 100%; border-radius: 10px; } </style> </body> </html>