问答详情
源自:3-2 动态调整图片高度

( _ _)ノ|求帮忙看下错在哪里了。。活生生看了一晚上眼残看不出啊


<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>sliderShow</title>
   <link href="CSS/main.css" rel="stylesheet">
   <!--script src="JS/control.js"></script-->
</head>
<body>
   <div class="slider">
       <!--0.修改VIEW->Template(关键字替换),增加template id-->
<div class="main"  id="template_main">
           <div class="main-i" id="main_{{index}}">
               <div class="caption">
                   <h2>{{h2}}</h2>
                   <h3>{{h3}}</h3>
               </div>
               <img src="imgs/{{index}}.jpg"/>
           </div>
       </div>
   </div>
   <div class="ctrl" id="template_ctrl">
       <a class="ctrl-i" id="ctrl_{{index}}" href="
       javascript:switchSlider({{index}});"><img src="imgs/{{index}}.jpg"/></a>
   </div>

   <script type="text/javascript">
       //1.数据定义(实际生产环境中应由后台给出)
var data = [
           {img:1,h1:'Creative',h2:'DUET'},
           {img:2,h1:'Friendly',h2:'DEVIL'},
           {img:3,h1:'Tranquilent',h2:'COMPATRIOT'},
           {img:4,h1:'Insecure',h2:'HUSSLER'},
           {img:5,h1:'Loving',h2:'REBEL'},
           {img:6,h1:'Crazy',h2:'FRIEND'}
       ];

       //2.定义通用函数
var g = function(id){
           if(id.substr(0,1) == '.'){ //通过classname获取
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*$/,'');    //去除前面和后面的空白符

           //3.2定义最终输出 HTML的变量
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)    //正则表达跟g表示全局替换
.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('');

       }
       //5.幻灯片切换
function switchSlider(n){
           //5.1获得要展现的幻灯片和控制按钮
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';
       }

       //6.定义合适处理幻灯片输出
window.onload = function(){
           addSliders();
           switchSlider(2)
       }

   </script>
</body>
</html>

提问者:加菲猫饿啦 2015-12-09 22:46

个回答

  • 会飞的龙猫
    2017-09-07 10:57:06
    已采纳

    你这段代码放错位置了

    “  <div class="ctrl" id="template_ctrl">
           <a class="ctrl-i" id="ctrl_{{index}}" href="
           javascript:switchSlider({{index}});"><img src="imgs/{{index}}.jpg"/></a>
       </div>”

    应该放在<div class="slider"></div>里面,和<div class="main">同级

  • qq_loading
    2015-12-21 23:52:25

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

    <title>幻灯片</title>

    <style type="text/css">

    *{

    padding:0;

    margin:0;

    }

    body {

    padding:50px 0;

    background-color: #fff;

    font-size:14px;

    font-family: 'Avenir Next';

    color: #555;

    -webkit-font-smoothing:antialiased;/*字体抗锯齿*/

    }

    .slider .main .main-i,

    .slider .main,

    .slider{

    width:100%;

    height:400px;

    position:relative;/*相对定位*/

    }

    /*幻灯片区域*/

    .slider .main{

    overflow: hidden;

    }

    /*每一个幻灯片的样式*/

    .slider .main .main-i{}

    .slider .main .main-i img{

    width:100%;

    position: absolute;

    left:0;

    top:0;

    z-index: 1;

    }

    .slider .main .main-i .caption{

    position:absolute;

    right:50%;

    top:30%;

    z-index: 9 ;

    }

    .slider .main .main-i .caption h2{

    font-size: 40px;

    line-height: 50px;

    color: #b5b5b5;

    text-align:right;

    }

    .slider .main .main-i .caption h3{

    font-size: 70px;

    line-height: 70px;

    color: #000000;

    text-align:right;

    font-family: 'Open Sans Condensed';

    }

    /*控制按钮区域*/

    .slider .ctrl{

    width:100%;

    height:13px;

    line-height: 13px;

    text-align: center;

    position: absolute;

    left: 0;

    bottom:-13px;

    position: relative;

    }

    .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;

    -webkit-transition: all .2s;

    }

    /*hover 到控制按钮的样式*/

    .slider .ctrl .ctrl-i:hover{

    background-color: #f0f0f0;

    }

    .slider .ctrl .ctrl-i:hover img{

    bottom: 13px;

    /*倒影*/

    -webkit-box-reflect: below 0px -webkit-gradient(

      linear,

      left top,

      left bottom,

      from( transparent ),

      color-stop(50% ,transparent),

      to( rgba(255,255,255,.3) )

    );

    opacity: 1;

    }

    /*active 当前展现状态*/

    .slider .ctrl .ctrl-i_active:hover,

    .slider .ctrl .ctrl-i_active{

    background-color: #000;

    }

    .slider .ctrl .ctrl-i_active:hover img{

    opacity: 0;

    }


    .slider .main .main-i{

    opacity: 0;

    position: absolute;

    right:50%;

    top: 0;

    -webkit-transition: all .5s;


    }

    .slider .main .main-i h2{

    margin-right: 45px; 

    }

    .slider .main .main-i h3{

    margin-right: -45px; 

    }

    .slider .main .main-i h2,

    .slider .main .main-i h3{

    -webkit-transition: all .8s 1s;

    }

    .slider .main .main-i_active{

    right:0;

    opacity: 1;

    }

    .slider .main .main-i_active h2,

    .slider .main .main-i_active h3{

    margin-right: 0;

    }

    </style>

    </head>

    <body>

    <div>

    <div id="template_main">

    <div class="main-i main-i_active" id="main_{{index}}">

    <div>

    <h2>{{h2}}</h2>

    <h3>{{h3}}</h3>

    </div>

    <img src="images/{{index}}.jpg" />

    </div>

    </div>

    <div id="template_ctrl">

    <a href="javascript:switchSlider({{index}});" id="ctrl_{{index}}">

    <img src="images/{{index}}.jpg">

    </a>

    </div>

    </div>


  • 十一不哭
    2015-12-09 23:07:31

    你应该打开Chrome到控制台(F12),看下错误信息提示。

    而且,这么一堆代码放上来,也不知道你想实现什么样的效果,引入的外部文件代码会不会影响,别人都很难知道