简体   繁体   English

图像滑块 - 幻灯片动画 - jQuery

[英]Image slider - slide animation - jQuery

Ok, i'm stuck.好吧,我被困住了。 I need to implement animation on my slider over the jQuery.我需要在 jQuery 上的滑块上实现动画。 So far i done arrows (navigation) functionality but, can't figure it out how to add sliding effect (1, 2 seconds) after clicking the "next" and "prev" buttons.到目前为止,我完成了箭头(导航)功能,但是,在单击“下一步”和“上一步”按钮后,无法弄清楚如何添加滑动效果(1、2 秒)。 Anyone have idea?有人有想法吗?

 $(document).ready(function() { $('.next').on('click', function() { var currentImg = $('.current'); var nextImg = currentImg.next(); if(nextImg.length == 0) { nextImg = $('.slider-secondary img').first(); } currentImg.removeClass('current'); nextImg.addClass('current'); }); $('.previous').on('click', function() { var currentImg = $('.current'); var prevImg = currentImg.prev(); if(prevImg.length == 0) { prevImg = $('.slider-secondary img').last(); } currentImg.removeClass('current'); prevImg.addClass('current'); }); });
 <div class="container"> <div class="slider-primary"> <img src="Assets/arrow-blue-left.png" class="previous" alt="Prev"> <div class="slider-secondary"> <img src="Assets/slider-image-1.jpg" class="current"> <img src="Assets/slider-image-2.jpg"> <img src="Assets/slider-image-3.jpg"> <img src="Assets/slider-image-4.jpg"> <img src="Assets/slider-image-5.jpg"> <img src="Assets/slider-image-6.jpg"> <img src="Assets/slider-image-7.jpg"> <img src="Assets/slider-image-8.jpg"> <img src="Assets/slider-image-9.jpg"> </div> <img src="Assets/arrow-blue-right.png" class="next" alt="Next"> </div> </div>

Thanks a lot guys for your help and references.非常感谢您的帮助和参考。 I appreciate it.我很感激。 Slider must not be automatic, it can work only on arrows(navigation) click, which are (and must be) images/icons.滑块不能是自动的,它只能在箭头(导航)点击上工作,它们是(并且必须是)图像/图标。 Also it's not alowed to use any existing scripts, plugins, etc.. so i tried to implement some further logic having in mind and considering all of the conditions above.此外,不允许使用任何现有的脚本、插件等。所以我试图实现一些进一步的逻辑,并考虑到上述所有条件。 It's actually one part of the code test.它实际上是代码测试的一部分。 And must be in jQuery in which i don't flow so well obviously.并且必须在 jQuery 中,其中我显然没有那么好。

Here is the css part also这里也是css部分

 *{ margin: 0; padding: 0; } .container, .slider-primary{ width:100%; margin:auto; overflow: hidden; position: relative; } .slider-secondary{ width:100%; height:600px; position:relative; overflow:hidden; float:left; } .slider-secondary img{ display:none; width:100%; height:100%; } .slider-secondary img.current{ display:inline-block; } .previous, .next{ float:left; cursor: pointer; position: absolute; top: 45%; width: 30px; height: 40px; } .previous{ margin-left:35px; z-index:100; } .next{ margin-left:-65px; z-index:100; }

You can use jQuery animate() Method.您可以使用 jQuery animate()方法。 Here is the link to the documentation .这是文档的链接

But I would suggest using any jQuery lightweight library for the same.但我建议使用任何 jQuery 轻量级库。

You can go for Responsive and Flexible Mobile Touch Slider - Swiper您可以选择响应式且灵活的移动触控滑块 - Swiper

Demo of the same! 演示的一样!

