[英]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.