【问题标题】:Carousel with Thumbnails, add class to next LI as carousel rotates带有缩略图的轮播,在轮播旋转时将类添加到下一个 LI
【发布时间】:2013-09-26 20:54:46
【问题描述】:

我有一个旋转木马,下面有小缩略图。我正在添加一个 .isActive 类来显示位于每个缩略图上的隐藏

。当轮播第一次滑动时,我可以从第一个缩略图
中删除 .isActive 类。但我无法将 .isActive 添加到下一个

如何将所选的缩略图与相应的幻灯片相匹配?

这是一个小提琴:http://jsfiddle.net/gybYP/

这是我的 HTML:

<div class="js-carousel">
  <div class="slidesContainer">
    <ul class="clearfix">
      <li class="slide green">One</li>
      <li class="slide blue">Two</li>
      <li class="slide red">Three</li>
    </ul>
  </div>
  <!-- /slidesContainer -->
  <div class="thumbnailContainer">
    <ul>
      <li class="thumb green">
        <div>
          <a href="#">
            <div class="smallSlide"></div>
            <div class="thumbOverlay isActive"></div>
          </a>
        </div>                                  
      </li>
      <li class="thumb blue">
        <div>
          <a href="#">
            <div class="smallSlide"></div>
            <div class="thumbOverlay"></div>
          </a>
        </div>
      </li>
      <li class="thumb red">
         <div>
          <a href="#">
            <div class="smallSlide"></div>
            <div class="thumbOverlay"></div>
          </a>
        </div>
      </li>
    </ul>
  </div>
  <!-- /thumbnailContainer -->
</div>
<!-- /js-carousel -->

这是我的 CSS:

.clearfix:before,
.clearfix:after {
  content: "";
  display: table;
} 
.clearfix:after {
  clear: both;
}
.clearfix {
  zoom: 1; /* For IE 6/7 (trigger hasLayout) */
}

.slidesContainer {
  width:200px;
  height: 200px;
  overflow: hidden;
  margin-bottom: 10px;
 }

 .slidesContainer ul {
   margin: 0;
   padding: 0;
   width: 600px; /* Slides width times total slides */
   position: relative;
   top: 0;
   left: 0;
   list-style:none;
 }

.slide {
  width: 200px;
  height: 200px;
  float: left;
}

.green {background-color: green;}
.blue {background-color: blue;}
.red {background-color: red;}

.thumbnailContainer ul {
  margin: 0;
  padding: 0;
  width: 600px; /* Slides width times total slides */
  position: relative;
  top: 0;
  left: 0;
  list-style:none;
}
.thumb {
  width: 50px;
  height: 50px;
  display: inline-block;
  position: relative;
}

.thumbOverlay {
  background-color: gray;
  width: 20px;
  height: 20px;
  position: absolute;
  top: 30%;
  left: 30%;
  display: none;
}

.thumbOverlay.isActive {
  display: block;
}

这是我的 JavaScript:

var slide_width = $('.slidesContainer li').outerWidth();

var left_value = slide_width * (-1);

$(document).ready(function() {

  var speed = 3000;
  var run = setInterval('rotate()', speed);

  $('.slide:first').before($('.slide:last'));

  //set the default item to the correct position 
  $('.slidesContainer ul').css({'left' : left_value});

  $('.slidesContainer').hover(

    function() {
      clearInterval(run);
    }, 
    function() {
      run = setInterval('rotate()', speed); 
    }
  ); 

});

function rotate() {
  //get the right position            
  var left_indent = parseInt($('.slidesContainer ul').css('left')) - slide_width;

  $('.slidesContainer ul').animate(
    {
      'left' : left_indent
    },
     200,
     function() {

       //Remove the class .isActive from the current active thumbnail
       $('.thumbnailContainer .thumbOverlay.isActive').removeClass('isActive');

       //move the first item and put it as last item
       $('.slidesContainer li:last').after($('.slidesContainer li:first'));                  

       //set the default item to correct position
       $('.slidesContainer ul').css({'left' : left_value});

     }
   );

}

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    // 大图

      <a href="<%# Eval("Link") %>"><img src="<%# Eval("Path") %>" alt="<%# "#htmlcaption" + (Container.ItemIndex + 1).ToString() %>"  /></a>
    

    //用于缩略图

    <li rel='<%#(Container.ItemIndex + 1).ToString() %>'> <img src="<%# Eval("Path") %>" width="50" height="50" /></li>
    

    aspx:

    <div id='wrapper'>
            <div id='header'></div>
            <div id='body'>
                <div id="bigPic">
    
    
                  <asp:Repeater ID="RepeaterBigBanner" runat="server">  
        <HeaderTemplate>  
    
        </HeaderTemplate>  
        <ItemTemplate>
             <a href="<%# Eval("Link") %>"><img src="<%# Eval("Path") %>" alt="<%# "#htmlcaption" + (Container.ItemIndex + 1).ToString() %>"  /></a>
        </ItemTemplate> 
    
        <SeparatorTemplate>  
    
        </SeparatorTemplate> 
    </asp:Repeater>
    
                    <%--<img src="imgs/1.jpg" alt=""  width="650" height="250"/>--%>
    
                </div>
    
                <div id="kucukList" runat="server" style="height:60px;text-align: center;">
                <ul id="thumbs" >
                    <%--<li class='active' rel='1'><img src="imgs/1_thumb.jpg" alt="" /></li>
                    <li rel='2'><img src="imgs/3_thumb.jpg" alt="" /></li>--%>
                     <asp:Repeater ID="RepeaterIconBanner" runat="server">  
        <HeaderTemplate>  
    
        </HeaderTemplate>  
        <ItemTemplate>
    
           <li rel='<%#(Container.ItemIndex + 1).ToString() %>'> <img src="<%# Eval("Path") %>" width="50" height="50" /></li>
        </ItemTemplate> 
    
        <SeparatorTemplate>  
    
        </SeparatorTemplate> 
    </asp:Repeater>
    
                </ul>
            </div>
            </div>
            <div class='clearfix'></div>
            <div id='push'></div>
        </div>
    

    带有 jquery-1.4.1.min.js 或更高版本的脚本端

    <script type="text/javascript">
            var currentImage;
            var currentIndex = -1;
            var interval;
    
            function showImage(index) {        
                    if (index < $('#bigPic img').length) {
                        var indexImage = $('#bigPic img')[index]
                        if (currentImage) {
                            if (currentImage != indexImage) {
                                $(currentImage).css('z-index', 2);
                                clearTimeout(myTimer);
                                $(currentImage).fadeOut(0, function () {
                                    myTimer = setTimeout("showNext()", 5000);
                                    $(this).css({ 'display': 'none', 'z-index': 1 })                                
                                });
                            }
                        }
                        $(indexImage).css({ 'display': 'block', 'opacity': 1 });
                        currentImage = indexImage;
                        currentIndex = index;
                        $('#thumbs li').removeClass('active');
                        $($('#thumbs li')[index]).addClass('active');
                    }           
                }                 
            function showNext() {
                var len = $('#bigPic img').length;
                var next = currentIndex < (len - 1) ? currentIndex + 1 : 0;
                showImage(next);
            }
    
    
            var myTimer;
    
            $(document).ready(function () {
    
    
                    myTimer = setTimeout("showNext()", 5000);
                    showNext(); //loads first image 
    
                //$('#thumbs li').bind('click', function (e) {
                //        var count = $(this).attr('rel');
                //        showImage(parseInt(count) - 1);
    
                //  });
                    $('#thumbs li').bind('mouseenter', function (e) {
    
                    var count = $(this).attr('rel');
                    showImage(parseInt(count) - 1);                             
                    document.getElementsById('thumbs')[0].style['transition-delay'] = '0.2s';
    
                    });          
            });
        </script>
    

    CSS 端

    #push {
        height: 14px; /* .push must be the same height as .footer */
        padding-top:0px;
    }
    #wrapper{
        width:675px;
    
        height: auto !important;
        height: 96%;
        text-align:left;
        /*margin: 0 auto -30px;*/
        /*padding:0 10px 0px 10px;*/
    }
    
    .clearfix{
        clear:both;
        float:none;
    }
    #bigPic{
        width:665px;
        height:300px;
    
        border:1px solid #CCC;
        background-color:#FFF;
        margin-bottom:0px;
    }
    #bigPic img{
        position:absolute;
        display:none;
    }
    
    #MainPic{
        width:665px;
        height:300px;
        float:right;
        border:1px solid #CCC;
        background-color:#FFF;
        margin-bottom:0px;
    }
    #MainPic img{
        position:absolute;
        display:none;
    }
    ul#thumbs li.active{
        background: transparent url(/img/newbannerimage2/icon-uparrowsmallwhite.png)top center no-repeat ;
      text-align:center;
        /*border:1px solid #000;*/
        /*padding:2px;*/
        padding-top: 8px;
    }
    ul#thumbs, ul#thumbs li{
            /*margin:0;
        padding:0;*/
        list-style:none;
        text-align:center;
    
    }
    
    ul#thumbs li{
        float:left;
    margin-right: 3.99px;
        margin-bottom:5px;
        /*border:1px solid #CCC*/;  
        /*padding:2px;*/
        padding-top: 8px;
        cursor:pointer;
    
    }
    ul#thumbs img{
        float:left;
        width:50px;
        height:50px;
        line-height:80px;
        overflow:hidden;
        position:relative;
        z-index:1;  
        border:1px solid #cecece;   
    
    }
    

    我是用 repater 创建的,但你可以创建任何你想要的东西

    【讨论】:

    • 我不确定我是否遵循 aspx。我对asp不熟悉。但我会玩这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 2019-11-22
    • 1970-01-01
    • 2016-08-10
    • 2012-07-13
    • 2018-04-11
    相关资源
    最近更新 更多