简体   繁体   中英

jQuery .animate function won't work

This is the HTML:

<h1 id="dictionaryTitle">Intrebari frecvente</h1>
<p class="question">QUESTION1</p>
<p class="answer">ANSWER1</p>
<p class="question">QUESTION2</p>
<p class="answer">ANSWER2</p>

This is the JavaScript/jQuery:

    $(document).ready(function() {
  $('.question').on("click", function() {
    if ($(this).next('.answer').css('display') === 'none') {
      $(this).next('.answer').animate({ "display": "block" }, 1000 });
    }
  });
});

The problem in that on click the function does not work - the display of the .answer p does not change.

Any possible solutions?

there is no option like display in the animate() function because there are no steps between display: block and display: none . transitions can only be possible with numeric values. take a look at the jquery docs http://api.jquery.com/animate/

use this:

$(document).ready(function() {
  $('.question').on("click", function() {
    var $answer = $(this).next('.answer');
    if(!$answer.is(':visible')) {
      $answer.fadeIn(1000);
    }
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM