简体   繁体   English

toggleClass揭示隐藏的div-添加动画和窗口滚动吗?

[英]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. 我创建了一个按钮来切换在其下方找到的某个div的可见性。 Is there a way to add a animation to the div between transitions of hidden and show? 有没有办法在隐藏和显示的过渡之间向div添加动画? Also, is there a way to get the window to scroll down to the top of the, now shown, div; 另外,有没有一种方法可以使窗口向下滚动到现在显示的div的顶部; but only when showing, not when hiding. 但仅当显示时,而不是隐藏时。

Example

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

HTML code HTML代码

<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) JS(在jQuery库顶部)

$('.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 在这里,您有一个代码,可用于向下滚动到定位的锚点: 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. 关于动画,我只是将CSS与我认为的transition属性一起使用。

here is a nice example for your problem :-) 这是您的问题的一个很好的例子:-)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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