【问题标题】:Selectors fail to translate properly from Chrome to Firefox and Safari选择器无法从 Chrome 正确转换为 Firefox 和 Safari
【发布时间】:2015-04-07 19:36:09
【问题描述】:

完整解释

昨晚我正在复制我发现的一个有趣的 Codepen。完成后,我对它的工作方式非常满意。因为我不打算在生产中使用它,所以我忽略了使用正确的 CSS 前缀来确保跨浏览器的兼容性。然而,当我决定在 Safari 和 Firefox 中检查它时,我注意到我的 ":hover" 事件没有正确翻译,到目前为止我还无法修复它。我不需要这个项目来做任何事情,但我想要一些帮助来弄清楚为什么会发生这种情况,以便我可以准备在未来的开发中解决这个问题。

直接问题

在 Firefox 和 Safari 中,当您将鼠标悬停在“Fly-Out”菜单标题右侧的菜单项上时,它们会折叠起来,而在 Chrome 中它们会保持在外面。

我想强调一下,我理解为什么某些功能(例如转换)不起作用,因为我没有为大多数功能指定“webkit”或“moz”前缀。困扰我的只是折叠的菜单。

代码

ul.sidebar {
  display: block;
  position: absolute;
  margin: 0;
  left: 0;
  height: 100%;
  width: 6.5em;
  background: #aa2266;
  padding: 0;
  text-align: center;
}
ul.sidebar * {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
ul.sidebar a {
  color: white;
  text-decoration: none;
  margin: 0;
  display: inline-block;
  width: 100%;
  height: 100%;
}
ul.sidebar a > i {
  margin-top: 0.13333em;
  font-size: 2em;
  display: block;
  margin-bottom: 0.2em;
}
ul.sidebar a > span {
  font-size: 0.8em;
  font-family: sans-serif;
}
ul.sidebar li {
  transition: background 0.3s;
  background: #aa2266;
}
ul.sidebar > li {
  margin: 0;
  padding: 0;
  height: 4em;
  display: inline-block;
  position: relative;
  width: 100%;
  background: #aa2266;
  transition: background 0.3s;
}
ul.sidebar > li:hover {
  background: #dc3278;
}
ul.sidebar > li:hover:after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 100%;
  width: 6px;
  background: linear-gradient(to right, rgba(0, 0, 0, 0.25) 0%, transparent 100%);
}
ul.sidebar > li > a {
  transition: background 0.3s;
}
ul.sidebar > li > a:hover {
  background: #ff5f87;
}
ul.sidebar > li > ul {
  overflow: hidden;
  height: 4em;
  padding: 0;
  position: absolute;
  z-index: -1;
  margin: 0;
  color: red;
  left: 100%;
  transition-delay: 0.3s;
  transition-property: transform;
  transition-duration: 0.6s;
  -webkit-transform: translateX(-100%);
  transform: translateX(-100%);
  width: auto;
  list-style: none;
  white-space: nowrap;
  top: 0;
  background: #dc3278;
}
ul.sidebar > li > ul > li {
  overflow: hidden;
  background: none;
  margin: 0 0px 0 -4px;
  padding: 0;
  display: inline-block;
  width: 6.5em;
  height: 4em;
  transition: background 0.3s;
}
ul.sidebar > li > ul > li:hover {
  background: #fa648c;
}
ul.sidebar li:hover > ul {
  -webkit-transform: translateX(0);
  transform: translateX(0);
}
ul.sidebar li:hover > ul > li {
  color: white;
}

body {
  padding: 0;
  position: fixed;
  margin: 0;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: #450f30;
}

