1

Ich möchte eine Bildergalerie erstellen. Jedes Bild würde eine Gruppe von Bildern darstellen. Ich erkannte, dass das eine gute Lösung wäre: Jedes Bild würde ein Modal auslösen, und innerhalb eines Modales würde ich ein Karussell platzieren, wo andere Bilder angezeigt würden. Aber ich stieß auf ein Problem, ich bin nicht in der Lage, Karussells richtig funktionieren zu lassen (sie sind in einer Reihe eingebettet). Bitte überprüfen Sie jsfiddle für weitere Informationen: https://jsfiddle.net/r0ukv00d/4/Wie werden mehr Karussells in einer Zeile angezeigt?

<div class="container"> 
    <div class="row"> 
     <div class="col-md-6 col-sm-6 col-xs-12"> <img src="https://upload.wikimedia.org/wikipedia/commons/8/8e/Eyjafjallaj%C3%B6kull.jpeg" height="42" width="42" data-toggle="modal" data-target="#myModal"><h5> 
     Click on the image and you will be able to see images from Iceland. 
     </h5> 
      <div id="myModal" class="modal fade"> 
       <div class="modal-dialog"> 
        <div class="modal-content"> 
         <div class="modal-header"> 
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
         </div> 
         <div class="modal-body"> 
          <!-- Modal content--> 
          <div id="carousel-1" class="carousel slide" data-ride="carousel"> 
           <!-- Indicators --> 
           <ol class="carousel-indicators"> 
            <li data-target="#carousel-1" data-slide-to="0" class="active"></li> 
            <li data-target="#carousel-1" data-slide-to="1"></li> 
            <li data-target="#carousel-1" data-slide-to="2"></li> 
           </ol> 
           <!-- Wrapper for slides --> 
           <div class="carousel-inner" role="listbox"> 
            <div class="item active"> <img src="https://upload.wikimedia.org/wikipedia/commons/8/8e/Eyjafjallaj%C3%B6kull.jpeg" alt="..."> 
             <div class="carousel-caption"> Iceland1 </div> 
            </div> 
            <div class="item"> <img src="http://www.icelandprocruises.co.uk/media/img/gallery/home/0002-gallery-iceland-waterfall-1.jpg" alt="..."> 
             <div class="carousel-caption">Iceland2 </div> 
            </div> 
            <div class="item"> <img src="http://querzy.com/wp-content/uploads/2016/06/iceland.jpg" alt="..."> 
             <div class="carousel-caption"> Iceland3 </div> 
            </div> 
           </div> 
           <!-- Controls --> 
           <a class="left carousel-control" href="#carousel-1" 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-1" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> 
          </div> 
         </div> 
        </div> 
        <!-- /.modal-content --> 
       </div> 
       <!-- /.modal-dialog --> 
      </div> 
      <!-- /.modal --> 
     </div> 
     <div class="col-md-6 col-sm-6 col-xs-12"> <img src="https://cache-graphicslib.viator.com/graphicslib/thumbs360x240/16674/SITours/private-sugar-loaf-and-christ-the-redeemer-tour-in-rio-de-janeiro-278478.jpg" height="42" width="42" data-toggle="modal" data-target="#myModal"> 
     <h5> 
     Click on the image and you will be able to see images from Brazil. 
     </h5> 
     <h3 style="color:red"> 
     Second carousel is not working! It shows pictures from Iceland instead of Brazil! 
     </h3> 
      <div id="myModal" class="modal fade"> 
       <div class="modal-dialog"> 
        <div class="modal-content"> 
         <div class="modal-header"> 
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
         </div> 
         <div class="modal-body"> 
          <!-- Modal content--> 
          <div id="carousel-2" class="carousel slide" data-ride="carousel"> 
           <!-- Indicators --> 
           <ol class="carousel-indicators"> 
            <li data-target="#carousel-2" data-slide-to="0" class="active"></li> 
            <li data-target="#carousel-2" data-slide-to="1"></li> 
            <li data-target="#carousel-2" data-slide-to="2"></li> 
           </ol> 
           <!-- Wrapper for slides --> 
           <div class="carousel-inner" role="listbox"> 
            <div class="item active"> <img src="https://cache-graphicslib.viator.com/graphicslib/thumbs360x240/16674/SITours/private-sugar-loaf-and-christ-the-redeemer-tour-in-rio-de-janeiro-278478.jpg" alt="..."> 
             <div class="carousel-caption"> Brazil1 </div> 
            </div> 
            <div class="item"> <img src="http://www.sportiputovanja.hr/wp-content/uploads/2016/12/RIO-BEACH.jpg" alt="..."> 
             <div class="carousel-caption"> Brazil2 </div> 
            </div> 
            <div class="item"> <img src="http://riotimesonline.com/wp-content/uploads/2014/01/Fernando-Maia-Riotur.png" alt="..."> 
             <div class="carousel-caption"> Brazil3 </div> 
            </div> 
           </div> 
           <!-- Controls --> 
           <a class="left carousel-control" href="#carousel-2" 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-2" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> 
          </div> 
         </div> 
        </div> 
        <!-- /.modal-content --> 
       </div> 
       <!-- /.modal-dialog --> 
      </div> 
      <!-- /.modal --> 
     </div> 
    </div> 
