无法实现轮播
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 | <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >6-2项目作业</ title > < link href = "6-2style.css" rel = "stylesheet" type = "text/css" /> </ head > < body > < div class = "main" id = "abc" > < div class = "nav" ></ div > < div class = "pics" id = "pics" > < div class = "pic active" > < a href = "#" > < img src = "img/1.jpg" /> </ a > </ div > < div class = "pic " > < a href = "#" > < img src = "img/3.jpg" /> </ a > </ div > < div class = "pic" > < a href = "#" > < img src = "img/4.jpg" /> </ a > </ div > < div class = "pic" > < a href = "#" > < img src = "img/5.jpg" /> </ a > </ div > </ div > </ div > < script src = "6-2script.js" ></ script > </ body > </ html > |
我的想法是:利用给不同的div添加active这个class,来实现让正确的图片出现block从而达到显示的目的,但为什么不行呢,我到底是出了什么问题,看了好多遍老师的例题还是无法理解。。。。。
0
收起
正在回答 回答被采纳积分+1
4回答
慕丝6937110
2018-09-13 10:45:13
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 | //封装一个函数来获取dom元素 function getId(idName){ return document.getElementById(idName); } //一些全局变量 //var main=document.getElementById("main") var abc=getId( "abc" ); var pics=getId( "pics" ).getElementsByTagName( "div" ); var len=pics.length; var index=0; var picsAuto; //实习图片每隔2秒轮播 abc.onclick= function (){ picsAuto=setInterval( function (){ index++; pics[index].className= "pic active" ; if (index==len){ for ( var i=0; i<len; i++){ pics[i].className= "pic" ; } index=-1; } },2000); } |
这是我的js代码
慕丝6937110
2018-09-13 10:44:32
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 | { margin:0px; padding:0px; } .main{ border:2px solid blue; /*使整个盒子水平居中显示*/ height:510px; width:1200px; position:absolute; left:0px; right:0px; top:0px; bottom:0px; margin: auto ; } .nav{ height:50px; width:1200px; border:1px solid black; } .pic{ position: absolute; display:none; } .active{display:block;} /*因为先在pic里写了none,然后再在 active里写了block,根据class优先级所以有效*/ /*为什么只能用display才有效, 我想设置z-index来让优先级大的图片 显示出却不行??*/ |
这是我的css代码
前端小白入门系列课程
- 参与学习 人
- 提交作业 11218 份
- 解答问题 36712 个
从一个不会编程的小白到一个老司机是需要过程的,首先得入门,学习基础知识,然后才能进阶,最后再到精通,本专题是你走进前端世界的不二选择!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