javascript - 如何在更改 div 内容时添加淡入淡出效果?

标签 javascript html

目前我有以下脚本...

$(document).ready(function () {
    $('.manual').click(function () {
        $('.description').html($('#manual').html());
        $('.descriptiveImage').html($('#manualImage').html());
    })
    $('.excercise').click(function () {
        $('.description').html($('#excercise').html());
        $('.descriptiveImage').html($('#excerciseImage').html());
    })
    $('.electro').click(function () {
        $('.description').html($('#electro').html());
        $('.descriptiveImage').html($('#electroImage').html());
    })
    $('.acupuncture').click(function () {
        $('.description').html($('#acupuncture').html());
        $('.descriptiveImage').html($('#acupunctureImage').html());
    })
    $('.hydrotherapy').click(function () {
        $('.description').html($('#hydrotherapy').html());
        $('.descriptiveImage').html($('#hydrotherapyImage').html());
    })
    $('.sports').click(function () {
        $('.description').html($('#sports').html());
        $('.descriptiveImage').html($('#sportsImage').html());
    })
});

如果我用一节来解释......

$('.sports').click(function () {
    $('.description').html($('#sports').html());
    $('.descriptiveImage').html($('#sportsImage').html());
})

所以,如果我点击一个带有“sports”类的 div,它就会... 删除 div 中带有类“description”的所有内容,并替换为 id 为“sports”的 div 的内容。然后它将获取类为“descriptiveImage”的 div 并将其替换为 id 为“sportsImage”的 div 的内容。

我相信你们大多数人都会认为这是一个相当通用的替换脚本。

我想做的是更改脚本,以便 div .description 的现有内容淡出,然后新内容淡入。这可能吗?

最佳答案

使用fadeOut()完成函数参数:

$('.description').fadeOut(400, function() {
    $('.description').html($('#sports').html()).fadeIn(200);
});
$('.descriptiveImage').fadeOut(400, function() {
    $('.descriptiveImage').html($('#sportsImage').html()).fadeIn(200);
});

关于javascript - 如何在更改 div 内容时添加淡入淡出效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41596351/

相关文章:

html - 使 <div> 长度相同并出现在列中

jQuery get Select 选项值显示未定义错误

javascript - HTML 5 模板标签(页面刷新)

javascript - Discord.js | guild.iconURL 在嵌入中不起作用

javascript - 如何对字符串的每个元素应用替换?在 JavaScript 中

javascript - 与 OrbitControls 结合转换 HTML 元素,使它们看起来随着场景中的对象旋转

android - 使用 Android 模拟器和浏览器放大时页面上的元素(即 a、div、h1)移动

javascript - 在子元素上应用悬停效果

javascript - 捕获所有 `a` 点击,包括动态添加的点击

javascript - iOS jQuery 更改输入值失败, "addClass"调用失败