Css悬停触发完全随机的元素

时间:2017-08-18 07:51:08

标签: html css

我正在制作一个嵌套的html菜单。由于某种原因,在实际悬停在正确的元素上之前,悬停会保持触发方式。就好像另一个div或元素在手前触发它一样。

我认为这与我的HTML结构以及我在css中调用悬停的方式有关。

请参阅下面的摘要 -

/*CAR ASSISTANT STYLES*/

.car-assist-container {
	position: relative;
    float: left;
    width: 210px;
}

.assist-label {
    display: block;
    font-size: 1em;
    float: left;
    padding: 7px;
    text-transform: uppercase;
    color: black;
}

.assist-label:hover > .assist-list {
  opacity: 1;
  height: auto;
}

.assist-list {
  width: 100px;
  background-color: #141825;
  height: 0;
  opacity: 0;
  -moz-transition: all 0.4s ease;
  -webkit-transition: all 0.4s ease;
  -o-transition: all 0.4s ease;
  transition: all 0.4s ease;
  display: block;
}

.assist-item{
    color: white;
    display: block;
    width: 100%;
    padding: 8px;
    border-bottom: 1px solid #1d2435;
    font-family: 'Montserrat', sans-serif;
    font-weight: 100;
    cursor: pointer;
    -webkit-transition: color 0.4s ease;
    -moz-transition: color 0.4s ease;
    -o-transition: color 0.4s ease;
    transition: ocolor 0.4s ease;
}

.assist-item:hover {
	color: #d40000;
}

.browse:after {
	content: '▸';
}

.browse:hover > .browse-list{
	opacity: 1;
}

.browse-list{
    position: absolute;
    right: 0;
    width: 100px;
    background-color: #1d2435;
    top: 0;
    opacity: 0;
    -webkit-transition: opacity 0.4s ease;
    -moz-transition: opacity 0.4s ease;
    -o-transition: opacity 0.4s ease;
    transition: opacity 0.4s ease;
    z-index: -1;
}

.browse-item{
    color: white;
    display: block;
    width: 100%;
    padding: 8px;
    border-bottom: 1px solid #141825;
    font-family: 'Montserrat', sans-serif;
    font-weight: 100;
}

.browse-item:hover {
	color: #d40000;
}
<div class="car-assist-container">
  <a href="#" class="assist-label" id="label">Car Assistant
              <ul class="assist-list">
                <li class="assist-item" id="carWorth">Car Worth</li>
                <li class="assist-item browse">
                  Browse
                  <ul class="browse-list">
                    <li class="browse-item" id="browseMake">Makes</li>
                    <li class="browse-item" id="browseType">Types</li>
                    <li class="browse-item" id="browseYear">Year</li>
                  </ul>
                </li>
                <li class="assist-item">Compare</li>
              </ul>
          </a>
</div>

3 个答案:

答案 0 :(得分:1)

这可能是您想要的行为,代码更改最少。

/*CAR ASSISTANT STYLES*/

.car-assist-container {
	position: relative;
    float: left;
    width: 210px;
}

.assist-label {
    display: block;
    font-size: 1em;
    float: left;
    padding: 7px;
    text-transform: uppercase;
    color: black;
}

.assist-label:hover > .assist-list {
  opacity: 1;
  height: auto;
}

.assist-list {
  width: 100px;
  background-color: #141825;
  height: 0;
  opacity: 0;
  -moz-transition: all 0.4s ease;
  -webkit-transition: all 0.4s ease;
  -o-transition: all 0.4s ease;
  transition: all 0.4s ease;
  display: block;

overflow: hidden;
margin: 0;
}

.assist-item{
    color: white;
    display: block;
    width: 100%;
    padding: 8px;
    border-bottom: 1px solid #1d2435;
    font-family: 'Montserrat', sans-serif;
    font-weight: 100;
    cursor: pointer;
    -webkit-transition: color 0.4s ease;
    -moz-transition: color 0.4s ease;
    -o-transition: color 0.4s ease;
    transition: ocolor 0.4s ease;
}

.assist-item:hover {
	color: #d40000;
}

.browse:after {
	content: '▸';
}

.browse:hover > .browse-list{
	opacity: 1;
    height: auto;

}