</div> 

Antwort

1

unten gegeben moderne Art und Weise tun. Sie können sich auf this link beziehen, um weitere Details zur dynamischen Erstellung von Modal zu erhalten. Sonst dann, dass JavaScript-Code entweder in message oder msg von mir getan wird, um alles dynamisch zu generieren, so dass, wenn Sie alles in der Datenbank gespeichert haben Sie nur eine ähnliche json dort erstellen und analysieren Sie es hier und Sie sind fertig. Hinzufügen und Löschen von Bildern, Land wird auch einfach sein wird auch einfach sein.

var countryjson={ 
 
\t \t \t 
 
\t \t \t list : [ 
 
\t \t \t \t { 
 
\t \t \t \t \t name : 'Iceland', 
 
\t \t \t \t \t img_path : 'https://upload.wikimedia.org/wikipedia/commons/8/8e/Eyjafjallaj%C3%B6kull.jpeg', 
 
\t \t \t \t \t otherimages : [ 
 
\t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t image_path : 'https://upload.wikimedia.org/wikipedia/commons/8/8e/Eyjafjallaj%C3%B6kull.jpeg', 
 
\t \t \t \t \t \t \t image_title: 'Title goes here.', 
 
\t \t \t \t \t \t \t image_caption : 'This is 1st image of iceland and this is its caption', 
 
\t \t \t \t \t \t }, 
 
\t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t image_path : 'http://www.icelandprocruises.co.uk/media/img/gallery/home/0002-gallery-iceland-waterfall-1.jpg', 
 
\t \t \t \t \t \t \t image_title: 'Title goes here.', 
 
\t \t \t \t \t \t \t image_caption : 'This is 2nd image of iceland and this is its caption', 
 
\t \t \t \t \t \t }, 
 
\t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t image_path : 'http://querzy.com/wp-content/uploads/2016/06/iceland.jpg', 
 
\t \t \t \t \t \t \t image_title: 'Title goes here.', 
 
\t \t \t \t \t \t \t image_caption : 'This is 3rd image of iceland and this is its caption', 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t ] 
 
\t \t \t \t }, 
 