description {
  position: absolute;
  bottom: 0;
  right: 0;
  padding: 0 0.4em 1em 0;
  color: white;
  font-size: 20px;
  font-family: times new roman;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<description>
  Full Stack Developer
</description>



<ul class="sidebar">  
  <li>
    <a href="#">
      <i class="fa fa-home"></i> 
      <span>
    My Projects
      </span>
     
    </a>
    <ul>
       <li>
        <a>
          <i class="fa fa-question-circle"></i>
          <span>Nothing</span>
        </a>
        
      </li>
      <li>
        <a>
          <i class="fa fa-square"></i>
          <span>Boxes for all</span>
        </a>
      </li>
      <li>
        <a>
          <i class="fa fa-th-large"></i>
          <span>More boxes</span>
        </a>
      </li>
    </ul>
  </li>
  <li>
    <a>
      <i class="fa fa-info-circle"></i>
      <span>
      About
      </span>
    </a>
    
    <ul>
      <li>
        <a>
          <i class="fa fa-eye"></i>
          <span>I have eyes</span>
        </a>
        
      </li>
      <li>
        <a>
          <i class="fa fa-bed"></i>
          <span>and a bed</span>
        </a>
      </li>
      <li>
        <a>
          <i class="fa fa-heartbeat"></i>
          <span>plus cool icons</span>
        </a>
      </li>
     </ul>
  </li>
  <li>
    <a>
      <i class="fa fa-globe">
      </i>
      <span>
        Links
      </span>
    </a>
    <ul>
   <li>
        <a>
          <i class="fa fa-external-link"></i>
          <span>No Links</span>
        </a>
      </li>
      <li>
        <a>
          <i class="fa fa-fire"></i>
          <span>For the wicked</span>
        </a>
      </li>
     </ul>
  </li>
  <li>
  <a>
    <i class="fa fa-envelope-o"></i>
    <span>
    Contact
    </span>
  </a>
    <ul>
      <li>
        <a href="mailto:alec.menke@gmail.com">
          <i class="fa fa-user"></i>
          <span style="font-size: 8px;">alec.menke@gmail.com</span>
        </a>
        </li>
    </ul>
</li>
    </ul>

Code Pen

【问题讨论】:

  • 看起来 Firefox 无法识别,在翻译之后,光标仍然悬停。如果添加 ul.sidebar 会发生什么 li:hover > ul:hover { transform: translateX(0); }?

标签: css cross-browser hover selector compatibility


【解决方案1】:

问题不在于转换和转换的供应商前缀。问题是z-index: -1 属性。根据定义,具有绝对位置的元素应位于所有内联或相对定位的元素之上。 -1 的技巧起作用并将其放置在具有相对位置的父菜单元素下方。但是,在 Firefox 和 IE 中,它甚至位于正文下方,因此当鼠标悬停在下拉菜单上时,实际上是在正文上悬停,即超出父菜单 &lt;li&gt; 元素。

为了解决这个问题,我进行了更改:

ul.sidebar &gt; li &gt; a类中将z-index改为1,并在ul.sidebar &gt; li &gt; a类中添加了以下属性:

  position: absolute;
  left: 0;
  background: #aa2266;
  z-index: 1;  

我在 Firefox 和 Internet Explorer 上检查了结果,现在可以正常工作了 :)

ul.sidebar {
  display: block;
  position: absolute;
  margin: 0;
  left: 0;
  height: 100%;
  width: 6.5em;
  background: #aa2266;
  padding: 0;
  text-align: center;
}
ul.sidebar * {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
ul.sidebar a {
  color: white;
  text-decoration: none;
  margin: 0;
  display: inline-block;
  width: 100%;
  height: 100%;
}
ul.sidebar a > i {
  margin-top: 0.13333em;
  font-size: 2em;
  display: block;
  margin-bottom: 0.2em;
}
ul.sidebar a > span {
  font-size: 0.8em;
  font-family: sans-serif;
}
ul.sidebar li {
  transition: background 0.3s;
  background: #aa2266;
}
ul.sidebar > li {
  margin: 0;
  padding: 0;
  height: 4em;
  display: inline-block;
  position: relative;
  width: 100%;
  background: #aa2266;
  transition: background 0.3s;
}
ul.sidebar > li:hover {
  background: #dc3278;
}
ul.sidebar > li:hover:after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 100%;
  width: 6px;
  background: linear-gradient(to right, rgba(0, 0, 0, 0.25) 0%, transparent 100%);
}
ul.sidebar > li > a {
  position: absolute;
  left: 0;
  background: #aa2266;
  z-index: 1;  
  transition: background 0.3s;
}
ul.sidebar > li > a:hover {
  background: #ff5f87;
}
ul.sidebar > li > ul {
  overflow: hidden;
  height: 4em;
  padding: 0;
  position: absolute;
  z-index: 0;
  margin: 0;
  color: red;
  left: 100%;
  transition-delay: 0.3s;
  transition-property: transform;
  transition-duration: 0.6s;
  -webkit-transform: translateX(-100%);
  transform: translateX(-100%);
  width: auto;
  list-style: none;
  white-space: nowrap;
  top: 0;
  background: #dc3278;
}
ul.sidebar > li > ul > li {
  overflow: hidden;
  background: none;
  margin: 0 0px 0 -4px;
  padding: 0;
  display: inline-block;
  width: 6.5em;
  height: 4em;
  transition: background 0.3s;
}
ul.sidebar > li > ul > li:hover {
  background: #fa648c;
}
ul.sidebar li:hover > ul {
  -webkit-transform: translateX(0);
  transform: translateX(0);
}
ul.sidebar li:hover > ul > li {
  color: white;
}

