简体   繁体   English

Javascript菜单不同的屏幕尺寸

[英]Javascript Menu different screen sizes

I have a website which has a navigation bar go across the page in the main header. 我有一个网站,该网站的主标题页中有一个导航栏。 For a handheld screen device, I want the navigation list ( <ul><li> ) to collapse (using CSS to set display: none; ) and for a new bar to appear allowing a click to expand the menu and change the display property to display: initial; 对于手持式屏幕设备,我希望导航列表( <ul><li> )折叠(使用CSS设置display: none; ),并出现一个新的栏,允许单击以展开菜单并更改显示属性显示:初始;

Using CSS @media queries I can adjust that just fine and the javascript code makes the menu expand and collapse. 使用CSS @media查询,我可以对其进行调整,而javascript代码可使菜单展开和折叠。

But once collapsed, the CSS won't override the JavaScript for larger screen sizes. 但是,一旦折叠,CSS就不会覆盖较大屏幕尺寸的JavaScript。

How do I ensure the navigation is always there for the larger screen size but allow it to collapse for the mobile site? 如何确保较大屏幕的导航始终存在,但对于移动网站却无法折叠?

You will have to use the desktop first approach. 您将必须使用桌面优先方法。 Write the css for what you want the style for the desktop and override using media queries later. 为所需的桌面样式编写CSS,并在以后使用媒体查询覆盖。

Note that there are two aspects to what you are trying to achieve: CSS + Javascript. 请注意,您要实现的目标有两个方面:CSS + Javascript。 I believe your confusion is in how these two will work together, well then let's make things clear: 我相信您的困惑在于这两者将如何协同工作,那么让我们澄清一下:

  1. You will need to code two menu bars, one for the desktop widths (anything beyond 768px or whatever you decide), and one for mobile widths, which will be at a minimum of 320px. 您将需要编写两个菜单栏,一个用于桌面宽度(768像素以外的任何宽度或您决定的大小),另一个用于移动宽度(至少为320像素)。 You can use CSS media queries to display or hide each, do not use Javascript for this task. 您可以使用CSS媒体查询来显示或隐藏每个媒体查询,请勿为此任务使用Javascript。

  2. Use Javascript (...or jQuery since it has nice functions for the following) to control the behavior of the navigation bars, such as on click events and the like. 使用Javascript(...或jQuery,因为它具有以下出色的功能)来控制导航栏的行为,例如单击事件等。 It will be good for you to look into the fadeToggle function (and similar functions) to achieve the animation of the mobile nav bar (collapsable or not). 最好看看fadeToggle函数(以及类似的函数)来实现移动导航栏的动画(可折叠与否)。

This way you can ensure navigation is always present. 这样,您可以确保导航始终存在。 A word of advice in terms of user experience: if you will be using icons to represent certain nav bar elements, make sure to use icon + text, that way it is clear to anyone what the icon represents. 关于用户体验的建议:如果您将使用图标来表示某些导航栏元素,请确保使用图标+文本,这样任何人都可以清楚地看到图标所代表的含义。

Let me know if this helped you. 让我知道这是否对您有帮助。 I can write code for you but I do not want to go along and implement it without seeing what you can achieve first, feel free to edit your question with some code... the above points should be enough to get you started in the right direction. 我可以为您编写代码,但我不想在没有看到您可以首先实现的目标的情况下继续实施它,可以随时使用一些代码来编辑您的问题...以上几点应足以使您从正确的角度入手方向。

暂无
暂无

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

相关问题 不同屏幕尺寸的Javascript - Javascript on different screen sizes 有条件地为不同的屏幕尺寸提供不同的JavaScript - Conditionally serving different javascript for different screen sizes 以2种不同的屏幕尺寸加载不同的Javascript函数 - Load Different Javascript Functions at 2 Different Screen Sizes 允许在不同屏幕尺寸的菜单上显示和隐藏的功能 - Function to allow show and hide on menu for different screen sizes 如何在不同屏幕尺寸下交换 JavaScript 事件侦听器 - How to swap JavaScript event listeners at different screen sizes 在javascript中使用鼠标坐标,如何适应不同的屏幕尺寸? - Using mouse coordinates in javascript, how to accommodate for different screen sizes? 如何放置底部菜单,使其在不同屏幕尺寸下都位于页面底部上方? - How can I position my bottom menu so that it stays above the bottom of the page across different screen sizes? 顶部导航菜单下拉菜单隐藏在网页内容下方,我想固定不同屏幕尺寸上的内容位置 - The top navigation menu dropdowns is hidden below the webpage content, and i want to fix content position on different screen sizes 单张响应式设计-使用JavaScript为不同的屏幕尺寸创建不同的缩放级别 - Leaflet responsive design - creating different zoom levels for different screen sizes using JavaScript 在较大的屏幕尺寸上删除了汉堡菜单 - Hamburger Menu removed on larger screen sizes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM