小卜-Tony

导航

简单的单击图片循环播放

简单的单击图片循环播放:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <img src="imges/01.jpg" alt="" id='img'>
    <button id='prev'>上一张</button>
    <button id='next'>下一张</button>
    
</body>
    <script>
        window.onload = function(){
            //获取要用到的元素,标签

            var img = document.getElementById('img');
            var prev = document.getElementById('prev');
            var next = document.getElementById('next');
            //定义两端的临界值, 定义活动的变量 最大5张、

            var maxIndex = 5;
            var minIndex = 1;
            var activeIndex = minIndex;
            
            // 确定事件源  绑定事件函数。

            prev.onclick = function(){
                //当点击 上一张的时候, img的src 实现切换
                img.src = 'imges/0' + activeIndex + '.jpg';
                
                if(activeIndex === 1){
                    activeIndex = 5;
                }else{activeIndex --;}
            }

            next.onclick = function(){
               
                if(activeIndex === 5){
                    activeIndex = 1;
                }else{
                    activeIndex++;
                }
                img.src = 'imges/0' + activeIndex + '.jpg';

            }

            

        }
    
    
    </script>
</html>

 

posted on 2018-06-27 05:02  小卜-Tony  阅读(551)  评论(0编辑  收藏  举报