2016-06-21 68 views
1

我有一个使用悬停状态的容器。每当我将鼠标悬停在上面时,背景都会变为不同的颜色。将鼠标悬停在背景中并不适用于绝对位置

另外我使用了一个导航,我用postion显示:绝对在容器上。每次我将鼠标悬停在导航上时,背景更改都不起作用,因为我没有在后台扫描容器。

有没有解决方法?

我的代码(悬停在1):

.wrapper {position: relative;} 
 
.container {background: red; width: 100%; height:200px;} 
 
.container:hover {background: blue;} 
 
.nav {position:absolute; top:50%;margin: 0 auto; width:100%; text-align:center;}
<div class="wrapper"> 
 
    <div class="container"> 
 
    
 
    </div> 
 
    <div class="nav"> 
 
    <a href="#">1</a> 
 
    </div> 
 
</div>

+0

这是很难理解你想要什么这里! –

回答

1

移动导航栏容器内:

<div class="wrapper"> 
    <div class="container"> 
     <div class="nav"> 
      <a href="#">1</a> 
     </div> 
    </div> 
</div> 

它是position: absolute所以内部或外部是不一个问题。


或者使用JavaScript:

<div class="wrapper"> 
    <div class="container" onmouseover="change()" onmouseout="changeBack()"> 

    </div> 
    <div class="nav" onmouseover="change()"> 
     <a href="#">1</a> 
    </div> 
</div> 

<script> 
    window.change = function() { 
     console.log("hello"); 
     document.getElementsByClassName("container")[1].style.background = "blue"; 
    } 

    window.changeBack = function() { 
     console.log("hello"); 
     document.getElementsByClassName("container")[1].style.background = "red"; 
    } 
</script> 

小提琴:https://jsfiddle.net/3h1orsmu/1/

+0

不幸的是,这是不可能的项目:-( – Cray

+0

编辑答案包括JavaScript解决方案 – theblindprophet

1

使用包装的悬停状态,但目标容器:

.wrapper:hover .container{ 
    background-color:blue; 
} 
+0

对不起,这是不可能在项目中,因为包装中有更多的容器应该不会受到影响,并且没有办法选择一个:-( – Cray

+0

)您可以向元素添加一个新类,以便它只会影响一个实例,然后只需使用上面的代码作为目标(将清除“可以和不可以完成”) - 因为你必须回答哪些问题可以解决问题。) – Birksy89