body {
  padding: 0;
  position: fixed;
  margin: 0;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: #450f30;
}

description {
  position: absolute;
  bottom: 0;
  right: 0;
  padding: 0 0.4em 1em 0;
  color: white;
  font-size: 20px;
  font-family: times new roman;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<description>
  Full Stack Developer
</description>



<ul class="sidebar">  
  <li>
    <a href="#">
      <i class="fa fa-home"></i> 
      <span>
    My Projects
      </span>
     
    </a>
    <ul>
       <li>
        <a>
          <i class="fa fa-question-circle"></i>
          <span>Nothing</span>
        </a>
        
      </li>
      <li>
        <a>
          <i class="fa fa-square"></i>
          <span>Boxes for all</span>
        </a>
      </li>
      <li>
        <a>
          <i class="fa fa-th-large"></i>
          <span>More boxes</span>
        </a>
      </li>
    </ul>
  </li>
  <li>
    <a>
      <i class="fa fa-info-circle"></i>
      <span>
      About
      </span>
    </a>
    
    <ul>
      <li>
        <a>
          <i class="fa fa-eye"></i>
          <span>I have eyes</span>
        </a>
        
      </li>
      <li>
        <a>
          <i class="fa fa-bed"></i>
          <span>and a bed</span>
        </a>
      </li>
      <li>
        <a>
          <i class="fa fa-heartbeat"></i>
          <span>plus cool icons</span>
        </a>
      </li>
     </ul>
  </li>
  <li>
    <a>
      <i class="fa fa-globe">
      </i>
      <span>
        Links
      </span>
    </a>
    <ul>
   <li>
        <a>
          <i class="fa fa-external-link"></i>
          <span>No Links</span>
        </a>
      </li>
      <li>
        <a>
          <i class="fa fa-fire"></i>
          <span>For the wicked</span>
        </a>
      </li>
     </ul>
  </li>
  <li>
  <a>
    <i class="fa fa-envelope-o"></i>
    <span>
    Contact
    </span>
  </a>
    <ul>
      <li>
        <a href="mailto:alec.menke@gmail.com">
          <i class="fa fa-user"></i>
          <span style="font-size: 8px;">alec.menke@gmail.com</span>
        </a>
        </li>
    </ul>
</li>
    </ul>

【讨论】:

  • 直到我更新了我的 codepen 我才意识到这一点,但是你的代码删除了我在滑块中内置的透明度效果。因此,为了保持这种效果,我不得不将主体的位置更改为“静态”并保持其余代码相同。尽管您的回答并没有完全解决我的所有问题,但它仍然解释了我做错了什么并且非常有帮助。感谢您的回复。
  • 当然没有人能比你自己更好地修复你的代码。我只是帮你指出问题:)
猜你喜欢
  • 2016-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-10
  • 1970-01-01
  • 2012-03-06
  • 2016-09-15
  • 2013-06-26
相关资源
最近更新 更多