\t \t \t \t { 
 
\t \t \t \t \t name : 'Brazil', 
 
\t \t \t \t \t img_path : 'https://cache-graphicslib.viator.com/graphicslib/thumbs360x240/16674/SITours/private-sugar-loaf-and-christ-the-redeemer-tour-in-rio-de-janeiro-278478.jpg', 
 
\t \t \t \t \t otherimages : [ 
 
\t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t image_path : 'https://cache-graphicslib.viator.com/graphicslib/thumbs360x240/16674/SITours/private-sugar-loaf-and-christ-the-redeemer-tour-in-rio-de-janeiro-278478.jpg', 
 
\t \t \t \t \t \t \t image_title: 'Title goes here.', 
 
\t \t \t \t \t \t \t image_caption : 'This is 1st image of iceland and this is its caption', 
 
\t \t \t \t \t \t }, 
 
\t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t image_path : 'http://www.sportiputovanja.hr/wp-content/uploads/2016/12/RIO-BEACH.jpg', 
 
\t \t \t \t \t \t \t image_title: 'Title goes here.', 
 
\t \t \t \t \t \t \t image_caption : 'This is 2nd image of iceland and this is its caption', 
 
\t \t \t \t \t \t }, 
 
\t \t \t \t \t \t { 
 
\t \t \t \t \t \t \t image_path : 'http://riotimesonline.com/wp-content/uploads/2014/01/Fernando-Maia-Riotur.png', 
 
\t \t \t \t \t \t \t image_title: 'Title goes here.', 
 
\t \t \t \t \t \t \t image_caption : 'This is 3rd image of iceland and this is its caption', 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t ] 
 
\t \t \t \t } 
 
\t \t \t ] 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t 
 
\t \t } 
 
\t \t 
 
\t \t 
 
\t \t function loadCountries() 
 
\t \t { 
 
\t \t 
 
\t \t \t for(i=0;i<countryjson.list.length;i++) 
 
\t \t \t { 
 
\t \t \t \t var countrybean=countryjson.list[i]; 
 
\t \t \t \t 
 
\t \t \t \t var msg=''; 
 
\t \t \t \t msg+='<div class="col-sm-3">'; 
 
\t \t \t \t msg+=' \t <div class="panel panel-default" onclick="showModal('+i+')">'; 
 
\t \t \t \t msg+=' \t \t <div class="panel-body">'; 
 
\t \t \t \t msg+=' \t \t \t <img src="'+countrybean.img_path+'" class="adjust-img" alt="'+countrybean.name+' Image">'; 
 
\t \t \t \t msg+=' \t \t </div>'; 
 
\t \t \t \t msg+=' \t \t <div class="panel-footer" align="center">'+countrybean.name+'</div>'; 
 
\t \t \t \t msg+=' \t </div>'; 
 
\t \t \t \t msg+='</div>'; 
 
\t \t \t \t 
 
\t \t \t \t $('#country_list').append(msg); 
 
\t \t \t \t 
 
\t \t \t } 
 
\t \t } 
 
\t \t 
 
\t \t 
 
