2015-05-12 27 views
7

我嘗試使用引導程序製作幻燈片放映,以便滑過圖像。但圖像沒有顯示。只顯示下一個和上一個按鈕。我使用asp.net mvc5帶引導程序的幻燈片放映

這是jQuery的:

$(document).ready(function() { 
      $myModal = $('#myModal'); 

      $('.row img.img-responsive').on('click', function() { // <-- notice the selector change 
       var $this = $(this), 
        src = $this.attr('src'), 
        html = '<img src="' + src + '" class="img-responsive" />'; 

       //Start 
       var index = $(this).parent('.row img.img-responsive').index(); 
       var html = ''; 
       html += html; 
       html += '<div style="height:25px;clear:both;display:block;">'; 
       html += '<a class="controls next" href="' + (index + 2) + '">next &raquo;</a>'; 
       html += '<a class="controls previous" href="' + (index) + '">&laquo; prev</a>'; 
       html += '</div>'; 
       //End 


       updateModalBody($myModal, html); 
       $myModal.modal(); 
      }); 

      $myModal.on('hidden.bs.modal', function() { 

       updateModalBody($myModal, ''); 
      }); 

      function updateModalBody($modal, html) { 
       $modal.find('.modal-body').html(html); 
       $modal.modal('hide'); 
      } 

     }) 

     $(document).on('click', 'a.controls', function() { 
      //this is where we add our logic 
      var index = $(this).attr('href'); 
      var src = $('ul.row li:nth-child(' + index + ') img').attr('src'); 

      $('.modal-body img').attr('src', src); 

      var newPrevIndex = parseInt(index) - 1; 
      var newNextIndex = parseInt(newPrevIndex) + 2; 

      if ($(this).hasClass('previous')) { 
       $(this).attr('href', newPrevIndex); 
       $('a.next').attr('href', newNextIndex); 
      } else { 
       $(this).attr('href', newNextIndex); 
       $('a.previous').attr('href', newPrevIndex); 
      } 

      var total = $('ul.row li').length + 1; 
      //hide next button 
      if (total === newNextIndex) { 
       $('a.next').hide(); 
      } else { 
       $('a.next').show() 
      } 
      //hide previous button 
      if (newPrevIndex === 0) { 
       $('a.previous').hide(); 
      } else { 
       $('a.previous').show() 
      } 


      return false; 
     }); 

,這的CSS/HTML:

<div id="tabs-2"> 
    <div class="row"> 
     @foreach (var item in Model.LolaBikePhotos) 
     { 

      @model ContosoUniversity.Models.UserProfile 
      @*<div class="col-lg-3 col-md-4 col-xs-6 thumb">*@ 
      <div class="col-lg-2 col-md-2 col-sm-3 col-xs-4 img-thumbnail"> 
       @*<a class="thumbnail">*@ 
       <img class="img-responsive" src="/Images/profile/@item.ImagePath" alt="" /> 
      </div> 

      <div class="modal hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 

       <div class="modal-dialog"> 
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
        <div class="modal-content"> 
         <div class="modal-body"></div> 
        </div> 
        <!-- /.modal-content --> 
       </div> 
       <!-- /.modal-dialog --> 

      </div> 
    <!-- /.modal --> 
     } 
    </div> 
</div> 

謝謝

我必須編輯這個:

html += html; 

但我不知道如何改變這一點。

這是您將如何看待它:enter image description here

+2

您的代碼有問題,我不是ASP開發人員,但請查看Bootstrap Carousel文檔並按照其步驟創建一個工作示例:http://getbootstrap.com/javascript/ #carousel – odedta

+1

如果您仍然遇到問題,請提供您*生成的* HTML - 解析asp.net代碼後得到的標記。 –

+0

你試過這個:src =「@ Url.Content(string.Format(」〜/ Images/Profile/{0}「,item.ImagePath))」? –

回答

1

爲了使這個簡單的

如果從

複製第一個看HTML和添加一些Razor,這應該產生一個旋轉木馬給你。

@model List<YOUR_MODEL_NAME> 
    @{var j = 0;} 
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> 
    <ol class="carousel-indicators"> 
    @for (var i = 0; i < Model.Count(); i++) 
    { 
     <li data-target="#carousel-example-generic" data-slide-to="@i"></li> 
     } 
    </ol> 

<div class="carousel-inner" role="listbox"> 

