使用一系列动画切换菜单

时间:2017-03-03 21:35:31

标签: jquery css animation menu

我准备编写一个叠加菜单,显然是通过点击显示的。问题是,它不仅仅是一个纯色的叠加层强加在屏幕上。相反,布局元素会发生某些淡出动画,然后菜单会出现。通过再次单击相同的按钮,菜单消失,然后元素一个接一个地淡入淡出。 我设法意识到这一点直到两个州之间的转换。如何在两种状态之间切换?

HTML

<div class="menu-contact trans-short">
  <dir class="wrap">
    <!-- 
    these 2 are one button. depending on the state one disappears 
    -->
    <a href="#" class="btn-contact">CONTACT</a>
    <a href="#" class="btn-contact-close off">CLOSE</a>
  </dir>
</div>

JQUERY

            var contact;

            // contact button
            $(document).on('click', 'a.btn-contact', function(){
                var contact = true;
            });
            $(document).on('click', 'a.btn-contact-close', function(){
                var contact = false;
            });

            if (contact == true) {

                    // disable scrolling
                    $.fn.fullpage.setAllowScrolling(false);
                    $.fn.fullpage.setKeyboardScrolling(false);

                    setTimeout(function() {
                        // switch to display block
                        $( ".content-wrapper-contact" ).removeClass( "off" );
                    }, 300);

                    // fade out other elements
                    $( '.container' ).addClass( 'blur' );
                    $( ".content-wrapper" ).fadeOut(300);
                    $( ".page-title" ).fadeOut(600);

                    setTimeout(function() {
                        $('.nav-bottom li').addClass('fade');
                        $( '.media-container' ).addClass( 'blur' );
                        $( '.overlay-title' ).addClass('reveal');

                    }, 800);

                    // button animation: contact -> close
                    $('.menu-contact').addClass('fade a-to-top');
                    setTimeout(function() {
                        // fade in the overlay container
                        $( ".content-wrapper-contact" ).removeClass( "fade" );

                        $('.btn-contact').addClass('off');
                        $('.btn-contact-close').removeClass('off');
                        setTimeout(function() {
                            $('.menu-contact').removeClass('fade a-to-top');
                        }, 510);
                    }, 510);
            }
            else if (contact == false) {

                    // button animation: close -> contact
                    $('.menu-contact').removeClass('fade a-to-top');
                    setTimeout(function() {
                        // fade in the overlay container
                        $( ".content-wrapper-contact" ).addClass( "fade" );

                        $('.btn-contact').removeClass('off');
                        $('.btn-contact-close').addClass('off');
                        setTimeout(function() {
                            $('.menu-contact').addClass('fade a-to-top');
                        }, 510);
                    }, 510);    
            }

1 个答案:

答案 0 :(得分:0)

中的脚本
if (contact == true) { ...

仅在加载页面时调用,而不是在单击链接时调用。例如,您可以直接将代码移动到点击功能中,即

// contact button
$(document).on('click', 'a.btn-contact', function(){

  // disable scrolling
  $.fn.fullpage.setAllowScrolling(false);
  $.fn.fullpage.setKeyboardScrolling(false);

  setTimeout(function() {
    // switch to display block
    $( ".content-wrapper-contact" ).removeClass( "off" );
  }, 300);

  // fade out other elements
  $( '.container' ).addClass( 'blur' );
  $( ".content-wrapper" ).fadeOut(300);
  $( ".page-title" ).fadeOut(600);

  setTimeout(function() {
    $('.nav-bottom li').addClass('fade');
    $( '.media-container' ).addClass( 'blur' );
    $( '.overlay-title' ).addClass('reveal');

  }, 800);

  // button animation: contact -> close
  $('.menu-contact').addClass('fade a-to-top');
  setTimeout(function() {

    // fade in the overlay container
    $( ".content-wrapper-contact" ).removeClass( "fade" );

    $('.btn-contact').addClass('off');
    $('.btn-contact-close').removeClass('off');
    setTimeout(function() {
      $('.menu-contact').removeClass('fade a-to-top');
    }, 510);
  }, 510);

});

$(document).on('click', 'a.btn-contact-close', function(){

  // button animation: close -> contact
  $('.menu-contact').removeClass('fade a-to-top');
  setTimeout(function() {

    // fade in the overlay container
    $( ".content-wrapper-contact" ).addClass( "fade" );

    $('.btn-contact').removeClass('off');
    $('.btn-contact-close').addClass('off');
    setTimeout(function() {
      $('.menu-contact').addClass('fade a-to-top');
    }, 510);
  }, 510);  

});

这是fiddle