switchSlider 为什么不能正常显示图片,求指教
<!DOCTYPE html>
<html>
<head>
<title>幻灯片</title>
<meta charset="utf-8">
<script type="text/javascript" src="jquery-1.11.0.min.js"></script>
<style type="text/css">
*{margin:0;padding:0;}
body{
font-size: 14px;color: #555;
/*字体抗锯齿*/
font-smoothing: antialiased;
-webkit-font-smoothing: antialiased;
}
.slider,.slider .main,.slider .main .main-i{width: 100%;height: 400px;position: relative;}
/*幻灯片内容区域*/
.slider .main{overflow: hidden;}
.slider .main .main-i img{width:100%;position: absolute;left: 0;top:0;}
.slider .main .main-i .caption{position: absolute;left: 20%;top:30%;}
.slider .main .main-i .caption h2{font-size: 40px;color: #b5b5b5;text-align: right;line-height: 50px;}
.slider .main .main-i .caption h3{font-size: 70px;color: #000000;line-height: 70px;text-align: right;font-family: '幼圆'}
/*控制按钮区域*/
.slider .ctrl{width: 100%;height: 13px;line-height: 13px;position: absolute;bottom:-13px;text-align: center;}
.slider .ctrl .ctrl-i{display: inline-block;width: 150px;height: 13px;background-color: #666;box-shadow: 0 1px 1px rgba(0,0,0,.3);position: relative;margin-left: 1px;}
.slider .ctrl .ctrl-i img{width: 100%;position: absolute;left: 0;bottom: 50px;z-index: 1;opacity: 0;transition:all .2s;}
/*hover 控制按钮的样式*/
.slider .ctrl .ctrl-i:hover{background-color: #f0f0f0;}
.slider .ctrl .ctrl-i:hover img{bottom: 13px;opacity: 1;
/*倒影*/
-webkit-box-reflect:below 0 -webkit-gradient(
linear,
left top,
left bottom,
from(transparent),
color-stop(50%,transparent),
to(rgba(255,255,255,.3))
);
}
/*active 当前展现状态*/
.slider .ctrl a.ctrl-i_active,.slider .ctrl a.ctrl-i_active:hover{background-color: #000;}
.slider .ctrl a.ctrl-i_active:hover img{opacity: 0;}
/*幻灯片切换的样式*/
.slider .main .main-i{opacity: 0;position: absolute;right: 50%;top:0;transition:all .5s;}
.slider .main .main-i h2{margin-right: 60px;}
.slider .main .main-i h3{margin-right: -60px;}
.slider .main .main-i h2,.slider .main .main-i h3{transition:all .8s 1s;}
.slider .main .main-i_active{opacity: 1;right: 0;}
.slider .main .main-i_active h2,.slider .main .main-i_active h3{margin-right: 0;}
</style>
</head>
<body>
<div class="slider">
<!-- 0.修改VIEW成模版template(关键字替换),增加 template id -->
<div class="main" id="template_main">
<div class="main-i" id="main_{{index}}">
<img src="images/{{index}}.jpg" />
<div class="caption">
<h2>{{h2}}</h2>
<h3>{{h3}}</h3>
</div>
</div>
</div>
<div class="ctrl" id="template_ctrl">
<a href="javascript:switchSlider({{index}});" id="ctrl_{{index}}" class="ctrl-i"><img src="images/{{index}}.jpg" /></a>
</div>
</div>
<script type="text/javascript">
// 定义脚本,获取信息
// 1、数据定义(实际生产环境中,应由后台给出)
var data=[
{img:1,h1:'Monday',h2:'The First'},
{img:2,h1:'Tuesday',h2:'The Second'},
{img:3,h1:'Wednesday',h2:'The Third'},
{img:4,h1:'Thursday',h2:'The Fourth'}
];
// 2、通用函数
var g = function (id) {
if ( id.substr(0,1) == '.' ) {
return document.getElementsByClassName(id.substr(1));
};
return document.getElementById(id);
};
//3.添加幻灯片的操作(所有幻灯片&对应的按钮)
function addSliders(){
//3.1 获取模版
var tpl_main = g('template_main').innerHTML.replace(/^\s*/,'').replace(/\s*$/,'');
var tpl_ctrl = g('template_ctrl').innerHTML.replace(/^\s*/,'').replace(/\s*$/,'');
//3.2 定义最终输出 HTML 的变量
var out_main = [];
var out_ctrl = [];
//3.3 遍历所有数据,构建最终输出的HTML
for ( i in data) {
var _html_main = tpl_main
.replace(/{{index}}/g,data[i].img)
.replace(/{{h2}}/g,data[i].h1)
.replace(/{{h3}}/g,data[i].h2);
var _html_ctrl = tpl_ctrl.replace(/{{index}}/g,data[i].img);
out_main.push(_html_main);
out_ctrl.push(_html_ctrl);
};
//3.4 把HTML 回写到对应的DOM里面
g('template_main').innerHTML = out_main.join('');
g('template_ctrl').innerHTML = out_ctrl.join('');
};
//4.幻灯片切换
function switchSlider(n){
//5.1 获得要展现的幻灯片&控制按钮 DOM
var main = g('main_' + n);
var ctrl = g('ctrl_' + n);
//5.2 获得所有的幻灯片以及控制按钮
var clear_main = g('.main-i');
var clear_ctrl = g('.ctrl-i');
//5.3 清除他们的active样式
for (i = 0; i < clear_ctrl.length; i++) {
clear_main[i].className = clear_main[i].className.replace('main-i_active','');
clear_ctrl[i].className = clear_ctrl[i].className.replace('ctrl-i_active','');
};
//5.4 为当前控制按钮和幻灯片附加样式
main.className += 'main-i_active';
ctrl.className += 'ctrl-i_active';
};
//5.定义何时处理幻灯片输出
window.onload = function(){
addSliders();
switchSlider(1);
};
</script>
</body>
</html>