javascript - 使用显示属性淡入淡出

标签 javascript jquery html css

function myFunction() {
    document.getElementById("myDIV").style.display = "block";
    document.getElementById("second").style.display = "none";
}

function myFunction2() {
    document.getElementById("second").style.display ="block";
    document.getElementById("myDIV").style.display = "none";
}
#myDIV {
    width: 500px;
    height: 500px;
    background-color: lightblue;
    display: none;
}
    
#second {
    width: 500px;
    height: 500px;
    background-color: lightblue;
    display: none;
}
<button onclick="myFunction()">Try it</button>
<button onclick="myFunction2()">Try this now</button>
<div id="myDIV">
    This is my DIV element.
</div>
<div id="second">
    This is my DIV2 element.
</div>

上面的代码让两个分区在同一个位置交替出现。我怎样才能以它们出现和消失的方式操纵这些部门。更具体地说是淡入(出现)淡出(消失)。谢谢

最佳答案

使用jQuery 进行事件处理和淡入淡出 动画:

HTML:

<button id="button1">Try it</button>
<button id="button2">Try this now</button>

Javascript:

$('#button1').on('click', function () {
    $('#second').fadeOut(2000, function () {
        $('#myDIV').fadeIn(2000);
    });
});


$('#button2').on('click', function () {
    $('#myDIV').fadeOut(2000, function () {
        $('#second').fadeIn(2000);
    });
});

演示:http://jsfiddle.net/tusharj/rLvpqcLb/

关于javascript - 使用显示属性淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30796874/

相关文章:

javascript - 检查输入的密码是否匹配

javascript - 搜索表javascript

javascript - 在 Protractor/E2E 测试中访问 $http 数据 (AngularJS)

Javascript 字符串对象只读?

javascript - 将串口原始数据缓冲区转换为数字数组?

jquery - 将数组从 js (jQuery) 发送到 sql 存储过程的最佳方法是什么?阵列层间传输

javascript - 在网页中模拟 DOS 或终端屏幕的最佳方法是什么?

html - 如何使导航左对齐?

html - 将百分比值设置为 border-width

javascript - 在 JavaScript 中设置默认值的惯用方法