哪里出错了,不会轮播啊

哪里出错了,不会轮播啊

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>图片特效</title>
 <link rel="stylesheet" type="text/css" href="css/test.css">
</head>
<body>
 <!--总盒子-->
<div class="main" id="main"> <div class="banner" id="banner">    <!--图片区-->
    <div class="picture" id="picture">
    <img src="img/bg1.jpg" class="show"> 
    <img src="img/bg2.jpg"> 
    <img src="img/bg3.jpg"> 
    </div>
   </div></div>
<script type="text/javascript" src="js/test.js"></script>
</body>
</html>


*{
 margin:0px;
 padding:0px;
}


.main{
 width:1200px;
 height:460px;
 margin:30px auto;
 position:relative; 
 overflow:hidden;
 
 
}

.banner{
    width:1200px;
 height:460px;
 position:relative;
 overflow:hidden;
}

.pictures img{
 position: absolute; 
 float:left;
 display:none;
}

.picture .show{
   display:block;
}


timer=null;
index=0;

//获取id的函数
function byId(id){
 return typeof(id)==="string"?document.getElementById(id):id;
}


var pictures=byId(picture).getElementsByTagName("img");
var size=pictures.length;


function pictureChange() {
 var main=document.getElementById("main");

//鼠标停在界面上
 main.onmouseover=function(){
       if(timer)
        clearInterval(timer);
        
 }
   //鼠标离开界面
   main.onmouseout=function(){
     timer=Interval(function(){
       index++;
       if(index>=3){
        index=0;
       }

        function Changeshow()

     },3000);

   }


   function Changeshow(){
    for(var i=0;i<size;i++){
     pictures[i].className=" ";
    }
     pictures[index].className="show";

   }

main.onmouseout();

}

pictureChange();


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,这里老师将所有的文件都贴出同学复制后,再来重试一下,并且同学使用的是什么浏览器,建议使用Chrome浏览器,例如:

html:

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>图片特效</title>
 <link rel="stylesheet" type="text/css" href="test2_1.css">
</head>
<body>
 <!--总盒子-->
	<div class="main" id="main">
		 <div class="banner" id="banner">    <!--图片区-->
			    <div class="picture" id="picture">
				    <img src="img/bg1.jpg" class="show"> 
				    <img src="img/bg2.jpg"> 
				    <img src="img/bg3.jpg"> 
			    </div>
  		</div>
   </div>
<script type="text/javascript" src="test2_1.js"></script>
</body>
</html>

css:

*{
 margin:0px;
 padding:0px;
}


.main{
 width:1200px;
 height:460px;
 margin:30px auto; 
 position:relative; 
 overflow:hidden;
 
 
}

.banner{
    width:1200px;
 height:460px;
 position:relative;
 overflow:hidden;
}

.pictures img{
 position: absolute; 
 float:left;
 display:none;
}

.picture .show{
   display:block;
}

js:

timer=null; index=0; 
//获取id的函数
 function byId(id){ 
      return typeof(id)==="string"?document.getElementById(id):id;
    } 
    var pictures=byId(picture).getElementsByTagName("img"); 
    var size=pictures.length; 
    function pictureChange() { 
    var main=document.getElementById("main"); 
    //鼠标停在界面上 m
      main.onmouseover=function(){
        if(timer) clearInterval(timer);
      } 
      //鼠标离开界面 
      main.onmouseout=function(){
          timer=setInterval(function(){ 
          index++; if(index>=3){ 
             index=0; 
          } 
          changeShow() 
        },3000);
     }
    
    function changeShow(){ 
        for(var i=0;i<size;i++){ 
            pictures[i].style.display="none"; 
        } 
        pictures[index].style.display="block";
       } 
      main.onmouseout();
    }
pictureChange();

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

好帮手慕小班 2019-11-20 18:29:17

同学你好,1、定时器是-->setInterval() 方法,可按照指定的周期(以毫秒计)来调用函数或计算表达式,对应在timer中需要添加调用setInterval方法。

        2、Changeshow方法的调用不要求在前面再加上function,加上function就是声明定义了不是调用,所以需要去掉Interval中调用Changeshow方法前的function

http://img1.sycdn.imooc.com//climg/5dd5137e09b805e302240207.jpg

        3、在Changeshow直接将className设置为空,并不能把img属性的display设置为none,所以这里建议修改为:

http://img1.sycdn.imooc.com//climg/5dd5155b0939308805090214.jpg

综上所述,修改后的js代码如下:

timer=null;
index=0;

//获取id的函数
function byId(id){
 return typeof(id)==="string"?document.getElementById(id):id;
}


var pictures=byId(picture).getElementsByTagName("img");
var size=pictures.length;


function pictureChange() {
 var main=document.getElementById("main");

//鼠标停在界面上
 main.onmouseover=function(){
       if(timer)
        clearInterval(timer);
         
 }
   //鼠标离开界面
   main.onmouseout=function(){
     timer=setInterval(function(){
       index++;
       if(index>=3){
        index=0;
       }

       Changeshow();

     },3000)

   }


   function Changeshow(){
    for(var i=0;i<size;i++){
    // pictures[i].className=" ";
     pictures[i].style.display="none";
    }
     //pictures[index].className = "show";
     pictures[index].style.display = "block";
     
   }

main.onmouseout();

}

pictureChange();

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

  • 提问者 慕仔0431810 #1
    还是不行啊老师,图片一直不会轮播
    2019-11-20 18:55:28
  • 好帮手慕小班 回复 提问者 慕仔0431810 #2
    同学你好,1、这里老师测试修改后的代码,是可以正常轮播的,同学根据上面的修改还是不能轮播吗,同学可以清理一下浏览器的缓存再来重试一下。2、如果还是不行,同学可以把自己修改后的代码再次贴出,老师来测试一下。如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
    2019-11-21 10:00:48
  • 好帮手慕小班 回复 提问者 慕仔0431810 #3
    同学你好,1、这里老师测试修改后的代码,是可以正常轮播的,同学根据上面的修改还是不能轮播吗,同学可以清理一下浏览器的缓存再来重试一下。2、如果还是不行,同学可以把自己修改后的代码再次贴出,老师来测试一下。如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
    2019-11-21 10:04:41
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师