簡體   English   中英

jquery 動畫在第一次點擊時不起作用

[英]jquery animate not working on first click

我正在嘗試使用 animation 實現閱讀更多按鈕。 Animation 在第一次單擊后工作正常,但在第一次單擊時,似乎沒有考慮 animation 方法。

以下代碼的JSFiddle鏈接。

這是我的代碼:

 var text = $('.content') const originalHeight = text.height(); $(".show-more a").on("click", function() { var $this = $(this); var $content = $this.parent().prev("div.content"); var linkText = $this.text().toUpperCase(); var fullHeight = text[0].scrollHeight; if (linkText === "SHOW MORE") { linkText = "Show less"; $content.addClass('showContent').removeClass('hideContent'); text.animate({ 'height': fullHeight }); } else { linkText = "Show more"; $content.addClass('hideContent').removeClass('showContent'); text.animate({ 'height': originalHeight }); }; $this.text(linkText); });
 div.text-container { margin: 0 auto; width: 75%; }.hideContent { overflow: hidden; line-height: 1em; height: 2em; }.showContent { line-height: 1em; height: auto; }.content { text-align: justify; }.show-more { text-align: center; display: inline; float: right; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="text-container"> <div class="content hideContent"> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. </div> <div class="show-more"> <a href="#">Show more</a> </div> </div>

嘗試這個:

 var text = $('.content') const originalHeight = text.height(); //====================== //add this line text.css("height",originalHeight ); //====================== $(".show-more a").on("click", function() { var $this = $(this); var $content = $this.parent().prev("div.content"); var linkText = $this.text().toUpperCase(); var fullHeight = text[0].scrollHeight; if (linkText === "SHOW MORE") { linkText = "Show less"; $content.addClass('showContent').removeClass('hideContent'); text.animate({ 'height': fullHeight }); } else { linkText = "Show more"; $content.addClass('hideContent').removeClass('showContent'); text.animate({ 'height': originalHeight }); }; $this.text(linkText); });
 div.text-container { margin: 0 auto; width: 75%; transition: all 0.15s ease-out; }.hideContent { overflow: hidden; line-height: 1em; height: 2em; }.showContent { line-height: 1em; height: auto; }.content { text-align: justify; }.show-more { text-align: center; display: inline; float: right; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="text-container"> <div class="content hideContent"> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. </div> <div class="show-more"> <a href="#">Show more</a> </div> </div>

Javascript ===是區分大小寫的比較。 您的代碼是if (linkText === "SHOW MORE")您的主要文本是Show more 就這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM