点击幻灯片指示器点并显示相关幻灯片

时间:2019-01-15 14:54:14

标签: jquery optimization

注意:我已更新代码以包含答案中的建议

我正在构建自定义幻灯片,而这个个人项目即将结束。我要添加的一项功能是选择幻灯片指示器/点和相关幻灯片。但是,理想的操作如下:

  • 当前#slide1
  • 选择点#slide3Dot以显示#slide3
  • 幻灯片循环显示#slide2
  • 然后移至#slide3。突出显示点#slide3Dot

我已经掌握了幻灯片的基本操作,并将这些幻灯片的点链接起来。点的单击功能可以工作到一个点,但通过循环#slide1,跳过#slide2然后加载#slide3的方式可能更好。

我不希望直接得到答案,但是如果人们有任何很好的指导/提示。

我要补充的另一点是,我注意到重复很多。这些可以避免吗?

$(document).ready(function () {

$('.slide:eq(-1)').addClass('last');
$('.dot:first').addClass('active');
$('.slide:first').addClass('active').delay($duration).queue(function () {
    $(this).addClass('show-text');
});
$('.slide:eq(1)').addClass('next');

});

var $classes = 'last active show-text next';
var $duration = 1250;

// Start of Recently updated
$('.dot').on('click' , function(){   // the dot click
  
   var $This = $(this);
   var GetIndex = $This.closest('li').index() + 1;

   $('.dot').removeClass('active').filter($This).addClass('active');
  
  $('.slide').dequeue();
  $('.slide').removeClass($classes);
   
  $('#slide' + GetIndex ).addClass('active').delay($duration).queue(function () {
$(this).addClass('show-text');
  });
  $('.slide').eq(($('.slide.active').index() - 1) % $('.slide').length).addClass('last');
  $('.slide').eq(($('.slide.active').index() + 1) % $('.slide').length).addClass('next');

});

// End of recently updated

$('.button').click(function moveSlide() {

var $active = $('.slide.active');
var $prevSlide = $('.slide').eq(($active.index() - 1) % $('.slide').length);
var $afterPrevSlide = $('.slide').eq(($active.index() - 2) % $('.slide').length);
var $nextSlide = $('.slide').eq(($active.index() + 1) % $('.slide').length);
var $slideAfterNext = $('.slide').eq(($active.index() + 2) % $('.slide').length);
var $tagNextDot = $('#' + $prevSlide.attr('id') + 'Dot' );
var $tagPrevDot = $('#' + $nextSlide.attr('id') + 'Dot' );

$active.dequeue();
$('.slide').removeClass($classes)
$('.dot').removeClass('active');

if ($(this).is("#prev")) {

    $active.addClass('next');
    $tagNextDot.addClass('active');
    $prevSlide.addClass('active').delay($duration).queue(function () {
        $(this).addClass('show-text');
    });
    $afterPrevSlide.addClass('last');

} else {
    $active.addClass('last');
    $tagPrevDot.addClass('active');
    $nextSlide.addClass('active').delay($duration).queue(function () {
        $(this).addClass('show-text');
    });
    $slideAfterNext.addClass('next');
}

});
body {
  font-size: 16px;
  font-family: 'Heebo', sans-serif;
  text-transform: uppercase;
  font-weight: 900;
}

/* Slides */
.slide-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  overflow: hidden;
}

.slide {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 70%;
  left: 140%;
  z-index: 0;
  transition: 1.25s;
  box-shadow: -10px 0px 21px -5px rgba(0,0,0,0.5);
}

.slide h2 {
  display: none;
  color: #fff;
  text-shadow: 0px 0px 8px rgba(0,0,0,0.5);
  letter-spacing: -2px;
  font-size: 3rem;
}

.slide.active.show-text h2 {
  display: block;
  animation: reveal-text 1.5s forwards;
}

@keyframes reveal-text {
  0%   { opacity: 0; }
  100% { opacity: 1; }
}

#slide1 {
  background-image: radial-gradient(
      rgba(0, 0, 0, 0.05), 
      rgba(0, 0, 0, 0.35)
    ), url('https://picsum.photos/1250/1600/?random');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

