2017-04-08 31 views
3

我有一個名爲hoverZone的div,另一個叫followMouseThe followMouse格,顧名思義始終跟隨鼠標移動,問題是,在我的CSS我有這樣的規則,從來沒有得到應用:懸停事件不跟隨鼠標跟一個div

.hoverZone:hover ~ .followMouse { 
box-shadow: 0px 0px 30px #fff; 
} 

如何繞過這個問題的任何想法?

window.addEventListener("mousemove", move, false); 
 

 
function move(e){ 
 
    var mouseX = parseInt(e.clientX); 
 
    var mouseY = parseInt(e.clientY); 
 
     
 
    var follower = document.querySelector(".followMouse"); 
 
    follower.style.left = mouseX + "px"; 
 
    follower.style.top = mouseY + "px"; 
 
}
.hoverZone { 
 
    display: block; 
 
    height: 90px; 
 
    width: 90px; 
 
    position: absolute; 
 
} 
 

 
.hoverZone:hover ~ .followMouse { 
 
    background-color: blue; 
 
    box-shadow: 0px 0px 30px #fff; 
 
} 
 

 
.followMouse{ 
 
    width: 90px; 
 
    height: 90px; 
 
    background-color: orange; 
 
}
<div class="hoverZone"></div> 
 
<div class="followMouse"></div>

回答

2

.followMouseposition:static;默認所以更改topleft和等沒有元素上的任何影響。給position: absolute;.followMouse將解決您的問題。

window.addEventListener("mousemove", move, false); 
 

 
function move(e){ 
 
    var mouseX = parseInt(e.clientX); 
 
    var mouseY = parseInt(e.clientY); 
 
     
 
    var follower = document.querySelector(".followMouse"); 
 
    follower.style.left = mouseX + "px"; 
 
    follower.style.top = mouseY + "px"; 
 
}
.hoverZone { 
 
    display: block; 
 
    height: 90px; 
 
    width: 90px; 
 
    position: absolute; 
 
} 
 

 
.hoverZone:hover ~ .followMouse { 
 
    background-color: blue; 
 
    box-shadow: 0px 0px 30px #000; 
 
} 
 

 
.followMouse{ 
 
    width: 90px; 
 
    height: 90px; 
 
    background-color: orange; 
 
    position:fixed; 
 
}
<div class="hoverZone"></div> 
 
<div class="followMouse"></div>

+0

您好!謝謝你的回答,我忘了把它放在我的代碼上面,但'followMouse'的位置是固定的('position:fixed;'),但爲什麼它必須是絕對的? – Safirah

+0

沒關係,你可以給'postion:fixed'。 – Alex

+1

你可以給followMouse類的css規則'pointer-events:none'來阻止它從hoverZone盜取事件 –