@foreach (var item in Model) 
    { 
    <div class="item @(j == 0 ? "active" : "")"> 
     <img src="@(string.Format("/Images/profile/{0}", item.ImagePath))" /> 
    </div> 
    @(j = 1) 
    } 

</div> 

<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev"> 
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> 
<span class="sr-only">Previous</span> 
</a> 
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next"> 
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> 
<span class="sr-only">Next</span> 
</a> 
</div> 

編輯

下面是一個縮略圖,當點擊縮略圖會打開一個模式,對話框,在其中一個旋轉木馬爲例。

我已經創建了場景的的jsfiddle: http://jsfiddle.net/u7hxy3zc/1/

你應該使用下面的代碼作爲一個例子,在您的視圖中使用,重命名所有的模型和參數。

注:下面的代碼沒有被編譯有語法錯誤

// DISPLAY THUMBNAILS 
<div class="col-md-2"> 
@for(int i =0; i < Model.Count(); i++) 
    { 
    <img src="@Model[i].ImageUrl" data-id="@i" data-action="image" /> 
    }   
</div> 



.......... REMOVE HTML TO MAKE EXAMPLE CLEAR 
... MODAL DIALOG START................. 


<div class="modal-body"> 

...........CAROUSEL START............ 


<div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> 
<ol class="carousel-indicators"> 
@for(var i = 0 ; i< Model.Count(); i++) 
{ 
<li data-target="#carousel-example-generic" data-slide-to="@i"></li> 
} 
</ol> 

<div class="carousel-inner" role="listbox"> 

@for(var i = 0; i < Model.Count(); i++) 
{ 
<div class="item" data-image-id="@i"> 

    // DISPLAY i for Testing  
    @i  

    <img src="@Model[i].ImageUrl" /> 
} 
</div> 

........... REMOVED REST OF CAROUSEL......(NEXT/PREV BUTTONS)......... 

.............CAROUSEL END................... 

末再加入這個jQuery來觸發單擊事件:

$('body').on('click', '*[data-action="image"]', function (e) { 
    e.stopPropagation(); 
    var img = $(this); 
    var currentId = img.data('id'); 
    $('#myModal').modal(); 
    var currentSlide = "*[data-slide-to='" + currentId + "']"; 
    var currentImagSlide = "*[data-image-id='" + currentId + "']"; 

    console.log(currentSlide); 

    $("*[data-slide-to]").removeClass("active"); 
    $(".item").removeClass("active"); 

    var item = $('.carousel-indicators').find(currentSlide); 
    var imgItem = $('.carousel-inner').find(currentImagSlide); 
    console.log(item); 
    item.addClass("active"); 
    imgItem.addClass("active"); 
}); 
+0

考慮參數化這些'id'屬性,這樣可以重複使用多次在任何給定的頁面內。 –

+0

@DaWood,謝謝你的answare。但它必須是一個照片庫,但你可以在下一個和上一個之間的大圖像之間滑動。因此無論如何都必須顯示縮略圖,就像我現在已經顯示的那樣,但是如果您單擊圖像,則可以在圖像之間滑動,而不是必須單擊每個縮略圖才能看到更大的格式的。對不起,我的誤解。像這樣:https://demos.devexpress.com/MVCxImageAndDataNavigationDemos/ImageSlider/PhotoGallery – InfinityGoesAround

+0

檢查我的編輯並按照這個:http://jsfiddle.net/u7hxy3zc/1/ –

0

<div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> 
 
    <!-- Indicators --> 
 
    <ol class="carousel-indicators"> 
 
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> 
 
    <li data-target="#carousel-example-generic" data-slide-to="1"></li> 
 
    <li data-target="#carousel-example-generic" data-slide-to="2"></li> 
 
    </ol> 
 

 
    <!-- Wrapper for slides --> 
 
    <div class="carousel-inner"> 
 
    <div class="item active"> 
 
     <img src="..." alt="..."> 
 
     <div class="carousel-caption"> 
 
     ... 
 
     </div> 
 
    </div> 
 
    ... 
 
    </div> 
 

 
    <!-- Controls --> 
 
    <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"> 
 
    <span class="glyphicon glyphicon-chevron-left"></span> 
 
    </a> 
 
    <a class="right carousel-control" href="#carousel-example-generic" data-slide="next"> 
 
    <span class="glyphicon glyphicon-chevron-right"></span> 
 
    </a> 
 
</div>

肯定會查看引導程序輪播