#slide2 {
  background-image: radial-gradient(
      rgba(0, 0, 0, 0.05), 
      rgba(0, 0, 0, 0.35)
    ),  url('https://picsum.photos/1200/1600/?random');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

#slide3 {
  background-image: radial-gradient(
      rgba(0, 0, 0, 0.05), 
      rgba(0, 0, 0, 0.35)
    ), url('https://picsum.photos/1200/1500/?random');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

#slide4 {
  background-image: radial-gradient(
      rgba(0, 0, 0, 0.05), 
      rgba(0, 0, 0, 0.35)
    ), url('https://picsum.photos/1300/1600/?random');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.slide.last {
  left: 0;
  z-index: 0;
}

.slide.active {
  left: 0;
  z-index: 1;
}

.slide.next {
  left: 70%;
  z-index: 2;
}

.dots-wrapper {
  z-index: 10;
  list-style: none;
  padding-left: 0;
  position: absolute;
  bottom: 0;
  padding: 10px;
}

.dots-wrapper li {
  display: inline;
}

.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border: 2px solid #fff;
  border-radius: 6px;
}

.dot.active {
  background-color: red;
  border-color: red;
}

/* Buttons */
.button-wrapper {
  display: flex;
  z-index: 10;
  width: 100%;
  justify-content: space-between;
  align-items: center;
}

.button {
  background-color: rgba(0,0,0,0.45);
  color: #ddd;
  height: 40px;
  border: none;
  font-weight: bold;
  padding: 10px 20px;
  transition: 0.3s;
}

.button:hover {
  cursor: pointer;
  background: rgba(0,0,0,0.85);
  color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="slide-wrapper">
  <div id="slide1" class="slide">
    <h2>Slide One.</h2>
  </div>
  <div id="slide2" class="slide">
    <h2>Slide Two.</h2>
  </div>
  <div id="slide3" class="slide">
    <h2>Slide Three.</h2>
  </div>
  <div id="slide4" class="slide">
    <h2>Slide Four.</h2>
  </div>
  <div class="button-wrapper"> 
    <button id="prev" class="button">Prev.</button>
    <button id="next" class="button">Next.</button>
  </div>
  <ul class="dots-wrapper">
    <li>
      <span id="slide1Dot" class="dot"></span>
    </li>
    <li>
      <span id="slide2Dot" class="dot"></span>
    </li>
    <li>
      <span id="slide3Dot" class="dot"></span>
    </li>
    <li>
      <span id="slide4Dot" class="dot"></span>
    </li>
  </ul>
</div>

1 个答案:

答案 0 :(得分:1)

您做得非常好..单击.dot可以从获取其li索引开始,然后使用该索引显示/隐藏幻灯片

$(document).ready(function () {

    $('.slide:eq(-1)').addClass('last');
    $('.dot:first').addClass('active');
    $('.slide:first').addClass('active').delay($duration).queue(function () {
        $(this).addClass('show-text');
    });
    $('.slide:eq(1)').addClass('next');

});

var $classes = 'last active show-text next';
var $duration = 1250;
$('.dot').on('click' , function(){   // the dot click
   var $This = $(this);
   var GetIndex = $This.closest('li').index(); // get this li index
   $('.dot').removeClass('active').filter($This).addClass('active');   // remove class active from other .dots and add the active class to the click one
   alert(GetIndex);
});
$('.button').click(function moveSlide() {

    var $active = $('.slide.active');
    var $prevSlide = $('.slide').eq(($active.index() - 1) % $('.slide').length);
    var $afterPrevSlide = $('.slide').eq(($active.index() - 2) % $('.slide').length);
    var $nextSlide = $('.slide').eq(($active.index() + 1) % $('.slide').length);
    var $slideAfterNext = $('.slide').eq(($active.index() + 2) % $('.slide').length);
    var $tagNextDot = $('#' + $prevSlide.attr('id') + 'Dot' );
    var $tagPrevDot = $('#' + $nextSlide.attr('id') + 'Dot' );

    $active.dequeue();
    $('.slide').removeClass($classes)
    $('.dot').removeClass('active');

    if ($(this).is("#prev")) {

        $active.addClass('next');
        $tagNextDot.addClass('active');
        $prevSlide.addClass('active').delay($duration).queue(function () {
            $(this).addClass('show-text');
        });
        $afterPrevSlide.addClass('last');

    } else {
        $active.addClass('last');
        $tagPrevDot.addClass('active');
        $nextSlide.addClass('active').delay($duration).queue(function () {
            $(this).addClass('show-text');
        });
        $slideAfterNext.addClass('next');
    }
});
body {
  font-size: 16px;
  font-family: 'Heebo', sans-serif;
  text-transform: uppercase;
  font-weight: 900;
}

/* Slides */
.slide-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  overflow: hidden;
}

