简体   繁体   English

切换按钮查询上的jQuery / JavaScript显示/隐藏事件

[英]jQuery/JavaScript show/hide event on Toggle Button query

I am just delving into the world of JavaScript and have a query regarding a conundrum I am currently experiencing.... 我只是在研究JavaScript的世界,并且有一个关于我当前遇到的难题的查询。

I have a menu button which reveals and hides the nav menu on the site I am working on.To this menu button I have attached some JavaScript to 'hide' the main site content when the nav bar is revealed and to 'show' the main content when the nav bar is subsequently closed again. 我有一个菜单按钮,可显示和隐藏正在工作的网站上的导航菜单。在此菜单按钮上,我附加了一些JavaScript,以在显示导航栏时“隐藏”主站点内容并“显示”主站点。导航栏随后再次关闭时显示内容。

The JavaScript I have coded does the job, but I know it's not right and that somehow I fluked it! 我编写的JavaScript可以完成这项工作,但我知道这是不对的,因此我不知所措! I know this because the timing values of the script for both the 'reveal' and 'hide' element interact with each other...making setting the timing of the animations somewhat unpredictable to set accurately. 我之所以知道这一点,是因为脚本的“显示”和“隐藏”元素的时间值相互影响...使得动画的时间设置有些难以预测,无法准确设置。

The menu button which shows and hides the nav menu has the following classes attached to it: 显示和隐藏导航菜单的菜单按钮具有以下类别:

.menu-toggle

And when it is clicked to reveal the nav bar an additional class is added which is: 当单击以显示导航栏时,将添加一个附加类:

.toggled-on

So in it's clicked state the classes are: 因此,在单击状态下,类为:

'.menu-toggle toggled-on'

Here is the JS which controls the hiding and revealing of the site content: 这是控制网站内容隐藏和显示的JS:

$(document).on('click', '#menu-toggle', function() {
  $('.site-content').animate({opacity:1, visibility:'visible'}, 100);
});

$(document).on('click', '.toggled-on', function() {
  $('.site-content').animate({opacity:0, visibility:'visible'},        300);
});

I believe the issue is arising because of the assignment of the classes remains in both the 'normal' and 'clicked' states of the menu button. 我相信问题是由于类别的分配而出现的,而这些类别仍然处于菜单按钮的“正常”和“单击”状态。

I think I need to do somethng like this: 我想我需要做这样的事情:

    $(document).ready(function(){

    if ( $('#menu-toggle').hasClass('toggled-on') ) {
    $('#menu-toggle').addClass('nav-on');
  } else {
    $('#menu-toggle').addClass('nav-off');
  }

});

This however doesn't work. 但是,这不起作用。

If in anyone could point me in the direction of how to better code this then that would be greatly appreciated. 如果有人能指出我如何更好地编写代码的方向,那么将不胜感激。

If i understood correctly, u want to toggle the class. 如果我正确理解,您想切换课程。

Could you try http://api.jquery.com/toggleclass/ . 您能否尝试http://api.jquery.com/toggleclass/

Let me know if it works for you ! 请让我知道这对你有没有用 !

Thanks 谢谢

Kamal 卡马尔

$(function () {
    $('body').on('click', '#menu-toggle', function () {
      $('.site-content').toggle(300)
    })
})

The above code will cause the .site-content div to fade in and fade out when you click #menu-toggle . 当您单击#menu-toggle时,以上代码将使.site-content div淡入和淡出。

Wrapping the function literal inside of the jQuery function $() is just shorthand for $(document).ready() . 将函数文字包装在jQuery函数$()中只是$(document).ready()简写。 You can write it either way and it works the same. 您可以使用任何一种方式编写它,并且其工作原理相同。 I like the more terse syntax. 我喜欢更简洁的语法。

The .toggle() method in jQuery simply toggles the visibility of an element. jQuery中的.toggle()方法只是切换元素的可见性。 You don't need to change classes or directly manipulate opacity or visibility to do that. 您无需更改类或直接操纵不透明度或可见性即可。 jQuery detects whether or not it's visible and takes care of the rest. jQuery会检测它是否可见并负责其余的工作。 You can add a duration to get the fade effect. 您可以添加持续时间以获得淡入淡出效果。

I delegated the binding to the body tag because I don't know the structure of your HTML. 我将绑定委托给body标签,因为我不知道您的HTML的结构。 If .site-content has a wrapper div, you can delegate to that.(The idea is that it's more efficient to delegate closer to the event target rather than make the event bubble all the way up to the document). 如果.site-content具有包装器div,则可以委派给它(想法是,将事件委派到更靠近事件目标的位置而不是使事件一直冒泡到整个文档更有效)。

Enjoy! 请享用!

UPDATE FROM OP: 从OP更新:

Thanks for the reply.This does indeed toggle the content,but I was using the visibility and opacity properties because I don't want the layout to change when the .site-content is hidden.When visibility is toggled the content obviously fully dissapears 感谢您的答复。的确切换了内容,但是我使用了visible和opacity属性,因为当.site-content隐藏时,我不希望更改布局。切换可见性时,内容显然完全消失了

NEW SOLUTION: 新解决方案:

I don't know what you layout / CSS looks like, but I think this is what you want... 我不知道您的布局/ CSS是什么样的,但是我想这就是您想要的...

Add the following to the CSS style for the .site-content : 将以下内容添加到.site-content的CSS样式中:

visibility: visible;

Now add the following CSS rule: 现在添加以下CSS规则:

.site-content.hidden {
    visibility: hidden;
}

Now change the JS code to the following: 现在将JS代码更改为以下代码:

$(function () {
    $('body').on('click', '#menu-toggle', function () {
      $('.site-content').toggleClass('hidden')
    })
}) 

The style visibility: hidden means that you will no longer see the element, but it will still be part of the document flow. 样式visibility: hidden意味着您将不再看到该元素,但它仍将是文档流的一部分。 In other words, it will still take up space in your layout. 换句话说,它仍然会占用布局中的空间。 This change will not affect the rest of the layout. 此更改不会影响其余的布局。

Now, when you click #menu-toggle it will either add or remove the hidden class. 现在,当您单击#menu-toggle ,它将添加或删除隐藏的类。

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

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