如何逐渐淡出和淡入div

时间:2015-10-07 15:31:42

标签: javascript jquery css

我希望按顺序淡出所有元素,然后使用新数据淡入所有元素。

我只做了50%的目标,因为只有fadeIn按顺序发生,淡出同时发生在所有div上。

我想知道我能做错什么,以便按顺序发生元素淡出。

虽然如何解决这个问题?

的CSS:

.class4 {
    width: 24%;
    height: 100px;
    float:left;
    box-shadow: 0px 2px 3px 0px rgba(174,174,174,0.50);
    background-image: url('http://assets.servedby-buysellads.com/p/manage/asset/id/25126');
    background-position: 50% 6%;
    background-size: 90%;
    background-repeat: no-repeat;


}
.class3 {
    width: 24%;
    height: 100px;
    float:left;
    box-shadow: 0px 2px 3px 0px rgba(174,174,174,0.50);
    background-image: url('http://assets.servedby-buysellads.com/p/manage/asset/id/25126');
    background-position: 50% 6%;
    background-size: 90%;
    background-repeat: no-repeat;

}
.class2 {
    width: 24%;
    height: 100px;
    float:left;
    box-shadow: 0px 2px 3px 0px rgba(174,174,174,0.50);
    background-image: url('http://assets.servedby-buysellads.com/p/manage/asset/id/25126');
    background-position: 50% 6%;
    background-size: 90%;
    background-repeat: no-repeat;

}
.class1 {
    width: 24%;
    height: 100px;
    float:left;
    box-shadow: 0px 2px 3px 0px rgba(174,174,174,0.50);
    background-image: url('http://assets.servedby-buysellads.com/p/manage/asset/id/25126');
    background-position: 50% 6%;
    background-size: 90%;
    background-repeat: no-repeat;
    border-bottom: 1px solid lightgray;
    border-left: 1px solid lightgray;
    border-right: 1px solid lightgray;
}

Jquery的:

$(document).ready(function() { 
    $('#btn').on('click', function(){
 var loadTime = 500;
            $('.class1, .class2, .class3, .class4').each(function (fadeInDiv) {
                //$(this).delay(fadeInDiv * 500).fadeIn(1000);
                $(this).fadeOut(loadTime, function () {
                    $(this).css('background-image', 'url("https://assets.servedby-buysellads.com/p/manage/asset/id/15119")');
                    $(this).fadeIn(loadTime);
                    loadTime += 500;
                });
            });
        });
});

HTML:

<input id = "btn" type="button" ><br><br>
<div class="class1"></div>
<div class="class2"></div>
<div class="class3"></div>
<div class="class4"></div>

JSFiddle:http://jsfiddle.net/xudaR/61/

1 个答案:

答案 0 :(得分:3)

$(document).ready(function() { 
    $('#btn').on('click', function(){
    var loadTime = 500;
    $('.class1, .class2, .class3, .class4').each(function (fadeInDiv) {
        
            $(this).fadeOut(loadTime, function(){
                $(this).css('background-image', 'url("https://assets.servedby-buysellads.com/p/manage/asset/id/15119")');
                
            }).data('delay', fadeInDiv * 300);
        
			$(this).fadeIn(loadTime);
             loadTime += 500;
            });
        
    });
    
});
.class4 {
  width: 24%;
  height: 100px;
  float: left;
  box-shadow: 0px 2px 3px 0px rgba(174, 174, 174, 0.50);
  background-image: url('http://assets.servedby-buysellads.com/p/manage/asset/id/25126');
  background-position: 50% 6%;
  background-size: 90%;
  background-repeat: no-repeat;
}
.class3 {
  width: 24%;
  height: 100px;
  float: left;
  box-shadow: 0px 2px 3px 0px rgba(174, 174, 174, 0.50);
  background-image: url('http://assets.servedby-buysellads.com/p/manage/asset/id/25126');
  background-position: 50% 6%;
  background-size: 90%;
  background-repeat: no-repeat;
}
.class2 {
  width: 24%;
  height: 100px;
  float: left;
  box-shadow: 0px 2px 3px 0px rgba(174, 174, 174, 0.50);
  background-image: url('http://assets.servedby-buysellads.com/p/manage/asset/id/25126');
  background-position: 50% 6%;
  background-size: 90%;
  background-repeat: no-repeat;
}
.class1 {
  width: 24%;
  height: 100px;
  float: left;
  box-shadow: 0px 2px 3px 0px rgba(174, 174, 174, 0.50);
  background-image: url('http://assets.servedby-buysellads.com/p/manage/asset/id/25126');
  background-position: 50% 6%;
  background-size: 90%;
  background-repeat: no-repeat;
  border-bottom: 1px solid lightgray;
  border-left: 1px solid lightgray;
  border-right: 1px solid lightgray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<input id="btn" type="button">
<br>
<br>
<div class="class1"></div>
<div class="class2"></div>
<div class="class3"></div>
<div class="class4"></div>

如果这是您想要的,请告诉我:code

http://jsfiddle.net/xudaR/66/