简体   繁体   English

JQuery汉堡菜单功能

[英]JQuery Hamburger Menu Functions

Below is the script I am trying to write to control two functions when the website's menu button is clicked; 下面是单击网站的菜单按钮时我试图编写的用于控制两个功能的脚本; it is a hamburger menu that toggles the menu links. 这是一个汉堡菜单,可切换菜单链接。 The first function shows/hides the menu links and the second fades an element on the page, both activated when the menu button is clicked. 第一个功能显示/隐藏菜单链接,第二个功能淡化页面上的元素,这两个功能都在单击菜单按钮时激活。

In the first function, I am having trouble creating a delay/fadeIn for the menu links. 在第一个功能中,我无法为菜单链接创建delay / fadeIn。 I need '.navbar-item' to fade in and out when the menu is clicked. 单击菜单时,我需要“ .navbar-item”淡入和淡出。 In the second function, I need to revert the opacity to 1.0 when the menu is clicked a second time. 在第二个功能中,当第二次单击菜单时,我需要将不透明度恢复为1.0。 I can not get any of the effects to occur after the first effect has completed, ie Menu is clicked to fade in menu links and dim '.values', menu is clicked to fade out menu links and revert '.values' to 100% opacity. 在第一个效果完成后,我无法获得任何效果,即单击“菜单”以淡入菜单链接并使“ .values”变暗,单击“菜单”以淡出菜单链接并将“ .values”还原为100%不透明度。

<div class="container">
    <section class="header">
      <h2 class="title"><a href="index.html">Title</a>
      <li class="client-item"><a class="client-link" href="#"><i class="fa fa-bars"></i></a></li></h2>
    </section>
    <nav class="navbar" style="display: none;">
      <ul class="navbar-list">
        <li class="navbar-item"><a class="navbar-link" href="#" target="_top">Contact</a></li>
        <li class="navbar-item navbar-link">Store</li>
      </ul>
    </nav>

<div class="section values">
        <div class="container">
          <div class="row">
            <div class="one-full column">
            </div>
          </div>
        </div>
</div>



 // Main Script For Site

    $(document).ready(function() {
  $('.client-link').click(function() {
          $('.navbar').slideToggle("fast");
          $('.values').animate({opacity:'0.6'});
  });
});

This answer gives how to get simultaneous animations. 该答案给出了如何获取同步动画。 jQuery's own docs describe slideToggle , including the bits you'd need to set similarly to how animate would need to be set. jQuery自己的文档描述了slideToggle ,其中包括需要设置的位与需要设置animate方式类似。

I might also point out that there's no reason to separate the animate calls like you have. 我可能还会指出,没有理由像您一样分开animate调用。 Since they're triggered by the same thing, they should be called from the same place. 由于它们是由同一事物触发的,因此应从同一位置调用它们。

Something like this, I think: 我想是这样的:

$(document).ready(function() {
  $('.client-link').click(function() {
    var $this = $(this);
    var opening = !$this.data('isOpen');
    $this.data('isOpen',opening);
    if(opening) {
      // opening animations
      $('.navbar').slideDown({duration:'fast',queue:false});
      $('.values').animate({opacity:1},{queue:false});
    } else {
      // closing animations
      $('.navbar').slideUp({duration:'fast',queue:false});
      $('.values').animate({opacity:0},{queue:false});
    }
  });
});

Though you may be better off moving your animations to CSS and just toggling a class. 尽管将动画移动到CSS并切换一个类可能会更好。

You were very close, you have just made some simple mistakes. 您非常接近,您刚刚犯了一些简单的错误。 Here is a JSFiddle gives you a solution to your problem: https://jsfiddle.net/nv1gytrs/1/ 这是JSFiddle为您提供的解决方案: https ://jsfiddle.net/nv1gytrs/1/

HTML: HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

<div class="client-link"></div>
<div class="navbar"></div>
<div class="values"></div>

CSS: CSS:

.client-link {
    height: 100px;
    width: 100px;
    border: 2px solid green;
 }
 .navbar {
    height: 100px;
    width: 100px;
    border: 2px solid red;
 }
 .values {
    height: 100px;
    width: 100px;
    border: 2px solid blue;
    transition: all 1s;
 }
 .fade {
    opacity: 0.2;
 }

JS: JS:

// Main Script For Site

$(document).ready(function() {
    $('.client-link').on("click", function() {
        $('.navbar').slideToggle("fast");
        $('.values').toggleClass("fade");
    });
});

Of course, all of your HTML and CSS would be unique to what you are trying to accomplish, this is just an example. 当然,您要尝试完成的所有HTML和CSS都是唯一的,这只是一个示例。

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

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