.slide {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 70%;
  left: 140%;
  z-index: 0;
  transition: 1.25s;
  box-shadow: -10px 0px 21px -5px rgba(0,0,0,0.5);
}

.slide h2 {
  display: none;
  color: #fff;
  text-shadow: 0px 0px 8px rgba(0,0,0,0.5);
  letter-spacing: -2px;
  font-size: 3rem;
}

.slide.active.show-text h2 {
  display: block;
  animation: reveal-text 1.5s forwards;
}

@keyframes reveal-text {
  0%   { opacity: 0; }
  100% { opacity: 1; }
}

#slide1 {
  background-image: radial-gradient(
      rgba(0, 0, 0, 0.05), 
      rgba(0, 0, 0, 0.35)
    ), url('https://picsum.photos/1250/1600/?random');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

#slide2 {
  background-image: radial-gradient(
      rgba(0, 0, 0, 0.05), 
      rgba(0, 0, 0, 0.35)
    ),  url('https://picsum.photos/1200/1600/?random');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

#slide3 {
  background-image: radial-gradient(
      rgba(0, 0, 0, 0.05), 
      rgba(0, 0, 0, 0.35)
    ), url('https://picsum.photos/1200/1500/?random');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

#slide4 {
  background-image: radial-gradient(
      rgba(0, 0, 0, 0.05), 
      rgba(0, 0, 0, 0.35)
    ), url('https://picsum.photos/1300/1600/?random');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.slide.last {
  left: 0;
  z-index: 0;
}

.slide.active {
  left: 0;
  z-index: 1;
}

.slide.next {
  left: 70%;
  z-index: 2;
}

.dots-wrapper {
  z-index: 10;
  list-style: none;
  padding-left: 0;
  position: absolute;
  bottom: 0;
  padding: 10px;
}

.dots-wrapper li {
  display: inline;
}

.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border: 2px solid #fff;
  border-radius: 6px;
}

.dot.active {
  background-color: red;
  border-color: red;
}

/* Buttons */
.button-wrapper {
  display: flex;
  z-index: 10;
  width: 100%;
  justify-content: space-between;
  align-items: center;
}

.button {
  background-color: rgba(0,0,0,0.45);
  color: #ddd;
  height: 40px;
  border: none;
  font-weight: bold;
  padding: 10px 20px;
  transition: 0.3s;
}

.button:hover {
  cursor: pointer;
  background: rgba(0,0,0,0.85);
  color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="slide-wrapper">
  <div id="slide1" class="slide">
    <h2>Slide One.</h2>
  </div>
  <div id="slide2" class="slide">
    <h2>Slide Two.</h2>
  </div>
  <div id="slide3" class="slide">
    <h2>Slide Three.</h2>
  </div>
  <div id="slide4" class="slide">
    <h2>Slide Four.</h2>
  </div>
  <div class="button-wrapper"> 
    <button id="prev" class="button">Prev.</button>
    <button id="next" class="button">Next.</button>
  </div>
  <ul class="dots-wrapper">
    <li>
      <span id="slide1Dot" class="dot"></span>
    </li>
    <li>
      <span id="slide2Dot" class="dot"></span>
    </li>
    <li>
      <span id="slide3Dot" class="dot"></span>
    </li>
    <li>
      <span id="slide4Dot" class="dot"></span>
    </li>
  </ul>
</div>

注意:它不是一个完整的答案,但它解释了使用li索引来获取被点击的.dot的数量的想法