1

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>
2

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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();
   })

Comments

-1

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";

1 Comment

how to do show/hide div with slide from left to right display: block/none simply shows/hides the element. \
-1

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.

Comments

-1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.