使用显示属性淡入和淡出
Fade in and out using display property
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
用于事件处理和fade
动画:
.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/
你可以用 css3 做,不需要 jquery,而不是显示 none 使用不透明度 0
<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {
width: 500px;
height: 500px;
background-color: lightblue;
opacity: 0;
transition: all 2s linear;
}
#second {
width: 500px;
height: 500px;
background-color: lightblue;
display: none;
}
</style>
</head>
<body>
<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>
<script>
function myFunction() {
document.getElementById("myDIV").style.opacity = "1";
document.getElementById("second").style.display = "none";
}
</script>
相关文章:
- Javascript将图像src更改为淡入淡出
- css(或jQuery)悬停时淡入淡出
- 将淡入淡出添加到“我的身体背景滑块”
- 多重潜水淡入淡出打开
- 添加/删除类淡入淡出不起作用
- 带有计数器、缩略图、进度条和淡入淡出效果的简单jQuery幻灯片
- 使用jquery淡入淡出
- 淡入淡出脚本不起作用
- 具有淡入淡出效果的全背景图像
- 纯 JavaScript 淡入淡出 - 淡入不起作用
- 帧淡出/淡入
- 如何在onClick事件中使用ReactJS淡出/淡入内容
- 淡出 ->淡入无法正常工作
- JQuery 淡出/淡入以创建排序列表
- 无法让元素淡出/淡入
- 正在添加'淡出/淡入'响应媒体查询
- jQuery:具有淡入和淡出(淡入)的图像
- Div won't淡出/淡入
- 淡出/淡入和上卷jquery效果
- 如何根据 SELECT 更改淡出/淡入视图