滑块横幅转换从所选幻灯片开始

时间:2018-02-02 07:49:53

标签: javascript jquery slider

我正在制作一个简单的横幅滑块,因此它会自动旋转,我有点导航显示或显示所选的横幅滑块,我的问题是我无法实现我的目标,让过渡从所选图像开始。< / p>

我在没有人回答之前发布这个问题。

我希望你理解我。

感谢。

codepen

$(document).on('ready', function() {
  var slide = $('.slider-single');
  var slideTotal = slide.length - 1;
  var slideCurrent = -1;

  function slideInitial() {
    slide.addClass('proactivede');
    setTimeout(function() {
      slideRight();
    }, 500);
  }

  function do_slide(){
    interval = setInterval(function(){
      slideRight();

      

      $('.dot').removeClass("active");
      $('[data-dot=' + slideCurrent + ']').addClass("active");
      
    }, 5000);
  }
  do_slide();
  
  function slideRight() {
    if (slideCurrent < slideTotal) {
      slideCurrent++;
    } else {
      slideCurrent = 0;
    }

    if (slideCurrent > 0) {
      var preactiveSlide = slide.eq(slideCurrent - 1);
    } else {
      var preactiveSlide = slide.eq(slideTotal);
    }
    var activeSlide = slide.eq(slideCurrent);
    if (slideCurrent < slideTotal) {
      var proactiveSlide = slide.eq(slideCurrent + 1);
    } else {
      var proactiveSlide = slide.eq(0);

    }

    slide.each(function() {
      var thisSlide = $(this);
      if (thisSlide.hasClass('preactivede')) {
        thisSlide.removeClass('preactivede preactive active proactive').addClass('proactivede');
      }
      if (thisSlide.hasClass('preactive')) {
        thisSlide.removeClass('preactive active proactive proactivede').addClass('preactivede');
      }
    });
    preactiveSlide.removeClass('preactivede active proactive proactivede').addClass('preactive');
    activeSlide.removeClass('preactivede preactive proactive proactivede').addClass('active');
    proactiveSlide.removeClass('preactivede preactive active proactivede').addClass('proactive');
  }

  function slideLeft() {
    if (slideCurrent > 0) {
      slideCurrent--;
    } else {
      slideCurrent = slideTotal;
    }

    if (slideCurrent < slideTotal) {
      var proactiveSlide = slide.eq(slideCurrent + 1);
    } else {
      var proactiveSlide = slide.eq(0);
    }
    var activeSlide = slide.eq(slideCurrent);
    if (slideCurrent > 0) {
      var preactiveSlide = slide.eq(slideCurrent - 1);
    } else {
      var preactiveSlide = slide.eq(slideTotal);
    }
    slide.each(function() {
      var thisSlide = $(this);
      if (thisSlide.hasClass('proactivede')) {
        thisSlide.removeClass('preactive active proactive proactivede').addClass('preactivede');
      }
      if (thisSlide.hasClass('proactive')) {
        thisSlide.removeClass('preactivede preactive active proactive').addClass('proactivede');
      }
    });
    preactiveSlide.removeClass('preactivede active proactive proactivede').addClass('preactive');
    activeSlide.removeClass('preactivede preactive proactive proactivede').addClass('active');
    proactiveSlide.removeClass('preactivede preactive active proactivede').addClass('proactive');
  }
  var left = $('.slider-left');
  var right = $('.slider-right');
  left.on('click', function() {
    slideLeft();
  });
  right.on('click', function() {
    slideRight();
  });
  slideInitial();
});


$(".dot").click(function(){
        var dotValue = $(this).data("dot");



$('[data-slide=' + dotValue + ']').siblings().removeClass("active preactive proactive").addClass("proactivede");
$('[data-slide=' + dotValue + ']').addClass("active").removeClass("preactive preactivede proactive proactivede");
$('[data-slide=' + dotValue + ']').prev().addClass("preactive").removeClass("active proactive preactivede proactivede");
$('[data-slide=' + dotValue + ']').next().addClass("proactive").removeClass("active preactive preactivede proactivede");

        $(".dot").removeClass("active");
        $(this).addClass("active");

      });
