图片轮播 JavaScript 的代码 事件

来源:互联网 发布:温州网络学堂手机版 编辑:程序博客网 时间:2024/04/30 04:58
window.onload = function(){
// 获取元素
var showPic = document.getElementById('showPic');
var bigImg = document.getElementById('bigImg');
var btnNext = document.getElementById('btnNext');
var btnPrev = document.getElementById('btnPrev');


var index = 1;
var timer = setInterval(showTime,3000);




// 点击事件
btnNext.onclick = function(){
index++;
showImg();
}
btnPrev.onclick = function(){
index--;
showImg();
}


// 鼠标移入事件
showPic.onmouseover = function(){
// 停止轮播
clearInterval(timer);
}


//鼠标移出事件
showPic.onmouseout = function(){
// 保证timer为全局变量的timer
timer = setInterval(showTime,3000);
}


function showTime(){
index++;
showImg();
}


function showImg(){
if(index > 6){
index = 1;
}else if(index<1){
index = 6;
}
bigImg.src = 'img/'+index+'.jpg';
}

}
0 0
原创粉丝点击