\t \t function showModal(country_id){ 
 
\t \t 
 
\t \t \t BootstrapDialog.show({ 
 
\t \t \t \t title: 'Other images of '+countryjson.list[country_id].name, 
 
\t \t \t \t message: function(dialog) { 
 
\t \t \t \t \t 
 
\t \t \t \t \t var message =''; 
 
\t \t \t \t \t var ol_list=''; 
 
\t \t \t \t \t var image_list=''; 
 
\t \t \t \t \t var corosol_id='myCarousel_'+country_id; 
 
\t \t \t \t \t 
 
\t \t \t \t \t 
 
\t \t \t \t \t var countrybean=countryjson.list[country_id]; 
 
\t \t \t \t \t 
 
\t \t \t \t \t for(i=0;i<countrybean.otherimages.length;i++) 
 
\t \t \t \t \t { 
 
\t \t \t \t \t \t var imagebean=countrybean.otherimages[i]; 
 
\t \t \t \t \t \t ol_list+='<li data-target="#'+corosol_id+'" data-slide-to="'+i+'" '+ (i==0?'class="active"':'') +'></li>'; 
 
\t \t \t \t \t \t image_list+='<div class="item '+ (i==0?'active':'') +'">'; 
 
\t \t \t \t \t \t image_list+=' \t <img src="'+imagebean.image_path+'" alt="'+imagebean.image_title+'" >'; 
 
\t \t \t \t \t \t image_list+=' \t <div class="carousel-caption">'; 
 
\t \t \t \t \t \t image_list+=' \t \t <h3>'+imagebean.image_title+'</h3>'; 
 
\t \t \t \t \t \t image_list+=' \t \t <p>'+imagebean.image_caption+'</p>'; 
 
\t \t \t \t \t \t image_list+=' \t </div>'; 
 
\t \t \t \t \t \t image_list+='</div>'; 
 
\t \t \t \t \t } 
 
\t \t \t \t \t 
 
\t \t \t \t \t message+='<div id="'+corosol_id+'" class="carousel slide" data-ride="carousel">'; 
 
\t \t \t \t \t message+=' \t <!-- Indicators -->'; 
 
\t \t \t \t \t message+=' \t <ol class="carousel-indicators">'+ol_list+'</ol>'; 
 
\t \t \t \t \t 
 
\t \t \t \t \t message+=' \t <!-- Wrapper for slides -->'; 
 
\t \t \t \t \t message+=' \t <div class="carousel-inner" role="listbox">'+image_list+'</div>'; 
 
\t \t \t \t \t 
 
\t \t \t \t \t message+=' \t <!-- Left and right controls -->'; 
 
\t \t \t \t \t message+=' \t <a class="left carousel-control" href="#'+corosol_id+'" role="button" data-slide="prev">'; 
 
\t \t \t \t \t message+=' \t \t <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>'; 
 
\t \t \t \t \t message+=' \t \t <span class="sr-only">Previous</span>'; 
 
\t \t \t \t \t message+=' \t </a>'; 
 
\t \t \t \t \t message+=' \t <a class="right carousel-control" href="#'+corosol_id+'" role="button" data-slide="next">'; 
 
\t \t \t \t \t message+=' \t \t <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>'; 
 
\t \t \t \t \t message+=' \t \t <span class="sr-only">Next</span>'; 
 
\t \t \t \t \t message+=' \t </a>'; 
 
\t \t \t \t \t message+='</div>'; 
 
\t \t \t \t \t 
 
\t \t \t \t \t return message; 
 
\t \t \t \t } 
 
\t \t \t }); 
 
\t \t 
 
\t \t } 
 
\t \t 
 
\t \t 
 
\t \t $(document).ready(function(){ 
 
\t \t \t loadCountries(); 
 
\t \t });
.adjust-img 
 
\t \t { 
 
\t \t \t width : 100%; 
 
\t \t \t height : 300px; 
 
\t \t }
<!-- Latest compiled and minified CSS --> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css"> 
 

 
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> 
 

 

 
<!-- jQuery library --> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 

 
<!-- Latest compiled JavaScript --> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 

 
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script> 
 

 

 
<div class="container-fluid"> 
 
\t \t <div clas="rows" id="country_list"> 
 
\t \t </div> 
 
\t </div>

+0

Das ist sehr ordentlich! Vielen Dank für die detaillierte Wiedergabe, da Datenbankproblem sowieso meine nächste Frage sein würde. Ich werde versuchen, es so umzusetzen! –

2

Sie sind das gleiche Modell ID für beide Bilder aufrufen. Bitte sehen https://jsfiddle.net/r0ukv00d/5/ für aktualisierte Code

