简体   繁体   中英

toggleClass to reveal hidden div - add animation and window scroll?

I have created a button to toggle the visibility of a certain div found below it. Is there a way to add a animation to the div between transitions of hidden and show? Also, is there a way to get the window to scroll down to the top of the, now shown, div; but only when showing, not when hiding.

Example

http://codepen.io/john84/pen/MKrGWV

HTML code

<a href="#section-contact-print" class="btn btn-primary btn-lg formtoggle">Log In</a>

<section id="section-contact-print" class="hidden">
    <form role="form">

        <div class="form-group">
            <label for="email">Email address:</label>
            <input type="email" class="form-control" id="email">
        </div>

        <div class="form-group">
            <label for="pwd">Password:</label>
            <input type="password" class="form-control" id="pwd">
        </div>

        <div class="checkbox">
            <label><input type="checkbox"> Remember me</label>
        </div>

        <button type="submit" class="btn btn-default">Submit</button>

    </form>
</section>

JS (on top of the jQuery library)

$('.formtoggle').click(function (event) {
    event.preventDefault();

    var target = $(this).attr('href');
    $(target).toggleClass('hidden show');
});

Here you have a code I use to scroll down to the anchor you are targeting : http://codepen.io/anon/pen/MKrGXZ

$('.formtoggle').click(function (event) {
    event.preventDefault();
  var $self = $(this);
    var hash = $self.attr('href');
    $(hash).toggleClass('hidden show');

  // Scroll then add hash to url
  $('html, body').stop().animate({
    scrollTop: $(hash).offset().top
  }, 300, function () {
    window.location.hash = hash;
  });
});

About the animation I would just use CSS for that with the transition property I think.

here is a nice example for your problem :-)

http://www.codesheet.org/codesheet/rNFhj0d7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM