Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery animation doesn't work properly on first run

this is the code: code on jsfiddle. On the first run it doesn't show the "slideDown" animation but subsequent times it works fine.

$("#more-news") .click(function() {
   $(".news-hide") .slideDown('slow').removeClass("hide");
});
like image 219
ZaitseV Avatar asked Feb 22 '16 13:02

ZaitseV


2 Answers

Use the following.

$("#more-news").click(function() {
    //changed the line below.
    $(".news-hide").hide().removeClass('hide').slideDown('slow');
    $("#less-news").fadeIn('slow').removeClass("hide");
    $("#more-news").fadeOut().addClass("hide");
});

DEMO

like image 189
rrk Avatar answered Oct 14 '22 10:10

rrk


Instead of using multiple elements and multiple events, you can use like this,

$("#more-news").click(function() {
  var button = $(this)
  $(".news-hide").slideToggle(function() {
    $(".news-hide").is(":visible") ? button.text("Less News") : button.text("More News")
  });
});

Fiddle

like image 4
Anoop Joshi P Avatar answered Oct 14 '22 09:10

Anoop Joshi P