/    Sign up×
Community /Pin to ProfileBookmark

Weird behaviour using animations and removeClass/addClass in jQuery

I’m trying to create a fancy little animation when opening and closing a search field underneath a nav bar. The nav bar is either black or white and the search area always the opposite. But once the search area is closed I want the nav bar to change it’s color to the opposite color. During the closing of the search area I display an overlay over the nav bar that grows in height, so it looks as if the search are area slides into and becomes the nav bar. Once the overlay has the size of the nav bar the nav bar color changes and the overlay fades out to make the inverted nav bar visible again. You can check it out right here [url]http://xweasel.chris-cole.de/v3/[/url] and here [url]http://xweasel.chris-cole.de/v3/search.php?Description=meg+test[/url]

Here are some screenshots:

[url]https://i.stack.imgur.com/RTdED.jpg[/url]

So the problem seems to be, that the color change of the nav bar sometimes happens once (as intended) and sometimes twice for some reason (addClass and removeClass .inverted) which makes the nav bar show in the wrong color. This happens (at least to me) totally randomly. Sometimes it works 5 times, sometimes I get the wrong color on the second try.

This is my first post and English is not my mother language, so pleas have mercy on me 😉 Thanks for your help!

[CODE]// Quick Search – Click
// ————————–

$(‘#search-icon’).click(function() {

if($(‘#search-bar’).hasClass(‘active’)) // Close the Search Bar
{
$(‘#search-bar-inner’).fadeOut( 500, function() {
// Animation complete.

var bottompos = $(document).height() – $(‘#nav-bar’).height();
$(‘#nav-overlay’).css(“bottom”, bottompos + “px”);

if($(‘#nav-bar’).hasClass(‘inverted’)) // Nav Bar is black and search bar is open (index page)
{
$(‘#nav-overlay’).css(“background-color”, “#fff”);
}
else // Nav Bar is white and search bar is open (search page)
{
$(‘#nav-overlay’).css(“background-color”, “#333”);
}

$(‘#nav-overlay’).css(“display”, “block”);

$(‘#search-bar’).slideUp(500, function() {
// Animation complete
$(‘#search-bar’).removeClass(‘active’);
$(‘#search-icon’).removeClass(‘active’);
});

$(‘#nav-overlay’).animate({
height: 60 + “px”
}, 500, function() {
// Animation complete.
if($(‘#nav-bar’).hasClass(‘inverted’)) // Nav Bar is black
{
$(‘#nav-bar’).removeClass(‘inverted’);
}
else // Nav Bar is white
{
$(‘#nav-bar’).addClass(‘inverted’);
}

$(‘#nav-overlay’).animate({
opacity: 0
}, 300, function() {
// Animation complete.
$(‘#nav-overlay’).css(“display”, “none”);
$(‘#nav-overlay’).css(“opacity”, 1);
$(‘#nav-overlay’).css(“height”, 0);
});
});

});
}
else // Open the Search Bar
{
$(‘#search-icon’).addClass(‘active’);

if($(‘#nav-bar’).hasClass(‘inverted’)) // Nav Bar is black and Search Bar is closed (search page)
{
$(‘#nav-overlay2’).css(“background-color”, “#fff”);
}
else // Nav Bar is white and Search Bar is closed (index page)
{
$(‘#nav-overlay2’).css(“background-color”, “#333”);
}

$(‘#nav-overlay2’).css(“display”, “block”);

$(‘#search-bar’).slideDown( 500, function() {
// Animation complete.
$(‘#search-bar-inner’).fadeIn(500, function() {
// Animation complete
$(‘#search-input’).focus();
});
});

$(‘#nav-overlay2’).animate({
height: 60 + “px”
}, 500, function() {
// Animation complete.

if($(‘#nav-bar’).hasClass(‘inverted’)) // Nav Bar is black
{
$(‘#nav-bar’).removeClass(‘inverted’);
$(‘#site-wrapper’).append(‘<br>white’);
}
else // Nav Bar is white
{
$(‘#nav-bar’).addClass(‘inverted’);
$(‘#site-wrapper’).append(‘<br>black’);
}

$(‘#nav-overlay2’).animate({
opacity: 0
}, 300, function() {
// Animation complete.
$(‘#nav-overlay2’).css(“display”, “none”);
$(‘#nav-overlay2’).css(“opacity”, 1);
$(‘#nav-overlay2’).css(“height”, 0);
$(‘#search-bar’).addClass(‘active’);
});
});
}

});[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@azalo_gabonJun 01.2017 — this forum is dead, go post on stackoverflow if you want help
Copy linkTweet thisAlerts:
@ZorgJun 02.2017 — Your code seems to work when i tested it locally, although its not working from the links you posted, maybe try moving all your js code to just before the closing </body> tag
×

Success!

Help @ChrisLee spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...