user3572805
user3572805

Reputation: 29

how to do show/hide div with slide in Javascript (without jQuery)

how to do show/hide div with slide from left to right in Javascript (without jQuery)

this is example for what i need (in jQuery): http://jsfiddle.net/dRpWv/1/


this is the example source of what i need in jQuery:

<a href="#" id="button" class="button_style">Show content</a>
<div id="hidden_content">Content</div>

<script>
$(document).ready(function() {
    $("#button").toggle(function() {
        $(this).text('Hide Content');
    }, function() {
        $(this).text('show Content');
    }).click(function(){
        $("#hidden_content").slideToggle("slow");
    });
});
</script>

Upvotes: 1

Views: 13689

Answers (5)

saj1991
saj1991

Reputation: 19

if button with toggle class by click that button will shows the div by incresing its width...(toggle)

<script>
var $block = $( ".block" );
$block.hide();
$( "#toggle" ).on( "click", function() {
$block.stop().slideToggle(1000 );
 $( ".block" ).animate({
width: "700px",
borderWidth: "10px"
}, 1500 );


});
<script>

Upvotes: -1

roy
roy

Reputation: 605

had to try it myself, not even close to Jquery but working :) http://jsfiddle.net/6MTma/

   function slideToggle() {
       var content = document.getElementById('hidden_content');
       var style = window.getComputedStyle(content);

       style['display'] == 'none' ? slideDown(content) : slideUp(content)
   }
   var btn = document.getElementById('button');
   btn.addEventListener('click', function(){
       slideToggle();
   })

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

http://jsfiddle.net/dRpWv/665/

Here ya go. Makes use of CSS3 transition to smoothly animate the slide (much more smoothly than jQuery could ever manage, I might add).

Since display is not transitionable, it uses height instead with overflow:hidden (which is how jQuery does it internally, I think). Uses scrollHeight to determine target height.

Upvotes: 7

fjc
fjc

Reputation: 5805

Just use a normal onclick handler and store a variable with the current state (displayed/hidden):

var displayed = true;
document.getElementById("button").onclick = function() {
    displayed = !displayed; // the toggle part
    if(displayed) {
        document.getElementById("button").innerHTML = "Show content";
    } else {
        document.getElementById("button").innerHTML = "Hide content";
    }
    // perform general onclick action that's unrelated to toggle, e.g. using a CSS3 transition to slide
};

You'll find out how to slide stuff easily by using Google.

Upvotes: -1

Jelle Smeets
Jelle Smeets

Reputation: 158

You can use this to hide your element:

document.getElementById("MyElement").style.display = "none";

To show an element you can set the display to block:

 document.getElementById("MyElement").style.display = "block";

Upvotes: -1

Related Questions