简体   繁体   English

使用jQuery显示/隐藏菜单

[英]Show/hide menu with jQuery

I have a page with content (on the left) and sidebar (on the right). 我有一个页面,其中包含内容(左侧)和侧边栏(右侧)。 For screen widths <= 480px the 2 div's are placed one under the other (100% widths). 对于宽度小于等于480像素的屏幕,将2个div放在另一个下方(100%宽度)。 The "Show/Hide" button that becomes visible is meant to toggle the sidebar's visibility when clicked. 可见的“显示/隐藏”按钮用于在单击时切换边栏的可见性。 I use 我用

$(".sidebar .box").slideToggle(200);

for this purpose. 以此目的。

See everything together on jsfiddle . jsfiddle上一起查看所有内容。

The problem: if I switch to wide screen and then back to narrow screen (width <= 480px) again, clicking the button produces a "back and forth" bug. 问题:如果我切换到宽屏,然后又回到窄屏 (宽度<= 480px),则单击按钮会产生“来回”错误。

Where is my mistake? 我的错误在哪里?

Thank you! 谢谢!

I believe the reason is that this code: 我相信原因是此代码:

$("a.expander").click(function () {
    $(this).toggleClass('close');
    $(".sidebar .box").slideToggle(200);
});

gets executed every time the function showSidebar is run, which is every time the window is resized. 每次运行showSidebar函数(即每次调整窗口大小)时都会执行一次 JQuery's click function adds a new event handler each time, and executes all of them on each window resize. jQuery的click函数每次都会添加一个新的事件处理程序,并在每次调整窗口大小时执行所有这些事件处理程序。

The solution would be to move the click handler registration outside of the function. 解决方案是将click处理程序注册移到该功能之外。

I believe that your problem is that your slide function gets called continuously when your window is resized. 我相信您的问题是,调整窗口大小时,滑动函数会被连续调用。 Rather than have this event occur unchecked, try to control it using an event handler like so: 而不是未经检查就发生此事件,请尝试使用如下事件处理程序对其进行控制:

Use .one() : 使用.one()

$('#your_element_id').one("click", function () {     
    $('.your_css_style_class').slideToggle(200); 
});

Another thing to keep in mind is that if down the line, you desire to display it hidden, you may want to use .slideDown() rather than .slideToggle() to begin with. 要记住的另一件事是,如果希望将其隐藏起来,则可能要使用.slideDown()而不是.slideToggle()

Here is a jQuery reference to the 'one' function: http://api.jquery.com/one 这是jQuery对“一个”函数的引用: http : //api.jquery.com/one

something like this? 这样的东西? http://jsfiddle.net/jqdvj42u/3/ http://jsfiddle.net/jqdvj42u/3/

$(document).ready(function() {
     $("a.expander").click(function () {
        $(this).toggleClass('close');
        $(".sidebar .box").slideToggle(200);
    });
});

function showSidebar() {   
    if ($(window).width() <= 480) {
        // Close sidebar when window width <= 480px
        $(".sidebar .box").removeClass('open');
        // Click expander button to show/hide sidebar

    } else {
        $("a.expander").removeClass('close');
        // Open sidebar when window width > 480px;
        $(".sidebar .box").addClass('open');
    }
}
showSidebar();

$(window).resize(showSidebar);

your click shouldnt be called within showSidebar() because it'll bind the click event every time that function is called. 您的点击不应在showSidebar()调用,因为它会在每次调用该函数时绑定click事件。 resize triggers showSidebar() multiple times, so the click is bound multiple times. 调整大小会多次触发showSidebar() ,因此单击会被绑定多次。 also, you should use jQuery 1.11 as the lowest version since its the most up-to-date version thats stable on IE<9. 同样,您应该使用jQuery 1.11作为最低版本,因为它的最新版本可以在IE <9上稳定。 if you dont mind support on IE<9, use jQuery version 2.0 or higher. 如果您不介意IE <9的支持,请使用jQuery 2.0或更高版本。

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

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