<div class="container"> 
    <div class="row"> 
     <div class="col-md-6 col-sm-6 col-xs-12"> <img src="https://upload.wikimedia.org/wikipedia/commons/8/8e/Eyjafjallaj%C3%B6kull.jpeg" height="42" width="42" data-toggle="modal" data-target="#myModal"><h5> 
     Click on the image and you will be able to see images from Iceland. 
     </h5> 
      <div id="myModal" class="modal fade"> 
       <div class="modal-dialog"> 
        <div class="modal-content"> 
         <div class="modal-header"> 
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
         </div> 
         <div class="modal-body"> 
          <!-- Modal content--> 
          <div id="carousel-1" class="carousel slide" data-ride="carousel"> 
           <!-- Indicators --> 
           <ol class="carousel-indicators"> 
            <li data-target="#carousel-1" data-slide-to="0" class="active"></li> 
            <li data-target="#carousel-1" data-slide-to="1"></li> 
            <li data-target="#carousel-1" data-slide-to="2"></li> 
           </ol> 
           <!-- Wrapper for slides --> 
           <div class="carousel-inner" role="listbox"> 
            <div class="item active"> <img src="https://upload.wikimedia.org/wikipedia/commons/8/8e/Eyjafjallaj%C3%B6kull.jpeg" alt="..."> 
             <div class="carousel-caption"> Iceland1 </div> 
            </div> 
            <div class="item"> <img src="http://www.icelandprocruises.co.uk/media/img/gallery/home/0002-gallery-iceland-waterfall-1.jpg" alt="..."> 
             <div class="carousel-caption">Iceland2 </div> 
            </div> 
            <div class="item"> <img src="http://querzy.com/wp-content/uploads/2016/06/iceland.jpg" alt="..."> 
             <div class="carousel-caption"> Iceland3 </div> 
            </div> 
           </div> 
           <!-- Controls --> 
           <a class="left carousel-control" href="#carousel-1" 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-1" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> 
          </div> 
         </div> 
        </div> 
        <!-- /.modal-content --> 
       </div> 
       <!-- /.modal-dialog --> 
      </div> 
      <!-- /.modal --> 
     </div> 
     <div class="col-md-6 col-sm-6 col-xs-12"> <img src="https://cache-graphicslib.viator.com/graphicslib/thumbs360x240/16674/SITours/private-sugar-loaf-and-christ-the-redeemer-tour-in-rio-de-janeiro-278478.jpg" height="42" width="42" data-toggle="modal" data-target="#myModal1"> 
     <h5> 
     Click on the image and you will be able to see images from Brazil. 
     </h5> 
     <h3 style="color:red"> 
     Second carousel is not working! It shows pictures from Iceland instead of Brazil! 
     </h3> 
      <div id="myModal1" class="modal fade"> 
       <div class="modal-dialog"> 
        <div class="modal-content"> 
         <div class="modal-header"> 
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
         </div> 
         <div class="modal-body"> 
          <!-- Modal content--> 
          <div id="carousel-2" class="carousel slide" data-ride="carousel"> 
           <!-- Indicators --> 
           <ol class="carousel-indicators"> 
            <li data-target="#carousel-2" data-slide-to="0" class="active"></li> 
            <li data-target="#carousel-2" data-slide-to="1"></li> 
            <li data-target="#carousel-2" data-slide-to="2"></li> 
           </ol> 
           <!-- Wrapper for slides --> 
           <div class="carousel-inner" role="listbox"> 
            <div class="item active"> <img src="https://cache-graphicslib.viator.com/graphicslib/thumbs360x240/16674/SITours/private-sugar-loaf-and-christ-the-redeemer-tour-in-rio-de-janeiro-278478.jpg" alt="..."> 
             <div class="carousel-caption"> Brazil1 </div> 
            </div> 
            <div class="item"> <img src="http://www.sportiputovanja.hr/wp-content/uploads/2016/12/RIO-BEACH.jpg" alt="..."> 
             <div class="carousel-caption"> Brazil2 </div> 
            </div> 
            <div class="item"> <img src="http://riotimesonline.com/wp-content/uploads/2014/01/Fernando-Maia-Riotur.png" alt="..."> 
             <div class="carousel-caption"> Brazil3 </div> 
            </div> 
           </div> 
           <!-- Controls --> 
           <a class="left carousel-control" href="#carousel-2" 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-2" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> 
          </div> 
         </div> 
        </div> 
        <!-- /.modal-content --> 
       </div> 
       <!-- /.modal-dialog --> 
      </div> 
      <!-- /.modal --> 
     </div> 
    </div> 
</div> 
Verwandte Themen