JavaScript开关中的幻灯片动画不一致

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript开关中的幻灯片动画不一致相关的知识,希望对你有一定的参考价值。

我试图在javascript switch语句中实现左/右滑动动画,并且动画(左右滑动而没有反弹效果且图像之间没有空格)不能始终如一地激活。此外,当在第一张幻灯片上单击上一个按钮并在最后一张幻灯片上单击下一个按钮时,幻灯片动画仍会激活。这不应该发生。有人有想法吗?请参阅代码示例。

$(function() {
	// USER EDITABLE CONTROLS
	var content = 'img'; // accepts any DOM element - div, img, table, etc...
	var showControls = true; // true/false shows/hides the slider's navigational controls
	var transition = 'slide'; // supports default, fade, slide
	var transitionDuration = .5; // adjust the time of the transition measured in seconds
	
	// VARIABLE DECLARATIONS
	var contentType = $(content);
	var $el = $('#showcase');
	var $leftArrow = '#left_arrow';
	var $rightArrow = '#right_arrow';
	var $load = $el.find(contentType)[0];
	var slideCount = $el.children().length;
	var slideNum = 1;
	
	// PRELOADS SLIDE WITH CORRECT SETTINGS
	$load.className = 'active';
	
	// ADD SLIDER CONTROLS TO PAGE
	if (showControls === true) {
		$('<div id="controls"><a href="#" id="' + $leftArrow.replace('#', '') + '">&laquo; Previous</a> <a href="#" id="' + $rightArrow.replace('#', '') + '">Next &raquo;</a></div>').insertAfter('#showcase');
		$('#controls').find('#left_arrow').addClass('disabled');
	}
	
	// LOGIC FOR SLIDE TRANSITIONS
	function transitions() {
		switch (transition) {
			// FADE TRANSITION
			case 'fade':
				$('.slide').stop().animate({opacity : 0}, transitionDuration*300, function(){
					$('.active').stop().animate({opacity : 1}, transitionDuration*1000);
				});
				break;

			// SLIDE TRANSITION
			case 'slide':
				if (slideNum > 1) {
					$('.slide').stop().animate({left : -160}, transitionDuration*800, function(){
						$('.active').stop().animate({left : 0}, transitionDuration*1000);
					});
				}
				
				if (slideNum < slideCount) {
					$('.slide').stop().animate({left : 160}, transitionDuration*800, function(){
						$('.active').stop().animate({left : 0}, transitionDuration*1000);
					});
				}
				break;

			// DEFAULT TRANSITION
			case 'default':
				break;
		}
	}
	
	// CHECKS FOR FIRST AND LAST INDEX IN THE SLIDER
	function checkSlide() {
		if (slideNum == 1) {
			$($leftArrow).addClass('disabled');
		} else {
			$($leftArrow).removeClass('disabled');
		}
		
		if (slideNum == slideCount) {
			$($rightArrow).addClass('disabled');
		} else {
			$($rightArrow).removeClass('disabled');
		}
	}

	// NAVIGATIONAL LOGIC FOR PREVIOUS/NEXT BUTTONS
	$(document).on('click', $leftArrow, function() {
		if (slideNum > 1) {
			var counter = $('.active').index();
      counter--;
			$('.active').addClass('slide');
			$('.active').removeClass('active');
			transitions();
			$el.find(contentType).eq(counter).addClass('active');
			slideNum--;
			checkSlide();
		}
	})
	
	$(document).on('click', $rightArrow, function() {
		if (slideNum < slideCount) {
			var counter = $('.active').index();
      counter++;
			$('.active').addClass('slide');
			$('.active').removeClass('active');
			transitions();
			$el.find(contentType).eq(counter).addClass('active');
			slideNum++;
			checkSlide();
		}
	})
});
#showcase {
	width: 160px;
	overflow: hidden;
}

img {
	width: 160px;
}

a {
	color: blue;
}

.disabled {
	color: red !important;
}

.slide {
	display: none;
	opacity: 0;
	position: relative;
	left: 0px;
	right: 0px;
}

.active {
	display: block;
	opacity: 1;
	position: relative;
	left: 0px;
	right: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="showcase">
	<img class="slide" src="https://picsum.photos/458/354" />
	<img class="slide" src="https://picsum.photos/458/354/?image=306" />
	<img class="slide" src="https://picsum.photos/458/354/?image=626" />
</div>
答案

如评论中所述,您需要修复条件语句。前一段时间你已经设置了两个click处理程序 - 其中一个绑定(并且无论条件如何都被触发),当另一个处理程序被触发时,这导致幻灯片动画在第一张幻灯片上单击上一个按钮时仍然激活在最后一张幻灯片问题上单击了下一个按钮。 至于动画,请参阅下面的代码。我把你的条件搞得一团糟。单击上一个时,幻灯片将从左向右移动。单击下一步时,幻灯片将从右向左移动。我使用一个标志来确定它将进行的移动 - 请参阅转换函数的新参数

$(function() {
  // USER EDITABLE CONTROLS
  var content = 'img'; // accepts any DOM element - div, img, table, etc...
  var showControls = true; // true/false shows/hides the slider's navigational controls
  var transition = 'slide'; // supports default, fade, slide
  var transitionDuration = .5; // adjust the time of the transition measured in seconds

  // VARIABLE DECLARATIONS
  var contentType = $(content);
  var $el = $('#showcase');
  var $leftArrow = '#left_arrow';
  var $rightArrow = '#right_arrow';
  var $load = $el.find(contentType)[0];
  var slideCount = $el.children().length;
  var slideNum = 1;

  // PRELOADS SLIDE WITH CORRECT SETTINGS
  $load.className = 'active';

  // ADD SLIDER CONTROLS TO PAGE
  if (showControls === true) {
    $('<div id="controls"><a href="#" id="' + $leftArrow.replace('#', '') + '">&laquo; Previous</a> <a href="#" id="' + $rightArrow.replace('#', '') + '">Next &raquo;</a></div>').insertAfter('#showcase');
    $('#controls').find('#left_arrow').addClass('disabled');
  }

  // LOGIC FOR SLIDE TRANSITIONS
  function transitions(impl = null) {
    switch (transition) {
      // FADE TRANSITION
      case 'fade':
        $('.slide').stop().animate({
          opacity: 0
        }, transitionDuration * 300, function() {
          $('.active').stop().animate({
            opacity: 1
          }, transitionDuration * 1000);
        });
        break;

        // SLIDE TRANSITION
      case 'slide':
        if (impl == "next") {
          $('.slide').css("left", '160px');
          $('.slide').stop().animate({
            left: 160
          }, transitionDuration * 800, function() {
            $('.active').stop().animate({
              left: 0
            }, transitionDuration * 1000);
          });
        } else if (impl == "prev") {
          $('.slide').css("left", '-160px');
          $('.slide').stop().animate({
            left: -160
          }, transitionDuration * 800, function() {
            $('.active').stop().animate({
              left: 0
            }, transitionDuration * 1000);
          });
        }
        break;

        // DEFAULT TRANSITION
      case 'default':
        break;
    }
  }

  // CHECKS FOR FIRST AND LAST INDEX IN THE SLIDER
  function checkSlide() {
    if (slideNum == 1) {
      $($leftArrow).addClass('disabled');
    } else {
      $($leftArrow).removeClass('disabled');
    }

    if (slideNum == slideCount) {
      $($rightArrow).addClass('disabled');
    } else {
      $($rightArrow).removeClass('disabled');
    }
  }

  // NAVIGATIONAL LOGIC FOR PREVIOUS/NEXT BUTTONS
  $(document).on('click', $leftArrow, function() {
    if (slideNum > 1) {
      var counter = $('.active').index();
      counter--;
      $('.active').addClass('slide');
      $('.active').removeClass('active');
      transitions('prev');
      $el.find(contentType).eq(counter).addClass('active');
      slideNum--;
      checkSlide();
    }
  })

  $(document).on('click', $rightArrow, function() {
    if (slideNum < slideCount) {
      var counter = $('.active').index();
      counter++;
      $('.active').addClass('slide');
      $('.active').removeClass('active');
      transitions('next');
      $el.find(contentType).eq(counter).addClass('active');
      slideNum++;
      checkSlide();
    }
  })
});
#showcase {
  width: 160px;
  overflow: hidden;
}

img {
  width: 160px;
}

a {
  color: blue;
}

.disabled {
  color: red !important;
}

.slide {
  display: none;
  opacity: 0;
  position: relative;
  left: 0px;
  right: 0px;
}

.active {
  display: block;
  opacity: 1;
  position: relative;
  left: 0px;
  right: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="showcase">
  <img class="slide" src="https://picsum.photos/458/354" />
  <img class="slide" style="left: 160px;" src="https://picsum.photos/458/354/?image=306" />
  <img class="slide" style="left: 160px;" src="https://picsum.photos/458/354/?image=626" />
</div>

以上是关于JavaScript开关中的幻灯片动画不一致的主要内容,如果未能解决你的问题,请参考以下文章

如何设定office ppt自动播放?

重新渲染 HTML 元素定位在 Javascript 幻灯片动画内的窗口调整大小

通过 JavaScript 分配 CSS 转换时不起作用

动画中的 UIView 幻灯片

使用 React.js 为自定义轮播设置动画

路由器导航幻灯片动画不起作用