是否可以平滑 JavaScript 中的文本更改

我正在使用 JavaScript 和 jQuery 开发内容更改点击功能,它可以工作但很笨重。有没有办法在 JS 或 jQuery 中使这种过渡更顺畅?


$(document).ready(function() {

  $("h1").click(function() {

    if (document.getElementById("frst_hd").innerHTML === "World") {

      document.getElementById("frst_hd").innerHTML = "Hello";

    } else {

      document.getElementById("frst_hd").innerHTML = "World";

    }

  });

});

<!DOCTYPE html>

<html>


<head>

  <title>Example</title>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>


<body>

  <h1 id="frst_hd">Hello</h1>

</body>


</html>

我是这门语言的新手,所以如果这个问题很容易解决,我很抱歉。感谢您的帮助



守着一只汪
浏览 148回答 1
1回答

喵喵时光机

$(document).ready(function() {&nbsp; $("h1").click(function() {&nbsp; &nbsp; if (document.getElementById("frst_hd").innerHTML === "World") {&nbsp; &nbsp; &nbsp; $("#frst_hd").hide().text("Hello").fadeIn(1000);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; $("#frst_hd").hide().text("World").fadeIn(1000);&nbsp; &nbsp; }&nbsp; });});<!DOCTYPE html><html><head>&nbsp; <title>Example</title>&nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script></head><body>&nbsp; <h1 id="frst_hd">Hello</h1></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript