banner图js轮播,为啥第一个图要鼠标才能播放 50
window.onload=function(){varcontent=document.getElementById("content");varbanner=docu...
window.onload = function () {
var content = document.getElementById("content");
var banner = document.getElementById("banner");
var li = document.querySelectorAll("#banner li");
var spanNode = document.querySelectorAll("#buttons span");
var img = document.getElementsByTagName("img")[0];
//前后按钮
var prev = document.getElementById('prev');
var next = document.getElementById('next');
//索引
var index = 1;
var timer = 0;
//给 content 设置宽,高,以及overflow:hidder
content.style.width = img.offsetWidth + "px";
content.style.height = img.offsetHeight + "px";
content .style.overflow = "hidden";
//给banner 设置宽高
banner.style.height = img.offsetHeight + "px";
banner.style.width = img.offsetWidth * li.length + "px";
banner.style.left = "-720px";
//轮播图
function animate(offset) {
banner.style.transition = "0.5s";
banner.style.left = -parseInt(offset )* index + "px";
}
next.onclick = function () {
//点击下一页:移动
if(index == spanNode.length){
index = 0;
}
index ++;
console.log("索引index:"+index);
animate(img.offsetWidth)
showButton()
}
prev.onclick = function () {
if(index == 1){
index = li.length-1;
}
index--;
animate(img.offsetWidth)
showButton()
}
function showButton() {
for (var i = 0; i< spanNode.length; i++){
spanNode[i].className = '';
}
console.log("小圆点的index值:"+index);
spanNode[index-1].className = "on";
}
//自动轮播
function play() {
timer = setInterval(function () {
banner.style.transition = "none";
setTimeout(function () {
next.onclick();
},200)
},3000);
} 展开
var content = document.getElementById("content");
var banner = document.getElementById("banner");
var li = document.querySelectorAll("#banner li");
var spanNode = document.querySelectorAll("#buttons span");
var img = document.getElementsByTagName("img")[0];
//前后按钮
var prev = document.getElementById('prev');
var next = document.getElementById('next');
//索引
var index = 1;
var timer = 0;
//给 content 设置宽,高,以及overflow:hidder
content.style.width = img.offsetWidth + "px";
content.style.height = img.offsetHeight + "px";
content .style.overflow = "hidden";
//给banner 设置宽高
banner.style.height = img.offsetHeight + "px";
banner.style.width = img.offsetWidth * li.length + "px";
banner.style.left = "-720px";
//轮播图
function animate(offset) {
banner.style.transition = "0.5s";
banner.style.left = -parseInt(offset )* index + "px";
}
next.onclick = function () {
//点击下一页:移动
if(index == spanNode.length){
index = 0;
}
index ++;
console.log("索引index:"+index);
animate(img.offsetWidth)
showButton()
}
prev.onclick = function () {
if(index == 1){
index = li.length-1;
}
index--;
animate(img.offsetWidth)
showButton()
}
function showButton() {
for (var i = 0; i< spanNode.length; i++){
spanNode[i].className = '';
}
console.log("小圆点的index值:"+index);
spanNode[index-1].className = "on";
}
//自动轮播
function play() {
timer = setInterval(function () {
banner.style.transition = "none";
setTimeout(function () {
next.onclick();
},200)
},3000);
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询