Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element "flickers" and Moves on fadeOut/fadeIn

I'm using the following code to fadeOut and fadeIn content divs on a page (jsfiddle here)...

HTML:

<ul>
  <li><a id="indexNav" href="index.html">Home</a></li>
  <li><a id="aboutNav" href="about.html">About</a></li>
</ul>

<div id="indexContent">
  This is the main page content.
</div>

<div id="aboutContent">
  This is the about page content.
</div>

CSS:

ul {
  float: left;
  display: block;
  margin-top: 50px;
  margin-left: 0px;
}

ul li {
  list-style: none;
  display: block;
  margin-top: 5px;
}

ul li a {
  display: block;
  height: 20px;
  width: 50px;
  margin: 0px;
  background: #7d5900;
  color: white;
  text-decoration: none;
  text-align: center;
}

div {
  width: 300px;
  height: 200px;
  margin: auto;
  margin-top: 70px;
  color: white;
  background-color: rgba(0, 36, 125, 0.5);
}

#aboutContent {
  display: none;
}

JavaScript:

$('#indexNav').click(function() {
  $('#aboutContent').fadeOut('fast');
  $('#indexContent').fadeIn('fast');
  return false;
});

$('#aboutNav').click(function() {
  $('#indexContent').fadeOut('fast');
  $('#aboutContent').fadeIn('fast');
  return false;
});

As demonstrated in the jsfiddle (at least in Firefox 9.0.1 and IE 9 on Windows 7), when clicking back and forth between the links to show/hide the elements in question, there is a bit of a page-wide flicker as the elements animate. Basically, the div moves far down the page, which causes a scrollbar to appear and push the div a little to the left, then it returns to normal when the animation finishes.

Not being an expert in CSS by any means, I've just been sort of tinkering with this to try to achieve a simple fade-out/fade-in effect (using jQuery, naturally). So I'm wondering if there's a more correct way to do this or if I'm making some kind of fundamental mistake in my design that's just not known to me.

Any ideas on what might be causing this flicker and how to correct it?

like image 989
David Avatar asked Jan 24 '12 17:01

David


People also ask

What is the syntax of jQuery fadeOut () method?

The jQuery fadeOut() method is used to fade out a visible element. Syntax: $(selector).fadeOut(speed,callback); The optional speed parameter specifies the duration of the effect.

Which function is used to show an element with the fade effect?

The fadeOut() function is used to fade out(hide) a element in HTML.

What is the difference between fadeOut and hide in jQuery?

Main difference between FadeIn, FadeOut vs hide, Show is When you use FadeIn and fadeout Its remove line Slowly Like Opacity will 100 to 0 in a few mili-second but On other hand hide, Show will remove the line immediately Without wasting any mili-second.

What is FadeIn in jQuery?

jQuery fadeIn() Method The fadeIn() method gradually changes the opacity, for selected elements, from hidden to visible (fading effect). Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: This method is often used together with the fadeOut() method.


2 Answers

This occurs because your elements are displayed relatively. That means that when both are present on the screen, they move each other around. When you do a cross-fade, which is essentially what you are doing, they will both be on the screen for a time. You need to absolutely position the elements to occupy the same space.

#aboutContent, #indexContent {
    position: absolute;
    top: 10px;
    left: 50px;
}

Example: http://jsfiddle.net/2TDj5/

You can put the elements in a wrapper div, which will allow you to position them relative to the page, but absolute with regard to each other. Just make sure that you explicitly set the wrapper to display: relative.

Example: http://jsfiddle.net/2TDj5/1/

like image 176
Jeff B Avatar answered Nov 09 '22 06:11

Jeff B


$('#indexNav').click(function() {
    $('#aboutContent').fadeOut('fast',function(){
        $('#indexContent').fadeIn('fast');
    });
    return false;
});

$('#aboutNav').click(function() {
    $('#indexContent').fadeOut('fast',function(){
        $('#aboutContent').fadeIn('fast');
    });
    return false;
});
like image 24
Ethan Avatar answered Nov 09 '22 06:11

Ethan