Build a HTML, JS code for a button type <input type="range"> to change images inside a…
- User
Build a HTML, JS code for a button type to change images inside a element.
- Assistant
Here's an example code that uses an
<input type="range">element to change images in a<div>element using JavaScript:HTML:
<div id="image-gallery"> <img src="image1.jpg" alt="Image 1"> <img src="image2.jpg" alt="Image 2"> <img src="image3.jpg" alt="Image 3"> </div> <label for="image-slider">Change image:</label> <input type="range" id="image-slider" value="0" min="0" max="2">JavaScript:
const gallery = document.getElementById('image-gallery'); const slider = document.getElementById('image-slider'); function updateImage() { // Get the index of the selected image based on the slider value const index = parseInt(slider.value); // Hide all images in the gallery for (let i = 0; i < gallery.children.length; i++) { gallery.children[i].style.display = 'none'; } // Show the selected image gallery.children[index].style.display = 'block'; } // Call the updateImage function whenever the slider value changes slider.addEventListener('input', updateImage); // Call the updateImage function initially to show the default image updateImage();