简体   繁体   English

jQuery下拉菜单和CSS Media Queries冲突

[英]jQuery drop down menu and CSS Media Queries conflicting

So let's start with the HTML. 那么让我们从HTML开始吧。 Probably don't need to bother putting it here, but I'll show you guys anyway. 可能不需要费心把它放在这里,但是无论如何我都会告诉大家。

        <div class="menu col md12 sm02 mn03">
            <div class="lines"></div>
            <div class="lines"></div>
            <div class="lines"></div>
        </div>
        <div class="nav col mx07 lg08 md12 sm12 mn12">
            <ul>
                <li class="col mx02 md02 sm04 mn06"><a href="#">Company</a></li>
                <li class="col mx02 md02 sm04 mn06"><a href="#">Solutions</a></li>
                <li class="col mx02 md02 sm04 mn06"><a href="#">Services</a></li>
                <li class="col mx02 md02 sm04 mn06"><a href="#">Websites</a></li>
                <li class="col mx02 md02 sm04 mn06"><a href="#">Software</a></li>
                <li class="col mx02 md02 sm04 mn06"><a href="#">Contact</a></li>
            </ul>
        </div>

Now for the CSS. 现在为CSS。 I have it set so my menu button stays hidden. 我设置好了,所以我的菜单按钮保持隐藏状态。

.menu {display: none;}

And then when the screen gets below 701 pixels, the media queries kick in, part of which is this. 然后当屏幕低于701像素时,媒体查询就会启动,其中一部分就是这个。

.menu {display: inherit;}
.nav {display: none;}

And then, good ol' jQuery comes in to make my navbar toggle on clicking the menu button. 然后,很好的jQuery进来,使我的导航栏在单击菜单按钮时切换。

$(document).ready(function() {
    $(".menu").click(function() {
        $(".nav").toggle("slow");
    });
});

So now for the issue. 所以现在问题。 I go to the smaller screen size that activates my menu button and hides my navigation bar. 我转到较小的屏幕尺寸,激活我的菜单按钮并隐藏我的导航栏。 Click the menu button, navbar shows up. 单击菜单按钮,将显示导航栏。 Hooray! 万岁! Click it again, it goes away. 再次单击它,它消失了。 Hooray! 万岁!

Except when I go back to the larger screen size, the navbar doesn't come back. 除非我回到更大的屏幕尺寸,导航栏才会回来。 The menu button disappears, but the navbar won't show up unless I go back to the smaller size, toggle it on, then go back to larger size again. 菜单按钮消失,但导航栏不会显示,除非我回到较小的尺寸,切换它,然后再次返回到更大的尺寸。 I know what's going on here, that's the easy part. 我知道这里发生了什么,这很容易。 Jquery changes and overrides the CSS and tells the browser "Okay, just hide it all the time." Jquery更改并覆盖CSS并告诉浏览器“好的,只是一直隐藏它。”

So question is, how do I make it so my navbar comes back once I get back to the larger screen size, regardless of the toggle state? 所以问题是,我如何做到这一点,一旦我回到更大的屏幕尺寸,我的导航栏会回来,无论切换状态如何?

I don't have the code for this last part anymore, but at one point I had it to where Javascript was checking for the screen size, but JS and CSS screen sizes were never the same so I just gave up on that solution. 我不再拥有最后一部分的代码了,但有一次我把它用于Javascript检查屏幕大小的地方,但JS和CSS屏幕尺寸从未相同,所以我放弃了该解决方案。

Thanks in advance for your answers. 提前感谢您的回答。

如果你给nav这些类“visible-lg visible-md”,它可能会工作,我假设你使用bootstrap。

Found the solution, thanks to user instead suggesting matchMedia() 找到了解决方案,这要感谢用户而不是建议matchMedia()

//screen size variables
var mqmn = window.matchMedia("screen and (min-width: 150px) and (max-width: 400px)")
var mqsm = window.matchMedia("screen and (min-width: 401px) and (max-width: 550px)")
var mqmd = window.matchMedia("screen and (min-width: 551px) and (max-width: 700px)")
var mqlg = window.matchMedia("screen and (min-width: 701px) and (max-width: 850px)")

//toggle drop-down menu
$(document).ready(function() {
    $(".menu").click(function() {
        $(".nav").toggle("slow");
    });
    $(window).resize(function(){
        if (mqmn.matches){
            //mn size
            $(".menu").show();
            $(".nav").hide();
        } else if (mqsm.matches) {
            //sm size
            $(".menu").show();
            $(".nav").hide();
        } else if (mqmd.matches) {
            //md size
            $(".menu").show();
            $(".nav").hide();
        } else if (mqlg.matches) {
            //lg size
            $(".menu").hide();
            $(".nav").show();
        } else {
            //mx size
            $(".menu").hide();
            $(".nav").show();
        }
    });
});

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

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