简体   繁体   中英

how to put a link on the photo after the photo opens?

I hope you don't mind helping me with this code. I'm using bootstrap and I have this html:

   <div class="gallery">
       <div class="container">
        <div class="row">
            <div class="col-xs-3 gallery-item">
                <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery">
                    <img src="images/port1.jpg" class="img-responsive img-gallery" alt="Projekti i pare">
                </a>
            </div> <!-- /.col -->

            <div class="col-xs-3 gallery-item">
                <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery">
                    <img src="images/port2.jpg" class="img-responsive img-gallery" alt="Projekti i dyte">
                </a>
            </div> <!-- /.col -->

            <div class="col-xs-3 gallery-item">
                <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery">
                    <img src="images/port3.jpg" class="img-responsive img-gallery" alt="Projekti i trete">
                </a>
            </div> <!-- /.col -->

            <div class="col-xs-3 gallery-item">
                <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery">
                    <img src="images/port4.jpg" class="img-responsive img-gallery" alt="Projekti i katert">
                </a>
            </div> <!-- /.col -->
            <div class="col-xs-3 gallery-item">
                <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery">
                    <img src="images/port4.jpg" class="img-responsive img-gallery" alt="Projekti i peste">
                </a>
            </div> <!-- /.col -->
            <div class="col-xs-3 gallery-item">
                <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery">
                    <img src="images/port4.jpg" class="img-responsive img-gallery" alt="Projekti i gjashte">
                </a>
            </div> <!-- /.col -->
            <div class="col-xs-3 gallery-item">
                <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery">
                    <img src="images/port4.jpg" class="img-responsive img-gallery" alt="Projekti i shtate">
                </a>
                   </div> <!-- /.col -->
              </div> <!--/.row  -->
            </div> <!-- /.container -->
        </div> <!-- /.gallery -->

       <div class="modal fade" id="modalGallery" tabindex="-1" role="dialog" aria-labelledby="modalGalleryLabel" aria-hidden="true">
       <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="modalGalleryLabel">Gallery</h4>
            </div> <!-- /.modal-header -->

            <div class="modal-body">
                <div id="carouselGallery" class="carousel slide" data-ride="carousel" data-interval="false">
                    <div class="carousel-inner">
                    </div> <!-- /.carousel-inner -->
                </div> <!-- /.carousel -->
            </div> <!-- /.modal-body -->

            <div class="modal-footer">
                <ul class="pagination">
                </ul>
            </div> <!-- /.modal-footer -->
        </div> <!-- /.modal-content -->
    </div> <!-- /.modal-dialog -->
</div> <!-- /.modal -->

This is my css:

   .gallery{
margin-top: 100px;
     }
  .gallery-item{
   margin-bottom: 30px;
     }
    .modal-footer{
     text-align: center;
   }
    .pagination{
    margin: 0;
    }

