web前端图片轮番Javascript实现

首先获取元素ID赋值给变量,然后对变量添加点击事件,以下是没有运用数组的情况

<script>
  	/*图片轮番*/
	window.onload=function(){
    	var pre=document.getElementById("pre");
      	var next=document.getElementById("next");
      	var pic=document.getElementById("pic");
      	n=5;
      	
      next.onclick=function(){
      	n=n+1;
        if(n>7){
        	n=5;
        }
        pic.src='{global.cfg_cmspath/}/templets/hx/images/'+n+'.png';
      }
      
      pre.onclick=function(){
      	n--;
        if(n<5){
        	n=7;
        }
        pic.src='{global.cfg_cmspath/}/templets/hx/images/'+n+'.png';
      }
    
    }  
</script>

猜你喜欢

转载自blog.csdn.net/weixin_38134491/article/details/84302891
今日推荐