简体   繁体   English

在移动设备上单击时更改背景元素的背景颜色和文本颜色

[英]Change background element's background-color and text color when clicked on mobile

I have a header and a footer that work perfectly when the user is on a desktop and has a mouse and can hover over them and navigate everything that way. 我有一个页眉和页脚,当用户在桌面上并且有鼠标并且可以将鼠标悬停在它们上并以这种方式导航时,它们可以完美地工作。

The problem is on mobile when there is no mouse, I need everything to work as close to the same as it does on desktop. 当没有鼠标时,问题出在移动设备上,我需要的所有功能都必须与台式机一样。 I was almost able to make the "PROJECTS" drop-down work to my liking when clicked as well as the "CONNECT" drop-up, but I'm having two issues. 单击和“连接”下拉菜单时,我几乎都能按自己的喜好使“项目”下拉菜单起作用,但是我遇到了两个问题。

The first problem I'm having is when "PROJECTS" or "CONNECT" drop-ups/drop-downs are clicked, when I click off them or I click them again, the drop-up/drop-down doesn't vanish. 我遇到的第一个问题是单击“项目”或“连接”下拉菜单/下拉菜单时,当我单击它们或再次单击它们时,下拉菜单/下拉菜单不会消失。 It becomes constantly displayed block and I need it to go back to display: none when the user clicks anywhere else or clicks "PROJECTS"/"CONNECT" a second time. 它变成了不断显示的块,我需要返回显示:当用户单击其他任何地方或再次单击“项目” /“连接”时,没有显示。 I thought the "else" statement in the script I'm using would accomplish that, but apparently it is not working and I'm not sure why! 我以为我正在使用的脚本中的“ else”语句可以完成该操作,但是显然它不起作用,我不确定为什么! What is the best way to make that work? 使该工作最佳的方法是什么?

The second problem I'm having is when the user clicks on the li item "ONE" inside "PROJECTS" or any of the li items inside "CONNECT", I need the background of the clicked element to turn black and the text to turn white right before it loads the link (similar to how it looks on the desktop version using the :hover css). 我遇到的第二个问题是,当用户单击“项目”内的li项目“ ONE”或“ CONNECT”内的任何li项目时,我需要clicked元素的背景变为黑色,文本变为加载链接之前为白色(类似于使用:hover css在桌面版本上的外观)。 At this point, when they are clicked it just flashes the gray highlight color over the element for a split second. 此时,当单击它们时,它只是在元素上闪烁灰色突出显示的颜色一秒钟。 I tried adding the same type of css but changing "hover" to "focus" or "visited" and I couldn't get either to work. 我尝试添加相同类型的CSS,但是将“悬停”更改为“焦点”或“访问”,但我都无法工作。 What is the best way to accomplish that? 做到这一点的最佳方法是什么?

Here is a JSFiddle of all the code: http://jsfiddle.net/xmdzg8vu/ 这是所有代码的JSFiddle: http : //jsfiddle.net/xmdzg8vu/

If you view the JSFiddle on desktop, it will be hard to see the issues I'm having since you have a mouse and can hover... hopefully having the code will help with finding my errors though! 如果您在桌面上查看JSFiddle,则很难看到我遇到的问题,因为您有鼠标并且可以悬停...希望代码可以帮助发现我的错误!

HTML: HTML:

<div id="background"></div>
<header id="header">
  <div id="nav">
    <ul>
      <li id="projects" onclick="displayDropdown()">
        PROJECTS
        <ul>
          <a href="/one" class="blocklink" target="_self">
            <li id="one">ONE</li>
          </a>
        </ul>
      </li>
    </ul>
  </div>
</header>
<footer id="footer">
  <div id="footer-nav">
    <ul>
      <li id="connect" onclick="displayDropup()">
        CONNECT
        <ul>
          <a href="https://www.instagram.com/" target="_blank" class="blocklink">
            <li id="instagram">
              INSTAGRAM
            </li>
          </a>
          <a href="https://twitter.com/" target="_blank" class="blocklink">
            <li id="twitter">
              TWITTER
            </li>
          </a>
          <a href="mailto:mail@mail.com" class="blocklink">
            <li id="email">
              EMAIL
            </li>
          </a>
        </ul>
      </li>
    </ul>
  </div>
</footer>

JS and CSS are seen in the JSFiddle. JS和CSS在JSFiddle中可见。

Thank you so much! 非常感谢!

UPDATE: I have updated my JSFiddle to remove extra script not applicable to this question. 更新:我已经更新了JSFiddle,以删除不适用于此问题的额外脚本。 Hopefully that cleans it up a little! 希望这可以清理一点!

For the second problem, have you tried the :active selector? 对于第二个问题,您是否尝试过:active选择器? For example, using part of your CSS code: 例如,使用部分CSS代码:

#one:focused, #one:visited, #one:hover, #one:active {
    background-color: black;
    color: white;
}

It looks like there are some issues in your markup. 标记中似乎存在一些问题。 Normally we don't nest ul>a . 通常我们不嵌套ul>a We normally do ul>li>a . 我们通常ul>li>a

For problem #1 you could add an onclick event handler to the submenu li to close the dropdown (/dropup) ul when a submenu item is clicked. 对于问题1,您可以在子菜单项中添加onclick事件处理程序,以在单击子菜单项时关闭下拉菜单(/ dropup)ul。

Let me know if that works. 让我知道是否可行。 You might also want to use touch events for mobile (even thought click might work). 您可能还想将触摸事件用于移动设备(即使认为click可能有效)。

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

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