jquery - 如何在 JQuery 中用动画替换 div 内容?

标签 jquery html animation

我需要用动画改变 div 的内容(只有 1 个图像元素)。如果我们可以根据方向变量来做,那就太好了。它应该像幻灯片一样工作。

var img = $("<img />").attr('src', imgUrl)
.load(function() {
    if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
        alert('broken image!');
    } else {
        $("#map").html(img);
    }
}); 

我尝试使用此解决方案,但它改变了布局并且只是淡入淡出的动画。

$('#map').fadeOut("slow", function(){
    var div = $("<div id='map'>"+img2+"</div>").hide();
    $(this).replaceWith(div);
    $('#map').fadeIn("slow");
});

只需要 JQuery 脚本解决方案。谢谢!

UPD:我们可以在淡出旧图像的同时淡入新图像吗?

最佳答案

当加载新图像时,您可以fadeOut() 旧图像和fadeIn() 新图像:

var duration = 250;
var img = $("<img />").attr('src', imgUrl).css("display", "none")
.load(function() {
    if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
        alert('broken image!');
    } else {
        $("#map").children().fadeOut(duration, function () {
            $("#map").html(img).children().fadeIn(duration);
        });
    }
}); 

关于jquery - 如何在 JQuery 中用动画替换 div 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30701349/

相关文章:

php - 为什么我的 DataTables 实现无法与 ColumnDefs 一起正常工作?

javascript - XMLHttpRequest 和进度条?

html - 在网页设计中是否应该不惜一切代价避免 <br/> 和 <hr/> ?

html - 为什么具有指定高度和宽度的图像渲染速度比没有指定高度和宽度的图像快?

html - CSS3 动画全屏

jquery-ui - 根据 id 而不是索引选择 jquery-ui-tab?

javascript - 如何克隆下拉列表中选定的元素

javascript - iPad网站图像性能和内存

css - 这个动画的关键帧代码是什么样的?

jquery盒子动画变化