3-4图片动不起来怎么回事

3-4图片动不起来怎么回事

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
    <div class="main" id="main">
        <!--图片轮播-->
        <div class="banner" id="banner">
            <a href="">
                <div class="banner-slide slide1 slide-active"></div>
            </a>
            <a href="">
                <div class="banner-slide slide2"></div>
            </a>
            <a href="">
                <div class="banner-slide slide3"></div>
            </a>
        </div>
        <!--上一张,下一张按钮-->
        <a href="javascript:void(0)" class="button prev"></a>
        <a href="javascript:void(0)" class="button next"></a>
        <!--圆点导航-->
        <div class="dots">
            <span class="active"></span>
            <span></span>
            <span></span>
        </div>
    </div>
    <script type="script.js"></script>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
*{
    margin0;
    padding0;
}
 
ul{
    list-style:none;
}
 
body{
    font-family"微软雅黑";
    color:#14191e;
}
 
.main{
    width:1200px;
    height460px;
    margin30px auto;
    overflowhidden;
    positionrelative;
}
 
.banner{
    width:1200px;
    height460px;
    overflowhidden;
    positionrelative;
}
 
.banner-slide{
    width:1200px;
    height460px;
    background-repeatno-repeat;
    position:absolute;
    displaynone;
}
 
.slide1{
    background-imageurl("bg1.jpg");
}
 
.slide2{
    background-imageurl("bg2.jpg");
}
 
.slide3{
    background-imageurl("bg3.jpg");
}
 
.slide-active{
    display:  block;
}
 
.button{
    positionabsolute;
    width40px;
    height80px;
    left244px;
    top:50%;
    margin-top-40px;
    background:url("arrow.png"no-repeat center center;
}
 
.next{
    leftauto;
    right0;
}
 
.prev{
    transform:rotate(180deg);
}
 
.button:hover{
    background-color#333;
    opacity:0.1;
    filter:alpha(opacity=10);
}
 
.dots{
    positionabsolute;
    right20px;
    bottom:24px;
    text-alignright;
}
 
.dots span{
    display: inline-block;
    width12px;
    height12px;
    border-radius: 50%;
    background:rgba(7,17,27,0.4);
    box-shadow: 0 0 0 2px rgba(255,255,255,0.8inset;
    margin-left8px;
    line-height12px;
    cursorpointer;
}
 
.dots span.active{
    box-shadow: 0 0 0 2px rgba(7,17,27,0.4inset;
    background#fff;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//封装一个代替document.getElementById()的方法
function byId(id){
    return typeof(id)==="string"?document.getElementById(id):id;
}
 
//全局变量
var index=0,
    timer=null,
    pics=byId("banner").getElementsByTagName("div"),
    len=pics.length;
 
function slideImg(){
    var main=byId("main");
    //划过清除定时器,离开继续
    main.onmouseover=function(){
        //划过清楚定时器
    }
    main.onmouseout=function(){
        timer=setInterval(function(){
            index++;
            if (index>=len) {
                index=0;
            }
            //切换图片
            changeImg();
        },2000)
    }
}
 
 
//切换图片
function changeImg(){
    //遍历banner下都有的div,将其隐藏
    for(var i=0;i<len;i++){
        pics[i].style.display="none";
    }
    pics[index].style.display="block";
}

跟着老师写的,,哪儿错啦。

正在回答 回答被采纳积分+1

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

3回答
卡布琦诺 2017-12-12 18:19:32

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

1
2
3
4
5
function byId(id) { 
    // return typeof(id) === "string"?document.getElementById("id"):id;
    // id没有引号
    return typeof(id)==="string"?document.getElementById(id):id; 
}

要细心一些哦~另外,课程列表的右侧有课程的源码,你可以下载对比着进行学习,祝学习愉快~

提问者 qq_陌_45 2017-12-12 17:22:09
1
//封装一个代替document.getElementById()的方法 function byId(id){ return typeof(id)==="string"?document.getElementById("id"):id; } //全局变量 var index=0, timer=null, pics=byId("banner").getElementsByTagName("div"), len=pics.length; function slideImg(){ var main=byId("main"); //划过清除定时器,离开继续 main.onmouseover=function(){ //划过清楚定时器 if(timer) clearInterval(timer); } main.onmouseout=function(){ timer=setInterval(function(){ index++; if (index>=len) { index=0; } //切换图片 changeImg(); },3000) } //自动在main上触发鼠标离开的事件 main.onmouseout(); } //切换图片 function changeImg(){ //遍历banner下都有的div,将其隐藏 for(var i=0;i<len;i++){ pics[i].style.display="none"; } //根据index索引找到当前div,将其显示出来 pics[index].style.display="block"; } slideImg();

加上了,也没动

卡布琦诺 2017-12-12 17:12:19

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

在js代码的最后的地方缺少调用slideImg();,添加上即可,祝学习愉快~

  • 提问者 qq_陌_45 #1
    //封装一个代替document.getElementById()的方法 function byId(id){ return typeof(id)==="string"?document.getElementById("id"):id; } //全局变量 var index=0, timer=null, pics=byId("banner").getElementsByTagName("div"), len=pics.length; function slideImg(){ var main=byId("main"); //划过清除定时器,离开继续 main.onmouseover=function(){ //划过清楚定时器 if(timer) clearInterval(timer); } main.onmouseout=function(){ timer=setInterval(function(){ index++; if (index>=len) { index=0; } //切换图片 changeImg(); },3000) } //自动在main上触发鼠标离开的事件 main.onmouseout(); } //切换图片 function changeImg(){ //遍历banner下都有的div,将其隐藏 for(var i=0;i<len;i++){ pics[i].style.display="none"; } //根据index索引找到当前div,将其显示出来 pics[index].style.display="block"; } slideImg(); 老师我加上了,也没动,怎么回事
    2017-12-12 17:21:32
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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