JavaScript achieves super simple picture switching effect

Image names are sorted by numbers, using template strings

<img class="image" src="./images/1.jpg" alt="">
    <br>
    <button class="up">上一张</button>
    <button class="down">下一张</button>
    <script>
        var image = document.querySelector('.image')
        var minIndex = 1,
            maxIndex = 4,
            currentIndex = minIndex

        document.querySelector('.down').onclick = function() {
    
    
            if (currentIndex === maxIndex) {
    
    
                currentIndex = minIndex
            } else {
    
    
                currentIndex++
            }
            image.setAttribute('src', `./images/${
      
      currentIndex}.jpg`)
        }
        document.querySelector('.up').onclick = function() {
    
    
            if (currentIndex === minIndex) {
    
    
                currentIndex = maxIndex
            } else {
    
    
                currentIndex--
            }
            image.setAttribute('src', `./images/${
      
      currentIndex}.jpg`)
        }
    </script>

The effect is simple
insert image description here

Guess you like

Origin blog.csdn.net/m0_37408390/article/details/124460428