简体   繁体   English

创建使用jQuery但不使用插件单击“下一个/上一个”时移动的响应式滑块

[英]Creating responsive slider that moves when clicking “next/previous” using jquery, but not using plugin

I've been trying to make a responsive slider and changes the image in the viewport when a user clicks the "next" or "previous" buttons. 我一直在尝试制作一个响应式滑块,并在用户单击“下一个”或“上一个”按钮时更改视口中的图像。 I also have dots indicating which picture the user is looking at, and I've been able to get that to change properly on click (though, it's certainly not the most "dry" code). 我也有一些点,指示用户正在查看的图片,而且我已经能够使它在单击时正确更改(尽管这当然不是最“干”的代码)。

I'm stumped on how to move the "innerCaro" the proper amount when someone clicks either "previous" or "next." 当有人单击“上一个”或“下一个”时,我对如何正确移动“ innerCaro”感到困惑。 I've tried a few things (trying to figure out the position of the caro at any given time and moving it depending on the position.), but nothing has worked. 我已经尝试了一些方法(试图在任何给定时间确定caro的位置,然后根据位置进行移动。),但是没有任何效果。

Also, I can't use any plugins for this. 另外,我不能为此使用任何插件。

Here's a Codepen of what I have so far. 这是到目前为止我所拥有的Codepen

And here is the code: 这是代码:

CSS 的CSS

.outercaro {
  width:66vw;
  overflow:hidden;
  position:relative;
}
.innerCaro {
    list-style:none;
    position:absolute;
    top:0;
    transition:all 1s ease;
    width: 700%;
    }
.dotContainer {
  width:21vw;
  margin:0 auto;
  height:3.91vw;    
}

.caroIMG {
    width:100%;
    float: left;
}

.innerCaro li {
    width: 66vw;
    float: left;
}

.dot {
  height:1vw;
  width:1vw;
  border-radius:50%;
  float:left;
  margin:1vw 1vw;
  background-color:#fff;
  cursor:pointer;
}

.dot:hover {
  background-color:#4D4D4E;
}
.activeDot {
    background-color:#4D4D4E;   
}

HTML 的HTML

<div class='outerCaro' style='width:66vw;overflow:hidden;position:relative;'>
        <ul class='innerCaro'>
            <li>
        <img class="caroIMG" src="//images.crateandbarrel.com/is/image/Crate/dHP_NwArrvls2trSrv_160701?wid=1228&qlt=50,0" alt=''>
            </li>

            <li>
        <img class="caroIMG" src="//images.crateandbarrel.com/is/image/Crate/dHP_NwArrvlsCeleste_160701_JL16?wid=1228&qlt=50,0" alt=''>
            </li>

            <li>
        <img class="caroIMG" src="//images.crateandbarrel.com/is/image/Crate/dHP_NwArrvlsEgypt_160701?wid=1228&qlt=50,0" alt=''>
            </li>

            <li>
        <img class="caroIMG" src="//images.crateandbarrel.com/is/image/Crate/dHP_NwArrvlsGeoffrey_160701_OMN16?wid=1228&qlt=50,0" alt=''>
            </li>

            <li>
        <img class="caroIMG" src="//images.crateandbarrel.com/is/image/Crate/dHP_NwArrvlsMarin_160701_OMN16?wid=1228&qlt=50,0" alt=''>
            </li>

            <li>
        <img class="caroIMG" src="//images.crateandbarrel.com/is/image/Crate/dHP_NwArrvlsSonata_160701?wid=1228&qlt=50,0" alt=''>
            </li>
            <li>
        <img class='caroIMG' src="//images.crateandbarrel.com/is/image/Crate/dHP_NwArrvlsSrvng_160701_JL16?wid=1228&qlt=50,0" alt="">
            </li>
        </ul>
        <div style='width:66vw;height:3.42vw;bottom:2%;position:absolute;'>
            <div class='dotContainer' style='width:21vw;margin:0 auto;height:3.91vw;'>
                <div id='dot1' class='dot activeDot'></div>
                <div id='dot2' class='dot'></div>
                <div id='dot3' class='dot'></div>
            <div id='dot4' class='dot'></div>
            <div id='dot5' class='dot'></div>
            <div id='dot6' class='dot'></div>
            <div id='dot7' class='dot'></div>
          </div>
        </div>
        <div style='width:66vw;height:3.42vw;bottom:40%;position:absolute;'>
            <div class='prevBtn' style='display:inline-block;float:left;cursor:pointer'>
                <img src="//images.crateandbarrel.com/is/image/Crate/arrow_Gray_LEFT?&fmt=png-alpha" alt="" />
            </div>
        <div class='nextBtn' style='display:inline-block;float:right;cursor:pointer'>
            <img src="//images.crateandbarrel.com/is/image/Crate/arrow_Gray_RIGHT?&fmt=png-alpha" alt="" />
        </div>
      </div>
    </div>
</div>

jQuery jQuery的

var $caroHeight = $('.caroIMG').css('height');
    $('.outerCaro').delay(1000).css('height', $caroHeight);

$(window).resize(function() {
      $caroHeight = $('.caroIMG').css('height');
      $('.outerCaro').css('height', $caroHeight);
    });

$('#dot1').click(function(){
      $('.dot').removeClass('activeDot');
      $(this).addClass('activeDot')
      $('.innerCaro').css('margin-left', '0px');
    })
    $('#dot2').click(function(){
      $('.dot').removeClass('activeDot');
      $(this).addClass('activeDot')
      $('.innerCaro').css('margin-left', '-100%');
    })
    $('#dot3').click(function(){
      $('.dot').removeClass('activeDot');
      $(this).addClass('activeDot')
      $('.innerCaro').css('margin-left', '-200%');
    })
    $('#dot4').click(function(){
      $('.dot').removeClass('activeDot');
      $(this).addClass('activeDot')
      $('.innerCaro').css('margin-left', '-300%');
    })
    $('#dot5').click(function(){
      $('.dot').removeClass('activeDot');
      $(this).addClass('activeDot')
      $('.innerCaro').css('margin-left', '-400%');
    })
    $('#dot6').click(function(){
      $('.dot').removeClass('activeDot');
      $(this).addClass('activeDot')
      $('.innerCaro').css('margin-left', '-500%');
    })
    $('#dot7').click(function(){
      $('.dot').removeClass('activeDot');
      $(this).addClass('activeDot')
      $('.innerCaro').css('margin-left', '-600%');
    })

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Here's a Codepen with the working arrows. 这是带有工作箭头的Codepen

Could also be more dry! 也可以更干!

Here's the code: 这是代码:

CSS unchanged CSS不变

HTML changes: HTML更改:

<div class='dotContainer' style='width:21vw;margin:0 auto;height:3.91vw;'>
  <div dot-index='1' class='dot activeDot'></div>
  <div dot-index='2' class='dot'></div>
  <div dot-index='3' class='dot'></div>
  <div dot-index='4' class='dot'></div>
  <div dot-index='5' class='dot'></div>
  <div dot-index='6' class='dot'></div>
  <div dot-index='7' class='dot'></div>
</div>

Js changes: js更改:

Added maxDotIndex to check if our target will be past the last picture 添加了maxDotIndex以检查我们的目标是否将超过最后一张图片

var $maxDotIndex = $('.dot:last-child').attr('dot-index');

Clicking on a dot... 点击一个点...

$('.dot').click(function(){
  var currentDot = $('.activeDot');
  var currentDotIndex = currentDot.attr('dot-index');
  var targetDot = $(this);
  var targetDotIndex = targetDot.attr('dot-index');

  if (currentDotIndex == targetDotIndex) {
    return
  } else {
    currentDot.removeClass('activeDot');
    targetDot.addClass('activeDot');
    var margin = ((targetDotIndex - 1) * -100) + '%';
    $('.innerCaro').css('margin-left', margin); 
  }
})

Clicking on the arrows... 点击箭头...

$('.nextBtn').click(function(){
  var currentDot = $('.activeDot');
  var currentDotIndex = currentDot.attr('dot-index');

  if (currentDotIndex == $maxDotIndex) {
    return
  } else {
    var targetDotIndex = (parseInt(currentDotIndex) + 1);
    var dotSelector = ".dot[dot-index='"+ targetDotIndex + "']";
    var targetDot = $(dotSelector);
    currentDot.removeClass('activeDot');
    targetDot.addClass('activeDot');
    var margin = ((targetDotIndex - 1) * -100) + '%';
    $('.innerCaro').css('margin-left', margin);
  }
})

$('.prevBtn').click(function(){
  var currentDot = $('.activeDot');
  var currentDotIndex = currentDot.attr('dot-index');

  if (currentDotIndex == 1) {
    return
  } else {
    var targetDotIndex = (parseInt(currentDotIndex) - 1);
    var dotSelector = ".dot[dot-index='"+ targetDotIndex + "']";
    var targetDot = $(dotSelector);
    currentDot.removeClass('activeDot');
    targetDot.addClass('activeDot');
    var margin = ((targetDotIndex - 1) * -100) + '%';
    $('.innerCaro').css('margin-left', margin);
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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