【发布时间】:2021-09-06 07:14:44
【问题描述】:
我有以下代码-
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
function toggleText(){
if ($("#txt-1").css("display") != "none") {
$("#txt-1").css("display", "none");
$("#txt-2").css("display", "block");
$("#txt-3").css("display", "none");
} else if ($("#txt-2").css("display") != "none") {
$("#txt-1").css("display", "none");
$("#txt-2").css("display", "none");
$("#txt-3").css("display", "block");
} else {
$("#txt-1").css("display", "block");
$("#txt-2").css("display", "none");
$("#txt-3").css("display", "none");
}
};
</script>
</head>
<body>
<button onclick="toggleText()">Toggle</button>
<p id="txt-1">Hello</p>
<p id="txt-2" style="display: none;">How are you?</p>
<p id="txt-3" style="display: none;">See you soon!</p>
</body>
</html>
我正在寻找的是在文本之间进行平滑过渡(可能是淡入淡出)。我不确定是写CSS 还是jQuery。
我尝试的是写一个CSS 类-
.smooth-fade {
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
}
并将此类名称smooth-fade 提供给所有h1 标签。它不起作用,执行此任务的最佳解决方法是什么?
【问题讨论】:
-
使用可见性和不透明度代替显示块来实现效果
-
你可以在css或jquery中完成,这里是jquery fadeOut api.jquery.com/fadeout jquery中还有一个fadeIn
标签: javascript html jquery css