我正在使用jquery + css更改div的背景图像,如下所示:
var slide_images = ["http://foodnetwork.sndimg.com/content/dam/images/food/fullset/2014/2/7/1/FNM_030114-Spaghetti-Carbonara-Recipe-h_s4x3.jpg", "http://www.labrasseriefirenze.it/labrasserie/wp-content/uploads/2013/12/spaghetti-carbonara.jpg"];
var slide_count = 0;
$(document).ready(function() {
setInterval(function() {
slide_count=++slide_count%slide_images.length;
$('.slide_photo').css('background-image', 'url(\''+slide_images[slide_count]+'\')');
}, 4000);
});

.slide_photo {
position: relative;
top: 30px;
width: 100%;
height: 200px;
background-image: url('http://foodnetwork.sndimg.com/content/dam/images/food/fullset/2014/2/7/1/FNM_030114-Spaghetti-Carbonara-Recipe-h_s4x3.jpg');
background-repeat: no-repeat;
background-position: center center;
background-size: 100% auto;
transition: all .2s ease;
}
.slide_photo:hover {
background-size: 110% auto;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div class="slide_photo"></div>
&#13;
问题是:是否可以添加淡入效果或转换以使更改更平滑?
答案 0 :(得分:3)
像这样的css3动画怎么样
.slide_photo{
-webkit-animation: fade 4s infinite;
-moz-animation: fade 4s infinite;
-o-animation: fade 4s infinite;
animation: fade 4s infinite;
}
.slide_photo {
position: relative;
top: 30px;
width: 100%;
height: 200px;
background-image: url('http://foodnetwork.sndimg.com/content/dam/images/food/fullset/2014/2/7/1/FNM_030114-Spaghetti-Carbonara-Recipe-h_s4x3.jpg');
background-repeat: no-repeat;
background-position: center center;
background-size: 100% auto;
transition: all .2s ease;
-webkit-animation: fade 4s infinite;
-moz-animation: fade 4s infinite;
-o-animation: fade 4s infinite;
animation: fade 4s infinite;
}
.slide_photo:hover {
background-size: 110% auto;
}
@-webkit-keyframes fade {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-moz-keyframes fade {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-o-keyframes fade {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@keyframes fade {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
var slide_images = ["http://foodnetwork.sndimg.com/content/dam/images/food/fullset/2014/2/7/1/FNM_030114-Spaghetti-Carbonara-Recipe-h_s4x3.jpg", "http://www.labrasseriefirenze.it/labrasserie/wp-content/uploads/2013/12/spaghetti-carbonara.jpg"];
var slide_count = 0;
$(document).ready(function() {
setInterval(function() {
slide_count = ++slide_count % slide_images.length;
$('.slide_photo').css('background-image', 'url(\'' + slide_images[slide_count] + '\')');
}, 4000);
});
&#13;
.slide_photo {
position: relative;
top: 30px;
width: 100%;
height: 200px;
background-image: url('http://foodnetwork.sndimg.com/content/dam/images/food/fullset/2014/2/7/1/FNM_030114-Spaghetti-Carbonara-Recipe-h_s4x3.jpg');
background-repeat: no-repeat;
background-position: center center;
background-size: 100% auto;
transition: all .2s ease;
-webkit-animation: fade 4s infinite;
-moz-animation: fade 4s infinite;
-o-animation: fade 4s infinite;
animation: fade 4s infinite;
}
.slide_photo:hover {
background-size: 110% auto;
}
@-webkit-keyframes fade {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-moz-keyframes fade {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-o-keyframes fade {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@keyframes fade {
0% {
opacity: 0;
}
50% {
opacity: 1;
}
100% {
opacity: 0;
}
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div class="slide_photo"></div>
&#13;