.browse-list{
    position: absolute;
    right: 0;
    width: 100px;
    background-color: #1d2435;
    top: 0;
    opacity: 0;
    -webkit-transition: opacity 0.4s ease;
    -moz-transition: opacity 0.4s ease;
    -o-transition: opacity 0.4s ease;
    transition: opacity 0.4s ease;
    z-index: 1;
    height: 0;
    overflow: hidden;
}

.browse-item{
    color: white;
    display: block;
    width: 100%;
    padding: 8px;
    border-bottom: 1px solid #141825;
    font-family: 'Montserrat', sans-serif;
    font-weight: 100;
}

.browse-item:hover {
	color: #d40000;
}
<div class="car-assist-container">
  <a href="#" class="assist-label" id="label">Car Assistant
              <ul class="assist-list">
                <li class="assist-item" id="carWorth">Car Worth</li>
                <li class="assist-item browse">
                  Browse
                  <ul class="browse-list">
                    <li class="browse-item" id="browseMake">Makes</li>
                    <li class="browse-item" id="browseType">Types</li>
                    <li class="browse-item" id="browseYear">Year</li>
                  </ul>
                </li>
                <li class="assist-item">Compare</li>
              </ul>
          </a>
</div>

答案 1 :(得分:0)

这是因为您的.browse-list div位于.assist-label的顶部,因此当您将鼠标悬停在.assist-label上时,您实际上是在.browse-list上方悬停。

答案 2 :(得分:0)

你为不透明度设置了动画,我已将它从display:block;显示:无;我们现在正在悬停时切换这些值。似乎现在在合适的时刻触发。但是你的子菜单仍然无法正常工作,但我没有调查,因为那不是问题; - )

&#13;
&#13;
/*CAR ASSISTANT STYLES*/

.car-assist-container {
	position: relative;
    float: left;
    width: 210px;
}

.assist-label {
    display: block;
    font-size: 1em;
    float: left;
    padding: 7px;
    text-transform: uppercase;
    color: black;
}

.assist-label:hover > .assist-list {
  opacity: 1;
  height: auto;
display:block;
}

.assist-list {
  width: 100px;
  background-color: #141825;
  height: 0;
  opacity: 0;
  -moz-transition: all 0.4s ease;
  -webkit-transition: all 0.4s ease;
  -o-transition: all 0.4s ease;
  transition: all 0.4s ease;
  display: none;
}

.assist-item{
    color: white;
    display: block;
    width: 100%;
    padding: 8px;
    border-bottom: 1px solid #1d2435;
    font-family: 'Montserrat', sans-serif;
    font-weight: 100;
    cursor: pointer;
    -webkit-transition: color 0.4s ease;
    -moz-transition: color 0.4s ease;
    -o-transition: color 0.4s ease;
    transition: ocolor 0.4s ease;
}

.assist-item:hover {
	color: #d40000;
}

.browse:after {
	content: '▸';
}

.browse:hover > .browse-list{
	opacity: 1;
    display:block;
}

.browse-list{
    position: absolute;
    right: 0;
    width: 100px;
    background-color: #1d2435;
    top: 0;
    opacity: 0;
    display:none;
    -webkit-transition: opacity 0.4s ease;
    -moz-transition: opacity 0.4s ease;
    -o-transition: opacity 0.4s ease;
    transition: opacity 0.4s ease;
    z-index: -1;
}

.browse-item{
    color: white;
    display: block;
    width: 100%;
    padding: 8px;
    border-bottom: 1px solid #141825;
    font-family: 'Montserrat', sans-serif;
    font-weight: 100;
}

.browse-item:hover {
	color: #d40000;
}
&#13;
<div class="car-assist-container">
  <a href="#" class="assist-label" id="label">Car Assistant
              <ul class="assist-list">
                <li class="assist-item" id="carWorth">Car Worth</li>
                <li class="assist-item browse">
                  Browse
                  <ul class="browse-list">
                    <li class="browse-item" id="browseMake">Makes</li>
                    <li class="browse-item" id="browseType">Types</li>
                    <li class="browse-item" id="browseYear">Year</li>
                  </ul>
                </li>
                <li class="assist-item">Compare</li>
              </ul>
          </a>
</div>
&#13;
&#13;
&#13;