问答详情
源自:7-1 自动播放

​为什么实现不了,求大神帮助,谢谢

window.onload = function(){

var container = document.getElementById('container');

var list = document.getElementById('list');

var prev = document.getElementById('prev');

var next = document.getElementById('next');

var buttons = document.getElementById('buttons').getElementsByTagName('span');

var index = 1;

var animated = false;

function showButton(){

for(var i = 0;i < buttons.length;i++){

buttons[i].className='';}

buttons[index-1].className='on';

}

function move(offset){

    animated = ture;

var newleft =parseInt(list.style.left) + offset;

var time = 300;

var jgtime = 10;

var distance = offset/(time/jgtime);

    var go=function(){

if((distance>0&&parseInt(list.style.left)<newfelt)||(distance<0&&parseInt(list.style.left)<newleft)){

list.style.left = parseInt(list.style.left) + distance + 'px';

setTimeout(go,jgtime);}

else{

list.style.left = newleft + 'px';

if(newleft<-6000){list.style.left = -1200 + 'px';}

if(newleft>-1200){list.style.left = -6000 + 'px';}

animated = false;

}

}

go();

}

function  play(){var timer;

timer=setTimeout(function(){next.onclick;play();},3000)}

function stop(){clearTimeout(timer);}

prev.onclick = function(){

move(1200);

  if(index==1){index=5;}

        else{index = index - 1;}

showButton();

}

next.onclick = function(){

move(-1200);

if(index==5){index=1;}

        else{index = index + 1;}

showButton();

}

for(var i = 0;i<buttons.length;i++){

buttons[i].onclick = function(){

   if (animated) {return;}

var myindex=parseInt(this.getAttribute('index'));

var offset = -1200*(myindex-index);

move(offset);

index = myindex;

showButton();

}}

container.onmouseover = stop;

container.onmouseout = play;


提问者:幕布斯8502644 2018-05-24 20:32

个回答

  • 丶沉逑
    2018-07-04 17:00:22

    定时器的代码:timer=setTimeout(function(){next.onclick;play();},3000)} 应该为 

    timer = setInterval(function(){

    next.onclick();

    },3000)


  • 夜阑卧听风吹雨y
    2018-06-25 10:20:07

    if((distance>0&&parseInt(list.style.left)<newfelt)||(distance<0&&parseInt(list.style.left)<newleft)){这句代码写错了。newfelt应该是newleft

  • 半俗半雅半疯癫93
    2018-05-29 15:06:46

    我看到的问题有:paly函数没有调用,timer变量定义为局部变量这两个地方有错。其他的没细看。你自己自对比下代码吧。

    /**=================焦点轮播图================**/


    window.onload = function(){

    var container = document.getElementById('container');

    var list = document.getElementById('list');

    var buttons = document.getElementById('buttons').getElementsByTagName('span');

    var prev = document.getElementById('prev');

    var next = document.getElementById('next');

    var index = 1;

    var animated = false;

    var  timer;


    function showButton(){

    for(var i=0;i<buttons.length;i++){

    if(buttons[i].className == 'on'){

    buttons[i].className = '';

    break;

    }

    }

    buttons[index-1].className = 'on';

    }


    // next.onclick = function(){

    // list.style.left = parseInt(list.style.left) - 600 +'px';

    // }


    // prev.onclick = function(){

    // list.style.left = parseInt(list.style.left) + 600 +'px';

    // }


    function animate(offset){

    animated = true;


    var newLeft = parseInt(list.style.left)+ offset;


    var time = 300;//位移总时间

    var interval = 10;//位移间隔时间

    var speed = offset/(time/interval);//每次位移量


    function go(){

    if((speed < 0 && parseInt(list.style.left) > newLeft) || (speed > 0 && parseInt(list.style.left) < newLeft)){

    list.style.left = parseInt(list.style.left) +speed +'px';

    setTimeout(go,interval);

    }else{

    animated = false;

    list.style.left = newLeft +'px';

    if(newLeft > -600){

    list.style.left = -3000 +'px';

    }

    if(newLeft < -3000){

    list.style.left = -600 +'px';

    }

    //debugger;

    }

    }


    go();


    }


    function play (){

    timer = setInterval(function(){

    next.onclick();

    },3000)

    }


    function stop(){

    clearInterval(timer);

    }


    next.onclick = function(){

    if(index == 5){

    index = 1;

    }else{

    index += 1;

    }

    showButton();

    if(!animated){

    animate(-600);

    }

    }


    prev.onclick = function(){

    if(index == 1){

    index = 5;

    }else{

    index -= 1;

    }

    showButton();

    if(!animated){

    animate(600);

    }

    }


    for(var i = 0;i < buttons.length ;i++){

    buttons[i].onclick = function(){

    if(this.className == 'on'){

    return;

    }

    var myIndex = parseInt(this.getAttribute('index'));

    var offset = -600 * (myIndex - index);

    if(!animated){

    animate(offset);

    }

    index = myIndex;

    showButton();


    }

    }


    container.onmouseover = stop;

    container.onmouseout = play;


    play();

    }