And my Javacsript

       $(document).ready(function(){
     $('.link-gallery').click(function(){
    var galleryId = $(this).attr('data-target');
    var currentLinkIndex = $(this).index('a[data-target="'+ galleryId +'"]');

    createGallery(galleryId, currentLinkIndex);
    createPagination(galleryId, currentLinkIndex);

    $(galleryId).on('hidden.bs.modal', function (){
        destroyGallry(galleryId);
        destroyPagination(galleryId);
    });

    $(galleryId +' .carousel').on('slid.bs.carousel', function (){
        var currentSlide = $(galleryId +' .carousel .item.active');
        var currentSlideIndex = currentSlide.index(galleryId +' .carousel .item');

        setTitle(galleryId, currentSlideIndex);
        setPagination(++currentSlideIndex, true);
    })
});

function createGallery(galleryId, currentSlideIndex){
    var galleryBox = $(galleryId + ' .carousel-inner');

    $('a[data-target="'+ galleryId +'"]').each(function(){
        var img = $(this).html();
        var galleryItem = $('<div class="item">'+ img +'</div>');

        galleryItem.appendTo(galleryBox);
    });

    galleryBox.children('.item').eq(currentSlideIndex).addClass('active');
    setTitle(galleryId, currentSlideIndex);
}

function destroyGallry(galleryId){
    $(galleryId + ' .carousel-inner').html("");
}

function createPagination(galleryId, currentSlideIndex){
    var pagination = $(galleryId + ' .pagination');
    var carouselId = $(galleryId).find('.carousel').attr('id');
    var prevLink = $('<li><a href="#'+ carouselId +'" data-slide="prev">«</a></li>');
    var nextLink = $('<li><a href="#'+ carouselId +'" data-slide="next">»</a></li>');

    prevLink.appendTo(pagination);
    nextLink.appendTo(pagination);

    $('a[data-target="'+ galleryId +'"]').each(function(){
        var linkIndex = $(this).index('a[data-target="'+ galleryId +'"]');
        var paginationLink = $('<li><a data-target="#carouselGallery" data-slide-to="'+ linkIndex +'">'+ (linkIndex+1) +'</a></li>');

        paginationLink.insertBefore('.pagination li:last-child');
    });

    setPagination(++currentSlideIndex);
}

function setPagination(currentSlideIndex, reset = false){
    if (reset){
        $('.pagination li').removeClass('active');
    }

    $('.pagination li').eq(currentSlideIndex).addClass('active');
}

function destroyPagination(galleryId){
    $(galleryId + ' .pagination').html("");
}

function setTitle(galleryId, currentSlideIndex){
    var imgAlt = $(galleryId + ' .item').eq(currentSlideIndex).find('img').attr('alt');

    $('.modal-title').text(imgAlt);
}
      });

I want to add an href link in the photo, when the photo gets bigger.What should I do?

Just add <a href=""></a> , while you are assigning the galleryItem .

Like:

var galleryItem = $('<div class="item"><a href="your-link">'+ img +'</a></div>');

To add different-different links to each <a> use an extra attribute 'data-link' in your link,

<a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery" data-link="your-link-1">

And in JS, create a variable and get that link via .attr() function & append this into your link like,

// Get the link from data attribute 'data-link'
var imgLink = $(this).attr('data-link');

// Append the link into the 'galleryItem'
var galleryItem = $('<div class="item"><a href="' + imgLink + '">'+ img +'</a></div>');

