简体   繁体   English

如何在小屏幕上触发此jQuery移动导航脚本?

[英]How do I make this jQuery mobile navigation script trigger only on small screens?

My jQuery is not very good - I'm trying to learn by piecing together bits of existing scripts but not having very much luck. 我的jQuery不是很好 - 我试图通过将现有脚本拼凑起来但没有太多运气来学习。 I'm trying to get a menu on a Wordpress website to act as a normal horizontal navigation bar on large screen sizes and to become a jQuery dropdown on widths below 980px - I've got the dropdown working but can't figure out how to get it to work only on small screen sizes. 我试图在Wordpress网站上获取一个菜单,作为大屏幕尺寸的普通水平导航栏,并成为宽度低于980px的jQuery下拉列表 - 我有下拉工作,但无法弄清楚如何让它在小屏幕尺寸上工作。

The code that works is: 有效的代码是:

    <script type="text/javascript">
jQuery(document).ready(function($) {
    $("#mmenu").hide();
    $(".mtoggle").click(function() {
        $("#mmenu").slideToggle(500);
    });
});
</script>

But obviously it hides the horizontal navigation on large screen sizes too. 但显然它也隐藏了大屏幕尺寸的水平导航。

I've tried 我试过了

    <script type="text/javascript">
jQuery(document).ready(function($) {
    if($(window).width() < 979) {
    $("#mmenu").hide();
    $(".mtoggle").click(function() {
        $("#mmenu").slideToggle(500);
    }
    });
});
</script>

but it seems to break the code completely and make it entirely non-functional and I can't figure out why. 但它似乎完全破坏了代码并使其完全无法正常运行,我无法弄清楚原因。

The website in question is http://host26.qnop.net/~fpsl/ if seeing the menu in context would be helpful. 有问题的网站是http://host26.qnop.net/~fpsl/,如果在上下文中查看菜单会有所帮助。

Any advice would be very much appreciated - thank you! 任何建议将非常感谢 - 谢谢!

if you reindent the code you can see the problem... the if closes before the click handler function 如果你重新编写代码,你可以看到问题......如果在单击处理函数之前关闭

here the corrected code: 这里更正的代码:

<script type="text/javascript">
jQuery(document).ready(function($) {
  if($(window).width() < 979) {
    $("#mmenu").hide();
    $(".mtoggle").click(function() {
      $("#mmenu").slideToggle(500);
    });
  }
});
</script>

i tend to do 我倾向于这样做

body:before{content:".";display:none;} (for desktop)
body:before{content:"..";display:none;} (for tablet)
body:before{content:"...";display:none;} (for mobile)

and then just query that, as CSS and Javascript have slightly different sizes when measuring the window. 然后只是查询,因为CSS和Javascript在测量窗口时的大小略有不同。

暂无
暂无

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

相关问题 如何仅在小屏幕上的特定页面上隐藏元素而无需刷新 webapp? - How do I hide an element, on specific pages only on small screens without having to refresh webapp? 如何使 header 仅在大屏幕上具有粘性,但在用户向下滚动时在移动设备上消失? - How can I make the header be sticky only on large screens but disappears on mobile as the user scrolls down? 仅在具有jquery或css的小屏幕上显示div - Show a div only on small screens with jquery or css React Native - 如何将本地化与 react-navigation 结合使用,以便跨屏幕访问翻译? - React Native - how do I use localization with react-navigation to make translations accessible across screens? 如何仅在移动屏幕上加载图像并做出响应? - How to make image load on mobile screens only and be responsive? 如何使 Vuetify 轮播图像响应,尤其是在移动屏幕上? - how do I make Vuetify carousel images responsive especially on mobile screens? 如何使元素触发以一个小的间隔一个接一个地出现? - How do I make elements trigger to appear one after another, with a small interval? 在 React Native Navigation 中,如何将道具发送到我的屏幕? - In React Native Navigation, how do I send props to my screens? 如何让这个粘性导航仅在向上滚动时显示? - How do I make this sticky navigation show only on scroll up? 如何在我的Jquery滑块中使这个子弹导航工作? - How do I make this bullet navigation work in my Jquery slider?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM