mig*_*ald 1 html javascript css jquery slideshow
我有一个 CSS + Javascripts 幻灯片,取自W3Schools 网站示例。
这是适合我的网络应用程序的代码(几乎相同):
$(document).ready(function() {
var slideIndex = 1;
function showSlides(n) {
var slides = $(".mySlides");
var i;
if (n > slides.length) {
slideIndex = 1;
}
if (n < 1) {
slideIndex = slides.length;
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex - 1].style.display = "block";
}
function plusSlides(n) {
showSlides(slideIndex += n);
}
showSlides(slideIndex);
$(".prev").click(function() {
plusSlides(-1);
});
$(".next").click(function() {
plusSlides(1);
});
});Run Code Online (Sandbox Code Playgroud)
@-webkit-keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
@keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
.slideshow-container {
position: relative;
margin: auto;
}
.prev,
.next {
cursor: pointer;
position: absolute;
top: 0;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 1.6s ease;
border-radius: 0 3px 3px 0;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img class="img-responsive" src="pic1.jpg" alt='Entrada'>
<div class="text">1</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img class="img-responsive" src="pic2.jpg">
<div class="text">2</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img class="img-responsive" src="pic3.jpg">
<div class="text">3</div>
</div>
<a class="prev">❮</a>
<a class="next">❯</a>
</div>Run Code Online (Sandbox Code Playgroud)
页面加载正确,但图片在一秒钟左右后消失。
我该如何修复它?
不久前我也遇到过同样的问题。我已经通过添加解决了它animation-fill-mode: forwards;。
通常,动画运行并停止,然后返回到原始样式。
有了animation-fill-mode你就可以改变这种行为。forwards将在动画结束后保持动画的结果。
请参阅 CSS 代码片段底部的实现。
$(document).ready(function() {
var slideIndex = 1;
function showSlides(n) {
var slides = $(".mySlides");
var i;
if (n > slides.length) {
slideIndex = 1;
}
if (n < 1) {
slideIndex = slides.length;
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex - 1].style.display = "block";
}
function plusSlides(n) {
showSlides(slideIndex += n);
}
showSlides(slideIndex);
$(".prev").click(function() {
plusSlides(-1);
});
$(".next").click(function() {
plusSlides(1);
});
});Run Code Online (Sandbox Code Playgroud)
@-webkit-keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
@keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
.slideshow-container {
position: relative;
margin: auto;
}
.prev,
.next {
cursor: pointer;
position: absolute;
top: 0;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 1.6s ease;
border-radius: 0 3px 3px 0;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
-webkit-animation-fill-mode: forwards;
animation-name: fade;
animation-duration: 1.5s;
animation-fill-mode: forwards;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img class="img-responsive" src="http://placehold.it/350x150" alt='Entrada'>
<div class="text">1</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img class="img-responsive" src="http://placehold.it/350x150">
<div class="text">2</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img class="img-responsive" src="http://placehold.it/350x150">
<div class="text">3</div>
</div>
<a class="prev">❮</a>
<a class="next">❯</a>
</div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2457 次 |
| 最近记录: |