Have a look at the snippet below:

 $(document).ready(function(){ $('.link-gallery').click(function(){ var galleryId = $(this).attr('data-target'); var currentLinkIndex = $(this).index('a[data-target="'+ galleryId +'"]'); var imgLink = $(this).attr('data-link'); createGallery(galleryId, currentLinkIndex, imgLink); createPagination(galleryId, currentLinkIndex); $(galleryId).on('hidden.bs.modal', function (){ destroyGallry(galleryId); destroyPagination(galleryId); }); $(galleryId +' .carousel').on('slid.bs.carousel', function (){ var currentSlide = $(galleryId +' .carousel .item.active'); var currentSlideIndex = currentSlide.index(galleryId +' .carousel .item'); setTitle(galleryId, currentSlideIndex); setPagination(++currentSlideIndex, true); }) }); function createGallery(galleryId, currentSlideIndex, imgLink){ var galleryBox = $(galleryId + ' .carousel-inner'); $('a[data-target="'+ galleryId +'"]').each(function(){ var img = $(this).html(); var galleryItem = $('<div class="item"><a href="' + imgLink + '">'+ img +'</a></div>'); galleryItem.appendTo(galleryBox); }); galleryBox.children('.item').eq(currentSlideIndex).addClass('active'); setTitle(galleryId, currentSlideIndex); } function destroyGallry(galleryId){ $(galleryId + ' .carousel-inner').html(""); } function createPagination(galleryId, currentSlideIndex){ var pagination = $(galleryId + ' .pagination'); var carouselId = $(galleryId).find('.carousel').attr('id'); var prevLink = $('<li><a href="#'+ carouselId +'" data-slide="prev">«</a></li>'); var nextLink = $('<li><a href="#'+ carouselId +'" data-slide="next">»</a></li>'); prevLink.appendTo(pagination); nextLink.appendTo(pagination); $('a[data-target="'+ galleryId +'"]').each(function(){ var linkIndex = $(this).index('a[data-target="'+ galleryId +'"]'); var paginationLink = $('<li><a data-target="#carouselGallery" data-slide-to="'+ linkIndex +'">'+ (linkIndex+1) +'</a></li>'); paginationLink.insertBefore('.pagination li:last-child'); }); setPagination(++currentSlideIndex); } function setPagination(currentSlideIndex, reset = false){ if (reset){ $('.pagination li').removeClass('active'); } $('.pagination li').eq(currentSlideIndex).addClass('active'); } function destroyPagination(galleryId){ $(galleryId + ' .pagination').html(""); } function setTitle(galleryId, currentSlideIndex){ var imgAlt = $(galleryId + ' .item').eq(currentSlideIndex).find('img').attr('alt'); $('.modal-title').text(imgAlt); } }); 
 .gallery{ margin-top: 100px; } .gallery-item{ margin-bottom: 30px; } .modal-footer{ text-align: center; } .pagination{ margin: 0; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="gallery"> <div class="container"> <div class="row"> <div class="col-xs-3 gallery-item"> <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery" data-link="your-link-1"> <img src="http://placehold.it/200x200" class="img-responsive img-gallery" alt="Projekti i pare"> </a> </div> <!-- /.col --> <div class="col-xs-3 gallery-item"> <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery" data-link="your-link-2"> <img src="http://placehold.it/200x200" class="img-responsive img-gallery" alt="Projekti i dyte"> </a> </div> <!-- /.col --> <div class="col-xs-3 gallery-item"> <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery" data-link="your-link-3"> <img src="http://placehold.it/200x200" class="img-responsive img-gallery" alt="Projekti i trete"> </a> </div> <!-- /.col --> <div class="col-xs-3 gallery-item"> <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery" data-link="your-link-4"> <img src="http://placehold.it/200x200" class="img-responsive img-gallery" alt="Projekti i katert"> </a> </div> <!-- /.col --> <div class="col-xs-3 gallery-item"> <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery" data-link="your-link-5"> <img src="http://placehold.it/200x200" class="img-responsive img-gallery" alt="Projekti i peste"> </a> </div> <!-- /.col --> <div class="col-xs-3 gallery-item"> <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery" data-link="your-link-6"> <img src="http://placehold.it/200x200" class="img-responsive img-gallery" alt="Projekti i gjashte"> </a> </div> <!-- /.col --> <div class="col-xs-3 gallery-item"> <a href="#galleryImg1" class="link-gallery" data-toggle="modal" data-target="#modalGallery" data-link="your-link-7"> <img src="http://placehold.it/200x200" class="img-responsive img-gallery" alt="Projekti i shtate"> </a> </div> <!-- /.col --> </div> <!--/.row --> </div> <!-- /.container --> </div> <!-- /.gallery --> <div class="modal fade" id="modalGallery" tabindex="-1" role="dialog" aria-labelledby="modalGalleryLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title" id="modalGalleryLabel">Gallery</h4> </div> <!-- /.modal-header --> <div class="modal-body"> <div id="carouselGallery" class="carousel slide" data-ride="carousel" data-interval="false"> <div class="carousel-inner"> </div> <!-- /.carousel-inner --> </div> <!-- /.carousel --> </div> <!-- /.modal-body --> <div class="modal-footer"> <ul class="pagination"> </ul> </div> <!-- /.modal-footer --> </div> <!-- /.modal-content --> </div> <!-- /.modal-dialog --> </div> <!-- /.modal --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> 

Hope this helps!

Please replace one line in javascript

 var galleryItem = $('<div class="item"><a href="http://google.com">'+ img +'</a></div>'); 

hope you will get.

and yes if you want to replace http://google.com to another one as you want ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM