Search code examples
javascriptjqueryhtmlcsscycle

Cycle image caption doesn't work


I've cycle slideshow gallery and I put a caption on my images but there is something wrong why I don't understand..Only my attribute name showing on my caption I don't want to attribute name I want to get attribute value what's wrong with me ?

$(document).ready(function() {

  $('.mySlideShow').cycle({
    log: false,
    fx: 'fade',
    slides: ">a",
    caption: '.cycle-caption',
    captionTemplate: "{{data-caption}}",
    pauseOnHover: true,
  });
})
.slide-gallery {
  position: relative;
  width: 790px;
  overflow: hidden;
  height: 500px;
}

.slide-gallery img {
  max-width: 100%;
  height: 100%;
}

.cycle-caption {
  position: absolute;
  bottom: 0;
  left: 0;
  background: rgba(0, 0, 0, .8);
  padding: 10px;
  color: #FFF;
  z-index: 1000;
  width: 100%;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.cycle2/2.1.6/jquery.cycle2.min.js"></script>

<div class="slide-gallery">
  <div class="mySlideShow">

    <a>
      <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/dandelion-lightbox.jpg" data-caption="Lorem ipsum dolor sit amet..">
    </a>
    <a>
      <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/daffodil-flowers-lightbox.jpg" data-caption="This is my caption....">
    </a>
    <a>
      <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/tulips-lightbox.jpg" data-caption="bla.....">
    </a>
  </div>

  <div class="cycle-caption"></div>
</div>


Solution

  • There were few bugs in the way you were defining your caption.

    Mistake 1-

    First of all the data-caption attribute in not supported to display caption . After reading the documentation despite of data-caption you can use data-cycle-title for the captions.

    Mistake 2-

    Since the selector is anchor tag slides: "> a" in your jquery code, the captions will work on anchor tag not on the < img > . So I have added the data-cycle-title="Lorem ipsum dolor sit amet.." on the anchors. Also changes the captiontemplate jquery to this:

    captionTemplate: "' {{cycleTitle}}'",
    

    Below is the working example . Happy Coding :)

    $(document).ready(function() {
    
      $('.mySlideShow').cycle({
        log: false,
        fx: 'fade',
        slides: "> a",
        caption: '.cycle-caption',
        captionTemplate: "' {{cycleTitle}}'",
        pauseOnHover: true,
      });
    })
    .slide-gallery {
      position: relative;
      width: 790px;
      overflow: hidden;
      height: 500px;
    }
    
    .slide-gallery img {
      max-width: 100%;
      height: 100%;
    }
    
    .cycle-caption {
      position: absolute;
      bottom: 0;
      left: 0;
      background: rgba(0, 0, 0, .8);
      padding: 10px;
      color: #FFF;
      z-index: 1000;
      width: 100%;
      text-align: center;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.cycle2/2.1.6/jquery.cycle2.min.js"></script>
    
    <div class="slide-gallery">
      <div class="mySlideShow">
    
        <a data-cycle-title="Lorem ipsum dolor sit amet..">
          <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/dandelion-lightbox.jpg" >
        </a>
        <a data-cycle-title="This is my caption....">
          <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/daffodil-flowers-lightbox.jpg" >
        </a>
        <a data-cycle-title="bla.....">
          <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/tulips-lightbox.jpg" >
        </a>
      </div>
    
      <div class="cycle-caption"></div>
    </div>