Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element jumps when using jQuery slideToggle

So, first #metext_hiddentext is hidden until you press #btn_more_metext and then height: 50% will be overwritten by height: auto.

jquery:

<script type="text/javascript">
    $( document ).ready(function() {
        $('#metext_hiddentext').hide();
        $('#btn_more_metext').click(function(){
            $('#me').css( "height", "auto" );
            $('#metext_hiddentext').slideToggle('slow');
        });
    });
</script>

html:

<div id="me">
    <div id="me_content">
     <div id="meimg"></div>
     <div class="metext">
      <h1>I'm Lazor Zombie</h1>
      <h2>Lorem ipsum dolor sit amet.</h2>
      <div id="metext_hiddentext">
      <h2>Lorem ipsum dolor sit amet.</h2>
     </div>
     <div id="btn_more_metext">...</div>
    </div>
</div>

css:

#me {
    background: #ff8400;
    height: 50%;
}


#btn_more_metext { 
    font: 20px/10px "Rosario", 'Ubuntu', sans-serif;
    cursor: pointer;
    width: 80px;
    height: 35px;
    line-height: 22px;

}
like image 299
Jaakko Uusitalo Avatar asked Dec 06 '13 10:12

Jaakko Uusitalo


People also ask

What does slideToggle do in jQuery?

The slideToggle() method toggles between slideUp() and slideDown() for the selected elements. This method checks the selected elements for visibility. slideDown() is run if an element is hidden. slideUp() is run if an element is visible - This creates a toggle effect.

How do I toggle between show and hide in jQuery?

The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.

How do I toggle an event in jQuery?

The toggle() method attaches two or more functions to toggle between for the click event for the selected elements. When clicking on an element, the first specified function fires, when clicking again, the second function fires, and so on. Note: There is also a jQuery Effects method called toggle().


2 Answers

Give padding:0 and margin:0 or Add custom margin and padding to <h2> because they have default margins and that is causing jump behavior in your slideToggle().

h2{
   margin:0;
   padding:0;
}

Here is the demo having <h2> tag with custom margin and padding

like image 141
Dhaval Marthak Avatar answered Oct 14 '22 17:10

Dhaval Marthak


I think the problem here is, that when div is animated, it has overflow:hidden; And the h2 has some margin on it... so try to remove margin form h2 and add it on the div that is toggeld:

here the fiddle: http://jsfiddle.net/qk8nq/

#me {
    background: #ff8400;
    height: 50%;
}
#metext_hiddentext {
    margin:1em 0;
}

#btn_more_metext { 
    font: 20px/10px "Rosario", 'Ubuntu', sans-serif;
    cursor: pointer;
    width: 80px;
    height: 35px;
    line-height: 22px;

}
h2 {
    padding:0;
    margin:0;
}
like image 32
reyaner Avatar answered Oct 14 '22 17:10

reyaner