@keyframes heartbeat {
  0% {
    transform: scale(0);
  }
  25% {
    transform: scale(1.2);
  }
  50% {
    transform: scale(1);
  }
  75% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
  }
}
body {
  color: #FFFFFF;
  background-color: #27292d;
  overflow: hidden;
}

.indicators{

  position: absolute;
  z-index: 3;
  display: block;
  top: 60%;
  right: 30%;
}
.dot{
  cursor:pointer;
  height: 8px;
  width: 8px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: width 0.6s ease;
}
.dot.active{
  width: 36px;
  border-radius: 4px;
}

.slider-container {
  position: absolute;
  left: 50%;
  top: 80%;
  width: 740px;
  height: 290px;
  margin: -300px 0 0 -400px;
}
.slider-container .slider-content {
  position: relative;
  left: 50%;
  top: 50%;
  width: 77%;
  height: 100%;
  transform: translate(-50%, -50%);
}
.slider-container .slider-content .slider-single {
  position: absolute;
  z-index: 0;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  transition: z-index 0ms 250ms;
}
.slider-container .slider-content .slider-single .slider-single-image {
  position: relative;
  border-radius: 10px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  box-shadow: 0px 10px 40px rgba(0, 0, 0, 0.2);
  transition: 500ms cubic-bezier(0.17, 0.67, 0.55, 1.43);
  transform: scale(0);
  opacity: 0;
}
.slider-container .slider-content .slider-single .slider-single-download {
  position: absolute;
  display: block;
  right: -22px;
  bottom: 12px;
  padding: 15px;
  color: #333333;
  background-color: #fdc84b;
  font-size: 18px;
  font-weight: 600;
  font-family: 'karla';
  border-radius: 5px;
  box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.3);
  transition: 500ms cubic-bezier(0.17, 0.67, 0.55, 1.43);
  opacity: 0;
}
.slider-container .slider-content .slider-single .slider-single-download:hover, .slider-container .slider-content .slider-single .slider-single-download:focus {
  outline: none;
  text-decoration: none;
}
.slider-container .slider-content .slider-single .slider-single-title {
  display: block;
  float: left;
  margin: 16px 0 0 20px;
  font-size: 20px;
  font-family: 'karla';
  font-weight: 400;
  color: #ffffff;
  transition: 500ms cubic-bezier(0.17, 0.67, 0.55, 1.43);
  opacity: 0;
}
.slider-container .slider-content .slider-single .slider-single-likes {
  display: block;
  float: right;
  margin: 16px 20px 0 0;
  transition: 500ms cubic-bezier(0.17, 0.67, 0.55, 1.43);
  opacity: 0;
}
.slider-container .slider-content .slider-single .slider-single-likes i {
  font-size: 20px;
  display: inline-block;
  vertical-align: middle;
  margin-right: 5px;
  color: #ff6060;
  transition: 500ms cubic-bezier(0.17, 0.67, 0.55, 1.43);
  transform: scale(0);
}
.slider-container .slider-content .slider-single .slider-single-likes p {
  display: inline-block;
  vertical-align: middle;
  margin: 0;
  color: #ffffff;
}
.slider-container .slider-content .slider-single .slider-single-likes:hover, .slider-container .slider-content .slider-single .slider-single-likes:focus {
  outline: none;
  text-decoration: none;
}
.slider-container .slider-content .slider-single.preactivede .slider-single-image {
  transform: translateX(-50%) scale(0);
}
.slider-container .slider-content .slider-single.preactive {
  z-index: 1;
}
.slider-container .slider-content .slider-single.preactive .slider-single-image {
  opacity: .3;
  transform: translateX(-25%) scale(0.8);
}
.slider-container .slider-content .slider-single.preactive .slider-single-download {
  transform: translateX(-150px);
}
.slider-container .slider-content .slider-single.preactive .slider-single-title {
  transform: translateX(-150px);
}
.slider-container .slider-content .slider-single.preactive .slider-single-likes {
  transform: translateX(-150px);
}
.slider-container .slider-content .slider-single.proactive {
  z-index: 1;
}
.slider-container .slider-content .slider-single.proactive .slider-single-image {
  opacity: .3;
  transform: translateX(25%) scale(0.8);
}
.slider-container .slider-content .slider-single.proactive .slider-single-download {
  transform: translateX(150px);
}
.slider-container .slider-content .slider-single.proactive .slider-single-title {
  transform: translateX(150px);
}
.slider-container .slider-content .slider-single.proactive .slider-single-likes {
  transform: translateX(150px);
}
.slider-container .slider-content .slider-single.proactivede .slider-single-image {
  transform: translateX(50%) scale(0);
}
.slider-container .slider-content .slider-single.active {
  z-index: 2;
  width: 640px;
  margin-left: -35px;
}
.slider-container .slider-content .slider-single.active .slider-single-image {
  opacity: 1;
  transform: translateX(0%) scale(1);
}
.slider-container .slider-content .slider-single.active .slider-single-download {
  opacity: 1;
  transition-delay: 100ms;
  transform: translateX(0px);
}
.slider-container .slider-content .slider-single.active .slider-single-title {
  opacity: 1;
  transition-delay: 200ms;
  transform: translateX(0px);
}
.slider-container .slider-content .slider-single.active .slider-single-likes {
  opacity: 1;
  transition-delay: 300ms;
  transform: translateX(0px);
}
.slider-container .slider-content .slider-single.active .slider-single-likes i {
  animation-name: heartbeat;
  animation-duration: 500ms;
  animation-delay: 900ms;
  animation-interation: 1;
  animation-fill-mode: forwards;
}
.slider-container .slider-left {
  position: absolute;
  z-index: 3;
  display: block;
  right: 85%;
  top: 50%;
  color: #ffffff;
  transform: translateY(-50%);
  padding: 20px 15px;
  border-top: 2px solid #fdc84b;
  border-right: 2px solid #fdc84b;
  border-bottom: 2px solid #fdc84b;
  border-left: 2px solid #fdc84b;
  margin-right: -2px;
}
.slider-container .slider-right {
  position: absolute;
  z-index: 3;
  display: block;
  left: 85%;
  top: 50%;
  color: #ffffff;
  transform: translateY(-50%);
  padding: 20px 15px;
  border-top: 2px solid #fdc84b;
  border-right: 2px solid #fdc84b;
  border-bottom: 2px solid #fdc84b;
  border-left: 2px solid #fdc84b;
  margin-left: -2px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="slider-container">

  <div class="slider-content">

    <div class="slider-single" data-slide="0">
      <img class="slider-single-image" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ1Ryrw8RcKm866nT-TcscKEKQUMvsrnhdNLT3y06nvxb-FhfDt" alt="1" />
      
    </div>

    <div class="slider-single" data-slide="1">
      <img class="slider-single-image" src="https://images.gaiawallpapers.com/wallpapersbin.20170831/tn-259x195/200206141-relaxing-cat.jpg" alt="1" />
      
    </div>

    <div class="slider-single" data-slide="2">
      <img class="slider-single-image" src="https://archive-media-0.nyafuu.org/wsr/image/1507/95/1507952581944.png" alt="2" />
      
    </div>


    <div class="slider-single" data-slide="3">
      <img class="slider-single-image" src="https://archive-media-1.nyafuu.org/vp/image/1413/42/1413428577192.jpg" alt="3" />
      
    </div>


    <div class="slider-single" data-slide="4">
      <img class="slider-single-image" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ1Ryrw8RcKm866nT-TcscKEKQUMvsrnhdNLT3y06nvxb-FhfDt" alt="4" />
     
    </div>

    <div class="slider-single" data-slide="5">
      <img class="slider-single-image" src="http://www.ourfurryfriendsonline.com/sitebuilder/images/Cherie6-259x192.jpg" alt="5" />
      
    </div>
    <div class="slider-single" data-slide="6">
      <img class="slider-single-image" src="https://archive-media-0.nyafuu.org/wsr/image/1507/95/1507952581944.png" alt="6" />
      
    </div>
      
    </div>
  
  </div>
<div class="indicators" style="text-align:center">
  <span class="dot active" data-dot="0"></span> 
  <span class="dot" data-dot="1"></span> 
  <span class="dot" data-dot="2"></span> 
  <span class="dot" data-dot="3"></span> 
  <span class="dot" data-dot="4"></span>
  <span class="dot" data-dot="5"></span>
  <span class="dot" data-dot="6"></span>
</div>
  

</div>

1 个答案:

答案 0 :(得分:1)

如果您的问题是点击该点,请将此作为您的js CodePen

$(document).on('ready', function() {
  var slide = $('.slider-single');
  var slideTotal = slide.length - 1;
  var slideCurrent = -1;

  function slideInitial() {
    slide.addClass('proactivede');
    setTimeout(function() {
      slideRight();
    }, 500);
  }

  function do_slide(){
    interval = setInterval(function(){
      slideRight();



      $('.dot').removeClass("active");
      $('[data-dot=' + slideCurrent + ']').addClass("active");

    }, 5000);
  }
  do_slide();

  function slideRight() {
    if (slideCurrent < slideTotal) {
      slideCurrent++;
    } else {
      slideCurrent = 0;
    }

    if (slideCurrent > 0) {
      var preactiveSlide = slide.eq(slideCurrent - 1);
    } else {
      var preactiveSlide = slide.eq(slideTotal);
    }
    var activeSlide = slide.eq(slideCurrent);
    if (slideCurrent < slideTotal) {
      var proactiveSlide = slide.eq(slideCurrent + 1);
    } else {
      var proactiveSlide = slide.eq(0);

    }

    slide.each(function() {
      var thisSlide = $(this);
      if (thisSlide.hasClass('preactivede')) {
        thisSlide.removeClass('preactivede preactive active proactive').addClass('proactivede');
      }
      if (thisSlide.hasClass('preactive')) {
        thisSlide.removeClass('preactive active proactive proactivede').addClass('preactivede');
      }
    });
    preactiveSlide.removeClass('preactivede active proactive proactivede').addClass('preactive');
    activeSlide.removeClass('preactivede preactive proactive proactivede').addClass('active');
    proactiveSlide.removeClass('preactivede preactive active proactivede').addClass('proactive');
  }

  function slideLeft() {
    if (slideCurrent > 0) {
      slideCurrent--;
    } else {
      slideCurrent = slideTotal;
    }

    if (slideCurrent < slideTotal) {
      var proactiveSlide = slide.eq(slideCurrent + 1);
    } else {
      var proactiveSlide = slide.eq(0);
    }
    var activeSlide = slide.eq(slideCurrent);
    if (slideCurrent > 0) {
      var preactiveSlide = slide.eq(slideCurrent - 1);
    } else {
      var preactiveSlide = slide.eq(slideTotal);
    }
    slide.each(function() {
      var thisSlide = $(this);
      if (thisSlide.hasClass('proactivede')) {
        thisSlide.removeClass('preactive active proactive proactivede').addClass('preactivede');
      }
      if (thisSlide.hasClass('proactive')) {
        thisSlide.removeClass('preactivede preactive active proactive').addClass('proactivede');
      }
    });
    preactiveSlide.removeClass('preactivede active proactive proactivede').addClass('preactive');
    activeSlide.removeClass('preactivede preactive proactive proactivede').addClass('active');
    proactiveSlide.removeClass('preactivede preactive active proactivede').addClass('proactive');
  }
  var left = $('.slider-left');
  var right = $('.slider-right');
  left.on('click', function() {
    slideLeft();
  });
  right.on('click', function() {
    slideRight();
  });
  slideInitial();

  $(".dot").click(function(){
        var dotValue = $(this).data("dot");
        slideCurrent =  dotValue; 



$('[data-slide=' + dotValue + ']').siblings().removeClass("active preactive proactive").addClass("proactivede");
$('[data-slide=' + dotValue + ']').addClass("active").removeClass("preactive preactivede proactive proactivede");
$('[data-slide=' + dotValue + ']').prev().addClass("preactive").removeClass("active proactive preactivede proactivede");
$('[data-slide=' + dotValue + ']').next().addClass("proactive").removeClass("active preactive preactivede proactivede");

        $(".dot").removeClass("active");
        $(this).addClass("active");

      });
});