我想在jQuery中创建简单的背景图像滑块。一切正常,除了动画。我认为它应该像这样:
但是我得到了一些不同的感觉,我觉得首先要换照片,然后图像逐渐消失。
我尝试了delay(),但是没有起作用,并且隐藏了(),但这使动画太不自然了。
这里我在jsfiddle中创建了示例,请看一下: https://jsfiddle.net/xpvt214o/375627/
$('.background').fadeOut(1000).css('background-image',
'url('+images[whichone++]+')')
.fadeIn(1000);
我该如何更改?
$(document).ready(function(){
var images= ['https://images.pexels.com/photos/46710/pexels-photo-46710.jpeg?cs=srgb&dl=beach-sand-summer-46710.jpg&fm=jpg','https://images.pexels.com/photos/219998/pexels-photo-219998.jpeg?cs=srgb&dl=beach-birds-calm-219998.jpg&fm=jpg','https://images.pexels.com/photos/207962/pexels-photo-207962.jpeg?cs=srgb&dl=artistic-blossom-bright-207962.jpg&fm=jpg'];
var howmany = images.length;
var whichone = 0;
function slideSwitch(){
if(whichone >= howmany-1 ) { whichone = 0}
$('.background').fadeOut(1000).css('background-image', 'url('+images[whichone++]+')')
.fadeIn(1000);
}
setInterval(slideSwitch, 5000);
});
.background{
background: cover;
position:fixed;
width: 100%;
height: 100%;
left:0;
top:0;
background-image: url("https://images.pexels.com/photos/207962/pexels-photo-207962.jpeg?cs=srgb&dl=artistic-blossom-bright-207962.jpg&fm=jpg") ;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="background"></div>
<div class="content">
答案 0 :(得分:1)
尝试如下所示
添加了transition: background-image 2s;
并稍微更改了您的js。同样,您的background:cover
样式是错误的。应该是background-size: cover;
$(document).ready(function() {
var images = ['https://images.pexels.com/photos/46710/pexels-photo-46710.jpeg?cs=srgb&dl=beach-sand-summer-46710.jpg&fm=jpg', 'https://images.pexels.com/photos/219998/pexels-photo-219998.jpeg?cs=srgb&dl=beach-birds-calm-219998.jpg&fm=jpg', 'https://images.pexels.com/photos/207962/pexels-photo-207962.jpeg?cs=srgb&dl=artistic-blossom-bright-207962.jpg&fm=jpg'];
var howmany = images.length;
var whichone = 0;
function slideSwitch() {
if (whichone >= howmany - 1) {
whichone = 0
}
$('.background').css('background-image', 'url(' + images[whichone++] + ')');
}
setInterval(slideSwitch, 5000);
});
.background {
background-size: cover;
position: fixed;
width: 100%;
height: 100%;
left: 0;
top: 0;
transition: background-image 2s;
background-image: url("https://images.pexels.com/photos/207962/pexels-photo-207962.jpeg?cs=srgb&dl=artistic-blossom-bright-207962.jpg&fm=jpg");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="background"></div>
<div class="content">