user10098657
user10098657

Reputation:

Slider with tabs

I have a slider tab with text and a related background images to it.when ever I click to the slider tab the related image should visible with sliding animation it. The problem occurs in slider images the If condition is not working when ever I click again to the image it display none. `

HTML:

<div class="ce-slider-wrapper">
  <div class="ce-slider-images-wrapper" id="csw">
    <div class="ce-slider-image active-frame" id="ce-img1C">
      <img src="https://cdn2.hubspot.net/hubfs/5215930/core-experience/svasahome.png" alt="Core Experience">
    </div>
    <div class="ce-slider-image" id="ce-img2C">
      <img src="https://cdn2.hubspot.net/hubfs/5215930/core-experience/Livingroom%20Classic%201.png" alt="Core Experience">
    </div>
    <div class="ce-slider-image" id="ce-img3C">
      <img src="https://cdn2.hubspot.net/hubfs/5215930/core-experience/indivillage.png" alt="Core Experience">
    </div>
  </div>
  <div class="ce-slider-thumbs" id="ce-thumbs">
    <div class="ce-slider-content">
      <div class="ce-desc" id="ce-img1">
        <div class="ce-desc-subheading">
          <div class="ce-desc-line"></div>
          <p>01 / SVASA HOMES</p>
        </div>
        <div class="ce-desc-text">
          <p>The Revival of Ancient Wisdom</p>
        </div>
      </div>
    </div>
    <div class="ce-slider-content">
      <div class="ce-desc" id="ce-img2">
        <div class="ce-desc-subheading">
          <div class="ce-desc-line"></div>
          <p>02 / VAYATHI WEAVES</p>
        </div>
        <div class="ce-desc-text">
          <p>Weaving A Grand New Story</p>
        </div>
      </div>
    </div>
    <div class="ce-slider-content">
      <div class="ce-desc" id="ce-img3">
        <div class="ce-desc-subheading">
          <div class="ce-desc-line"></div>
          <p>03 / THOTA</p>
        </div>
        <div class="ce-desc-text">
          <p>Uplifting Farmers From The Ground Up</p>
        </div>
      </div>
    </div>
  </div>
</div>

jQuery:

$(document).ready(function() {
  $('.ce-slider-thumbs').slick({
    slidesToShow: 2,
    slidesToScroll: 1,
    autoplay: true,
    infinite: true,
    arrows: false,
    dots: false,
    fade: false,
    centerMode: false,
    autoplaySpeed: 2000,
    focusOnSelect: true,
    draggable: false
  });

  $('#ce-thumbs .ce-desc:not(:first)').addClass('inactive');
  $('.ce-slider-image').hide();
  $('.active-frame').show();

  $('.ce-desc').click(function() {
    var css = $(this).attr('id');
    if ($(this).hasClass('inactive')) { //this is the start of our condition 
      $('.ce-desc').addClass('inactive');
      $(this).removeClass('inactive');
      $(this).addClass('active-tab');
      $('.ce-slider-image').hide();
      $('#' + css + 'C').css('display', 'block');
    }
  });

});

CSS:

.ce-slider-wrapper {
  width: 100%;
  height: 100vh;
  position: relative;
}

.ce-slider-image {
  width: 100%;
  height: 100vh;
}

.ce-slider-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.ce-slider-thumbs {
  position: absolute;
  bottom: 20px;
  left: 0px;
  right: 0px;
  display: flex;
  width: 60%;
  margin: auto;
  outline: none;
  justify-content: space-between;
}

.ce-slider-content {
  width: 30%;
  color: #fff;
}

.ce-desc {
  display: flex;
  flex-direction: column;
}

.ce-desc-line {
  width: 180px;
  height: 5px;
  background: #b7b7b7;
  opacity: 0.5;
}

.ce-desc-subheading p {
  font-size: 14px;
  color: #fff;
  font-family: arial;
  letter-spacing: 0.35rem;
}

.slick-cloned {
  display: none;
}

I have a slider tab with text and a related background images to it.when ever I click to the slider tab the related image should visible with sliding animation. The problem occurs in slider images the If condition is not working when ever I click again to the image it display none it is no.

Upvotes: 2

Views: 3204

Answers (2)

Asfan Shaikh
Asfan Shaikh

Reputation: 769

I have added slick slider property 'asNavFor' for linking Main slider and Thumb slider.

$(document).ready(function() {
  $('.ce-slider-thumbs').slick({
    slidesToShow: 2,
    slidesToScroll: 1,
    autoplay: false,
    infinite: true,
    arrows: false,
    dots: false,
    fade: false,
    centerMode: false,
    autoplaySpeed: 2000,
    focusOnSelect: true,
    draggable: false,
    asNavFor: '.ce-slider-images-wrapper'
  });

  $('.ce-slider-images-wrapper').slick({
    slidesToShow: 1,
    slidesToScroll: 1,
    autoplay: false,
    infinite: true,
    arrows: false,
    dots: false,
    fade: true,
    useCSS: false,
    centerMode: false,
    autoplaySpeed: 2000,
    focusOnSelect: true,
    draggable: false,
    asNavFor: '.ce-slider-thumbs'
  });

});
.ce-slider-wrapper {
  width: 100%;
  height: 100vh;
  position: relative;
}

.ce-slider-image {
  width: 100%;
  height: 100vh;
}

.ce-slider-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.ce-slider-thumbs.slick-slider {
  position: absolute;
}
.ce-slider-thumbs {
  position: absolute;
  bottom: 20px;
  left: 0px;
  right: 0px;
  display: flex;
  width: 60%;
  margin: auto;
  outline: none;
  justify-content: space-between;
}

.ce-slider-content {
  width: 30%;
  color: #fff;
}

.ce-desc {
  display: flex;
  flex-direction: column;
}

.ce-desc-line {
  width: 180px;
  height: 5px;
  background: #b7b7b7;
  opacity: 0.5;
}

.ce-desc-subheading p {
  font-size: 14px;
  color: #fff;
  font-family: arial;
  letter-spacing: 0.35rem;
}

.slick-cloned {
  display: none;
}

/*== Add this extra CSS ==*/
.ce-slider-images-wrapper .slick-slide{
  opacity: 0 !important;
}
.ce-slider-images-wrapper .slick-slide.slick-active{
  opacity: 1 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.js"></script>
<div class="ce-slider-wrapper">
  <div class="ce-slider-images-wrapper" id="csw">
    <div class="ce-slider-image active-frame" id="ce-img1C">
      <img src="https://cdn2.hubspot.net/hubfs/5215930/core-experience/svasahome.png" alt="Core Experience">
    </div>
    <div class="ce-slider-image" id="ce-img2C">
      <img src="https://cdn2.hubspot.net/hubfs/5215930/core-experience/Livingroom%20Classic%201.png" alt="Core Experience">
    </div>
    <div class="ce-slider-image" id="ce-img3C">
      <img src="https://cdn2.hubspot.net/hubfs/5215930/core-experience/indivillage.png" alt="Core Experience">
    </div>
  </div>
  <div class="ce-slider-thumbs" id="ce-thumbs">
    <div class="ce-slider-content">
      <div class="ce-desc" id="ce-img1">
        <div class="ce-desc-subheading">
          <div class="ce-desc-line"></div>
          <p>01 / SVASA HOMES</p>
        </div>
        <div class="ce-desc-text">
          <p>The Revival of Ancient Wisdom</p>
        </div>
      </div>
    </div>
    <div class="ce-slider-content">
      <div class="ce-desc" id="ce-img2">
        <div class="ce-desc-subheading">
          <div class="ce-desc-line"></div>
          <p>02 / VAYATHI WEAVES</p>
        </div>
        <div class="ce-desc-text">
          <p>Weaving A Grand New Story</p>
        </div>
      </div>
    </div>
    <div class="ce-slider-content">
      <div class="ce-desc" id="ce-img3">
        <div class="ce-desc-subheading">
          <div class="ce-desc-line"></div>
          <p>03 / THOTA</p>
        </div>
        <div class="ce-desc-text">
          <p>Uplifting Farmers From The Ground Up</p>
        </div>
      </div>
    </div>
  </div>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.css" />

Upvotes: 2

nazifa rashid
nazifa rashid

Reputation: 1504

Check if that works for you For reference you can also check https://www.w3schools.com/howto/howto_js_slideshow.asp this link. Hope it'd help I have included the text in one slide only just for reference. Use that for other slides as well

var slideIndex = 0;
showSlides();

function showSlides() {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";  
  }
  slideIndex++;
  if (slideIndex > slides.length) {slideIndex = 1}    
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " active";
  setTimeout(showSlides, 2000); // Change image every 2 seconds
}
* {box-sizing: border-box;}

body {
  font-family: Verdana, sans-serif;
}

.mySlides {
  display: none;
}

img {
  vertical-align: middle;
}

/* Slideshow container */
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot {
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active {
  background-color: #717171;
}

/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

.ce-slider-content {
  width: 30%;
  color: #fff;
  display: inline-flex;
}

.ce-desc {
  display: flex;
  flex-direction: column;
}

.ce-desc-line {
  width: 180px;
  height: 5px;
  background: #b7b7b7;
  opacity: 0.5;
}

.ce-desc-subheading p {
  font-size: 14px;
  color: #fff;
  font-family: arial;
  letter-spacing: 0.35rem;
}

.slick-cloned {
  display: none;
}

@-webkit-keyframes fade {
  from {
    opacity: .4
  } 
  to {
    opacity: 1
  }
}

@keyframes fade {
  from {
    opacity: .4
  } 
  to {
    opacity: 1
  }
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .text {
    font-size: 11px
  }
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div class="slideshow-container">

  <div class="mySlides fade">
    <img src="https://i.imgur.com/v9f1nS2.jpg" style="width:100%">
    <div class="text">
        <div class="ce-slider-content">
      <div class="ce-desc" id="ce-img1">
        <div class="ce-desc-subheading">
          <div class="ce-desc-line"></div>
          <p>01 / SVASA HOMES</p>
        </div>
        <div class="ce-desc-text">
          <p>The Revival of Ancient Wisdom</p>
        </div>
      </div>
    </div>
    <div class="ce-slider-content">
      <div class="ce-desc" id="ce-img2">
        <div class="ce-desc-subheading">
          <div class="ce-desc-line"></div>
          <p>02 / VAYATHI WEAVES</p>
        </div>
        <div class="ce-desc-text">
          <p>Weaving A Grand New Story</p>
        </div>
      </div>
    </div>
    <div class="ce-slider-content">
      <div class="ce-desc" id="ce-img3">
        <div class="ce-desc-subheading">
          <div class="ce-desc-line"></div>
          <p>03 / THOTA</p>
        </div>
        <div class="ce-desc-text">
          <p>Uplifting Farmers From The Ground Up</p>
        </div>
      </div>
    </div>
    </div>
  </div>

  <div class="mySlides fade">
    <img src="https://i.imgur.com/v9f1nS2.jpg" style="width:100%">
    <div class="text">Caption Two</div>
  </div>

  <div class="mySlides fade">
    <img src="https://i.imgur.com/v9f1nS2.jpg" style="width:100%">
    <div class="text">Caption Three</div>
  </div>

  </div>
  <br>

  <div style="text-align:center">
    <span class="dot"></span> 
    <span class="dot"></span> 
    <span class="dot"></span> 
  </div>
</body>
</html>

Upvotes: 0

Related Questions