Here is one working example.这是一个工作示例。 Hope it'll help you.希望它会帮助你。 for the full reference visit codepen完整的参考访问codepen

 $('.slider').each(function() {
      var $this = $(this);
      var $group = $this.find('.slide_group');
      var $slides = $this.find('.slide');
      var bulletArray = [];
      var currentIndex = 0;
      var timeout;

      function move(newIndex) {
        var animateLeft, slideLeft;

        advance();

        if ($group.is(':animated') || currentIndex === newIndex) {
          return;
        }

        bulletArray[currentIndex].removeClass('active');
        bulletArray[newIndex].addClass('active');

        if (newIndex > currentIndex) {
          slideLeft = '100%';
          animateLeft = '-100%';
        } else {
          slideLeft = '-100%';
          animateLeft = '100%';
        }

        $slides.eq(newIndex).css({
          display: 'block',
          left: slideLeft
        });
        $group.animate({
          left: animateLeft
        }, function() {
          $slides.eq(currentIndex).css({
            display: 'none'
          });
          $slides.eq(newIndex).css({
            left: 0
          });
          $group.css({
            left: 0
          });
          currentIndex = newIndex;
        });
      }

      function advance() {
        clearTimeout(timeout);
        timeout = setTimeout(function() {
          if (currentIndex < ($slides.length - 1)) {
            move(currentIndex + 1);
          } else {
            move(0);
          }
        }, 4000);
      }

      $('.next_btn').on('click', function() {
        if (currentIndex < ($slides.length - 1)) {
          move(currentIndex + 1);
        } else {
          move(0);
        }
      });

      $('.previous_btn').on('click', function() {
        if (currentIndex !== 0) {
          move(currentIndex - 1);
        } else {
          move(3);
        }
      });

      $.each($slides, function(index) {
        var $button = $('<a class="slide_btn">&bull;</a>');

        if (index === currentIndex) {
          $button.addClass('active');
        }
        $button.on('click', function() {
          move(index);
        }).appendTo('.slide_buttons');
        bulletArray.push($button);
      });

      advance();
    });

Try below way.试试下面的方法。 I have created a demo for you.我已经为你创建了一个演示。

Please Find below code:请找到以下代码:

Html:网址:

<h1>Incredibly Basic Slider</h1>
<div id="slider">
  <a href="#" class="control_next">></a>
  <a href="#" class="control_prev"><</a>
  <ul>
    <li>SLIDE 1</li>
    <li style="background: #aaa;">SLIDE 2</li>
    <li>SLIDE 3</li>
    <li style="background: #aaa;">SLIDE 4</li>
  </ul>  
</div>

<div class="slider_option">
  <input type="checkbox" id="checkbox">
  <label for="checkbox">Autoplay Slider</label>
</div> 

Css: css:

@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,300,600); 

html {
  border-top: 5px solid #fff;
  background: #58DDAF;
  color: #2a2a2a;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans';
}

h1 {
  color: #fff;
  text-align: center;
  font-weight: 300;
}

#slider {
  position: relative;
  overflow: hidden;
  margin: 20px auto 0 auto;
  border-radius: 4px;
}

#slider ul {
  position: relative;
  margin: 0;
  padding: 0;
  height: 200px;
  list-style: none;
}

#slider ul li {
  position: relative;
  display: block;
  float: left;
  margin: 0;
  padding: 0;
  width: 500px;
  height: 300px;
  background: #ccc;
  text-align: center;
  line-height: 300px;
}

a.control_prev, a.control_next {
  position: absolute;
  top: 40%;
  z-index: 999;
  display: block;
  padding: 4% 3%;
  width: auto;
  height: auto;
  background: #2a2a2a;
  color: #fff;
  text-decoration: none;
  font-weight: 600;
  font-size: 18px;
  opacity: 0.8;
  cursor: pointer;
}

a.control_prev:hover, a.control_next:hover {
  opacity: 1;
  -webkit-transition: all 0.2s ease;
}

a.control_prev {
  border-radius: 0 2px 2px 0;
}

a.control_next {
  right: 0;
  border-radius: 2px 0 0 2px;
}

.slider_option {
  position: relative;
  margin: 10px auto;
  width: 160px;
  font-size: 18px;
}

Js: JS:

jQuery(document).ready(function ($) {

  $('#checkbox').change(function(){
    setInterval(function () {
        moveRight();
    }, 3000);
  });

    var slideCount = $('#slider ul li').length;
    var slideWidth = $('#slider ul li').width();
    var slideHeight = $('#slider ul li').height();
    var sliderUlWidth = slideCount * slideWidth;

    $('#slider').css({ width: slideWidth, height: slideHeight });

    $('#slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });

    $('#slider ul li:last-child').prependTo('#slider ul');

    function moveLeft() {
        $('#slider ul').animate({
            left: + slideWidth
        }, 200, function () {
            $('#slider ul li:last-child').prependTo('#slider ul');
            $('#slider ul').css('left', '');
        });
    };

    function moveRight() {
        $('#slider ul').animate({
            left: - slideWidth
        }, 200, function () {
            $('#slider ul li:first-child').appendTo('#slider ul');
            $('#slider ul').css('left', '');
        });
    };

    $('a.control_prev').click(function () {
        moveLeft();
    });

    $('a.control_next').click(function () {
        moveRight();
    });

});

I hope this will help you.我希望这能帮到您。 Let me know if you have any query.如果您有任何疑问,请告诉我。

Thank you.谢谢你。

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

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