最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to do showhide div with slide in Javascript (without jQuery) - Stack Overflow

programmeradmin1浏览0评论

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): /


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>

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/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>
Share Improve this question edited May 10, 2014 at 11:32 user3572805 asked May 10, 2014 at 11:30 user3572805user3572805 291 gold badge1 silver badge6 bronze badges 2
  • Its rather easy in css3 if your target browsers suppott it – Banana Commented May 10, 2014 at 11:34
  • look at css transitions developer.mozilla/en-US/docs/Web/Guide/CSS/… – Chris Moutray Commented May 10, 2014 at 11:36
Add a ment  | 

5 Answers 5

Reset to default 7

http://jsfiddle/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.

had to try it myself, not even close to Jquery but working :) http://jsfiddle/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();
   })

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

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.

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>
发布评论

评论列表(0)

  1. 暂无评论