老师,怎样修改一下script文件,能实现自动轮播,我改的实现不了

来源:5-1 事件参数

幕布斯1062488

2020-08-20 15:47:14

$(document).ready(function() {

   var timer = null;

   var index = 0;

   $('a').click(function() {

       index = $(this).index();

       wrapper();

       stopAutoPlay();

   });

   $('a').mouseenter(function() {

       index = $(this).index();

       wrapper();

       stopAutoPlay();

   });


   var wrapper = function() {

       $('a').eq(index)

           .css({ 'background': 'yellow' })

           .siblings()

           .css({ 'background': 'white' });

       $('img').eq(index)

           .css({ 'opacity': '1' })

           .siblings()

           .css({ 'opacity': '0' });

   };

   $(document).keydown(function(event) {

       if (event.keyCode == 37) {

           index = index > 0 ? --index : $('a').length - 1;

       } else if (event.keyCode == 39) {

           index = index < $('a').length - 1 ? ++index : 0;

       } else { return false; }

       wrapper();

       stopAutoPlay();

   });

   var autoPlay = function() {

       timer = setInterval(function() {

           if (index > $('a').length - 1) {

               index = 0;

               ++index;

           } else if (index < 0) {

               index = $('a').length - 1;

               --index;

           };

           wrapper();

       }, 1000);

   }

   autoPlay();

   var stopAutoPlay = function() {

       if (timer) {

           clearInterval(timer);

       }

   }

})


写回答

1回答

好帮手慕慕子

2020-08-20

同学你好,针对同学的问题解答如下:因为没有在定时器中设置改变index的值,所以默认index的值一直都是0 ,无法实现自动轮播。建议修改:添加index++,每隔一段时间就增加index的值。实现自动轮播。

http://img.mukewang.com/climg/5f3e33c8099f98ea07300426.jpg

如果我的回答帮助到了你,欢迎采纳,祝学习愉快~

1

0 学习 · 14456 问题

查看课程