简体   繁体   English

适用于移动设备的CSS下拉导航

[英]Mobile-friendly CSS dropdown navigation

I am probably going to invoke the ire of the pros here. 我可能会在这里引起专业人士的愤怒。 I am a rank amateur trying to "learn as I go" on the job, but most javascript still has me baffled. 我是一个业余爱好者,试图在工作中“边走边学”,但是大多数javascript仍然让我感到困惑。 (I'm fairly good in HTML and CSS.) (我在HTML和CSS方面相当出色。)

I am developing a new dropdown navigation for my company's website (yes, finally in over my head), but I can't seem to figure out to make it mobile-friendly. 我正在为公司的网站开发一个新的下拉导航(是的,终于在我的头上了),但是我似乎无法弄清楚它是否适合移动设备。 I have searched and searched and searched this site as well as others, but my ignorance is perhaps preventing me from understanding some of the possible solutions provided. 我已经搜索并搜索了该站点以及其他站点,但是我的无知也许使我无法理解所提供的一些可能的解决方案。 (So rudimentary explanations would be highly appreciated from the patient.) (因此,对患者的基本解释将不胜感激。)

I am requiring instruction in just two things for making my navigation menu mobile-friendly: 我只需要两件事就可以使我的导航菜单适合移动设备:

  1. I need the dropdown to respond to click/touch, not hover. 我需要下拉菜单来响应单击/触摸,而不是悬停。 Because all levels of my drop-down contain links, I want the user to have to click/touch each button twice before they go to the link. 因为下拉菜单的所有级别都包含链接,所以我希望用户在单击链接之前必须单击/触摸两次每个按钮。 The first click/touch will simply open the actions I've assigned to the tags. 第一次单击/触摸只会打开我分配给标签的操作。 (Example: first click/touch on model number will show an appended thumbnail image of model--I already know how to do this.) (例如:第一次点击/触摸型号会显示一个附加的模型缩略图-我已经知道该怎么做。)
  2. I need the dropdown to able to close by either clicking/touching on a "close window" button or by clicking/touching the negative space on the page. 我需要下拉菜单以通过单击/触摸“关闭窗口”按钮或单击/触摸页面上的负空格来关闭。 At present, only refreshing the page or clicking/touching another active link enables the user to advance. 目前,只有刷新页面或单击/触摸另一个活动链接才能使用户前进。

For the record, I am simply trying to adapt the dropdown menu demonstrated at the following page for our company's use, but it is hover rather than click/touch activated: 作为记录,我只是在尝试调整下一页中显示的下拉菜单以供我们公司使用,但该菜单悬停而不是单击/触摸激活:

http://code.tutsplus.com/tutorials/how-to-build-a-kick-butt-css3-mega-drop-down-menu--net-15129 http://code.tutsplus.com/tutorials/how-to-build-a-kick-butt-css3-mega-drop-down-menu--net-15129

Please help. 请帮忙。 Forgive my ignorance, I am trying. 原谅我的无知,我正在努力。 Blessed are the merciful. 仁慈的人有福了。

Thanks in advance. 提前致谢。

I would think that jQuery is your friend here, Specifically .on('click', ... and .on('dblclick', ... 我认为jQuery是您的朋友,特别是.on('click',...和.on('dblclick',...

There are a few ways I can think to do this. 我可以考虑几种方法来做到这一点。 one is to keep track of your clicked items such that the first time an item is clicked 'this' is remembered and when the item is clicked again we compare the items and if they match trigger the event: 一种是跟踪您单击的项目,以便第一次单击该项目时会记住“ this”,当再次单击该项目时,我们将比较这些项目,如果匹配则触发事件:

var clickedItem = '';

$(document).on('click', '#dropdown1', function() {
  if (clickedItem == '' || clickedItem != this) {
    clickedIten = this;
    Do other stuff..
  }
  else if (clickedItem == this) {
    Open Dropdown...
  }
});

As for closing the dropdown when nothing is clicked checkout jQuery focusout and set it to a general selector for all of the dropdown. 至于单击未单击时关闭下拉菜单,请签出jQuery focusout,并将其设置为所有下拉菜单的常规选择器。

At the risk of being dealt the full wrath of the pro developers on here, I've decided to post my solution, such as it is, just incase it helps others that are as lazy as me. 冒着被专业开发人员大怒的风险,我决定发布我的解决方案,例如它,以防万一它可以帮助像我这样懒惰的其他人。

I too am a rank amateur and, like many others, have come up against the problem of pure html/css dropdown menus working on mobiles and tablets when going mobile friendly. 我也是一名业余爱好者,并且和许多其他人一样,遇到友好移动时在移动设备和平板电脑上使用纯html / css下拉菜单的问题。

The thing is, on my old, none mobile friendly site, the dropdown menu worked just fine on mobiles, iPhones, tablets etc but after re-developing it to be mobile friendly (thanks Google!) it suddenly didn't!? 问题是,在我以前没有移动设备的旧网站上,下拉菜单在手机,iPhone,平板电脑等设备上都可以正常工作,但是在将其重新开发为移动设备友好型产品(感谢Google!)后,它突然没有了!? After spending hours going through my code to find out why, I realised that the only difference between the old site and new, other than pixel widths being changed to % widths, was that the new site had this new and ALL important meta tag: 在花了几个小时查找代码原因之后,我意识到,旧站点和新站点之间的唯一区别(除了像素宽度已更改为%宽度)是新站点具有此新的和所有重要的元标记:

<meta name="viewport" content="width=device-width, initial-scale=1.0">

I knew this code needed to be there but now I also knew that it was causing all my problems... Well, all my website related problems anyway. 我知道此代码必须存在,但现在我也知道它导致了我所有的问题……好吧,无论如何,我所有与网站有关的问题都存在。

I came up with this lazy and cheeky solution that got my dropdown menu working as it had before going mobile friendly. 我想出了一个这个懒惰而厚脸皮的解决方案,它使我的下拉菜单像在移动友好之前一样工作。

Simply change the initial-scale a nominal amount below 1.0 and add a bit of code for good measure, like this: 只需将初始比例的名义金额更改为低于1.0,并添加一些代码即可,例如:

<meta name="viewport" content="width=device-width, initial-scale=0.994, user-scalable=yes">

I'm not sure why this works but I can only assume that setting the initial-scale to 1.0, ie, the full width of the screen, disables these devices inbuilt double tap feature. 我不确定为什么这样做,但我只能假设将初始比例设置为1.0(即屏幕的整个宽度)会禁用这些设备的内置双击功能。

Setting it just below, opens the door just enough for their own double tap features to work again, saving you the trouble of putting in a whole bunch of complicated code just to get mobiles, iPhones and tablets to do something they're already capable of doing. 将其设置在正下方,足以为他们自己的双击功能再次打开打开门,免除了您输入一堆复杂代码以使手机,iPhone和平板电脑执行他们已经能够执行的操作的麻烦在做。

I have tested this on my Sony Z1 and also put it through Googles mobile friendly test and no problems there! 我已经在我的Sony Z1上对其进行了测试,并且还通过了Google的移动友好测试,并且没有任何问题!

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

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