按下下一个幻灯片按钮时如何使屏幕上的幻灯片动画化?



我正在处理的页面上有一个视频滑块,但我希望在更改幻灯片时出现一个漂亮的动画,而不仅仅是更改为下一张幻灯片。例如,对屏幕一侧的幻灯片和从另一侧的下一张幻灯片进行动画处理。

var slideIndex = 1;
		showDivs(slideIndex);
		function plusDivs(n) {
			showDivs(slideIndex += n);
		}
		function showDivs(n) {
			var i;
			var x = document.getElementsByClassName("mySlides");
			if (n > x.length) {slideIndex = 1}    
			if (n < 1) {slideIndex = x.length}
				for (i = 0; i < x.length; i++) {
					x[i].style.display = "none";  
				}
			x[slideIndex-1].style.display = "block";  
		}
<div class="video__container" style="display: flex; display: -webkit-flex; flex-direction: row; -webkit-flex-direction: row;">
			<div class="arrow__container">
				<div class="video__container--arrow arrow__back" onclick="plusDivs(-1)">
					<img class="img__full" src="css/images/template_arrow.svg">
				</div>
			</div>
		
			<div class="video__container--item">
				<!-- Must include data-id, data-bg and optional video name -->
				<div class="video__slide mySlides">
					<div class="youtube-container template-youtube-container">
		   				<div class="youtube-player" data-id="bqlzoxTvOkE" data-bg="css/images/pb/video1.jpg"><span class="video-name">Branding</span></div>
					</div>
				</div>
				<div class="video__slide mySlides">
					<div class="youtube-container template-youtube-container">
		   				<div class="youtube-player" data-id="S-sJp1FfG7Q" data-bg="css/images/pb/video2.jpg"><span class="video-name">UX Design</span></div>
					</div>
				</div>
				<div class="video__slide mySlides">
					<div class="youtube-container template-youtube-container">
		   				<div class="youtube-player" data-id="zVntJ21thpQ" data-bg="css/images/pb/video3.jpg"><span class="video-name">UI Design</span></div>
					</div>
				</div>
			</div>
			
			<div class="arrow__container">
				<div class="video__container--arrow arrow__next" onclick="plusDivs(1)">
					<img class="img__full" src="css/images/template_arrow.svg">
				</div>
			</div>
		</div>

好的,是的,它很多,但我们在这里所做的是避免JavaScript缓动动画。如果您确实想使用缓动,请使用以下链接,因为我无法像现在这样解释它。 https://www.kirupa.com/html5/introduction_to_easing_in_javascript.htm 如果你仍然想跳过整个缓和的学习过程(像我一样),有一种方法可以解决它。 我会给你一个简短的版本,如果你仍然感到困惑,我有一个很好的长例子。首先,您必须制作一个div 容器来容纳幻灯片。基本上,您将使用 JavaScript 函数为该容器分配一个类。您将为每张幻灯片制作两个,除了第一张和最后一张(一张用于前进,一张用于后退)。执行此操作时,请确保通过分配另一个类作为 null 来重置动画,否则...您刚刚创建的那些类将保存动画,但 null 类除外。添加您的动画、一些样式和 html,确保包含一个带有前面函数的按钮。我的真的很基本,但你可以随心所欲地装饰它。

/*This is the Javascript. What we are doing here is bypassing the complexity of
Javascript animations and instead changing the class of what we want to move.
This allows us to still use the onclick function in our html, while at 
the same time using css3 animations*/
function magicbtnf() {
document.getElementById('container').className = "magicslide";
}
function magicbtnb() {
document.getElementById('container').className = "magicslide2";
}
/*This is css. Most of it is styling, but I will point out importants in 
comments.*/
body,
html {
width: 100%;
margin: 0;
border: 0;
font-family: "century gothic", "Arial";
font-size: 18px;
}
#container {
/*This is the container, make it 100% x the number of slides you have.
	This will allow each slide to fill 100% of the screen*/
width: 200%;
overflow: hidden;
position: fixed;
display: flex;
}
#container>div {
display: inline-block;
text-align: center;
vertical-align: top;
}
#slide1 {
/* You can insert your first slide CSS in here , just make sure the width
	is 1/the amount of slides you have. % is recommended, but I'm sure there
	is a way to use vw.*/
height: 400px;
width: 50%;
background-color: red;
float: right;
position: relative;
}
.magicslide {
/*This is one of two classes for the animation. We will use javascript to
	set and unset the classes to the slides*/
-webkit-animation: switch 4s 1;
-o-animation: switch 4s 1;
-moz-animation: switch 4s 1;
animation: switch 4s 1;
animation-fill-mode: forwards;
-webkit-animation-fill-mode: forwards;
-moz-animation-fill-mode: forwards;
-o-animation-fill-mode: forwards;
}
@-moz-keyframes switch {
0% {
margin-left: 0px;
}
25% {
margin-left: 0px;
}
50% {
margin-left: -60%;
}
100% {
margin-left: -100%;
}
}
@-o-keyframes switch {
0% {
margin-left: 0px;
}
25% {
margin-left: 0px;
}
50% {
margin-left: -60%;
}
100% {
margin-left: -100%;
}
}
@-webkit-keyframes switch {
0% {
margin-left: 0px;
}
25% {
margin-left: 0px
}
50% {
margin-left: -60%;
}
100% {
margin-left: -100%;
}
}
@keyframes switch {
0% {
margin-left: 0px;
}
25% {
margin-left: 0px
}
50% {
margin-left: -60%;
}
100% {
margin-left: -100%;
}
}
.notsomagic {
/*This is basically a void element. This resets the animation so when
	you go back a slide, you can continue again.*/
height: 400px;
width: 100%;
}
#slide2 {
/*Same thing here as the earlier slide!*/
height: 400px;
width: 50%;
background-color: lightblue;
float: left;
position: relative;
}
.magicslide2 {
/*This is the same animation sequence as before, but backwards*/
-webkit-animation: switchb 4s 1;
-o-animation: switchb 4s 1;
-moz-animation: switchb 4s 1;
animation: switchb 4s 1;
animation-fill-mode: forwards;
-webkit-animation-fill-mode: forwards;
-moz-animation-fill-mode: forwards;
-o-animation-fill-mode: forwards;
}
@keyframes switchb {
0% {
margin-left: -100%;
}
25% {
margin-left: -100%;
}
50% {
margin-left: -40%
}
100% {
margin-left: 0%;
}
}
@-moz-keyframes switchb {
0% {
margin-left: -100%;
}
25% {
margin-left: -100%;
}
50% {
margin-left: -40%
}
100% {
margin-left: 0%;
}
}
@-o-keyframes switchb {
0% {
margin-left: -100%;
}
25% {
margin-left: -100%;
}
50% {
margin-left: -40%
}
100% {
margin-left: 0%;
}
}
@-webkit-keyframes switchb {
0% {
margin-left: -100%;
}
25% {
margin-left: -100%;
}
50% {
margin-left: -40%
}
100% {
margin-left: 0%;
}
}
<!--This is just some boring old html. It's esentially placing everything where it needs to go. The most noticable part is the input, which states our function from the javascript onclick -->
<!DOCTYPE html>
<html>
<head>
<title>Slide</title>
<link rel="stylesheet" type="text/css" href="change_slides.css">
<script type="text/javascript" src="change_slides.js"></script>
</head>
<body>
<div id="container">
<div id="slide1">
Hello! This is my slideshow!
<br>
<input type="button" id="change" value="Click Me!" onclick="magicbtnf()">
</div>
<div id="slide2">
This is slide #2!
<br>
<input type="button" id="change2" value="Click Me Too!" onclick="magicbtnb()">
</div>
</div>
</body>
</html>

如果您不想制作自己的动画,可以免费下载。我推荐Animate.css。它充满了非常流畅的动画,通过一些快速编辑,您可以使它们与您的幻灯片很好地配合使用。链接是:https://daneden.github.io/animate.css/我希望这有帮助!:D

最新更新