如何将选定的divs
(与.stayhovered
配对)在悬停在div
保持可见时触发,直到该人悬停在另一个div
上? / p>
jsfiddle此处。
提前谢谢!
HTML
<div class="show" id="subject01">
<h1>Subject 01</h1>
<div class="targetDiv info01">
<div class="topleft stayhovered">
<b>Subject 01, topleft<br/><u>Maintain hover</u> until subject02 is hovered</b></div>
<div class="topright">
Subject 01, topright<br/>Fadeout when cursor hovers out</div>
<div class="bottomleft stayhovered">
<b>Subject 01, bottomleft<br/><u>Maintain hover</u> until subject02 is hovered</b></div>
<div class="bottomright">
Subject 01, bottomright<br/>Fadeout when cursor hovers out</div>
</div> <!-- END | 01 target -->
</div> <!-- END | 01 show -->
<div class="show" id="subject02">
<h1>Subject 02</h1>
<div class="targetDiv info02">
<div class="topleft">
Subject 02, topleft<br/>Fadeout when cursor hovers out</div>
<div class="topright stayhovered">
<b>Subject 02, topright<br/><u>Maintain hover</u> until subject01 is hovered</b></div>
<div class="bottomleft">
Subject 02, bottomleft<br/>Fadeout when cursor hovers out</div>
<div class="bottomright stayhovered">
<b>Subject 02, bottomright<br/><u>Maintain hover</u> until subject01 is hovered</b></div>
</div> <!-- END | 02 target -->
</div> <!-- END | 02 show -->
CSS
/* hoverover text placement */
body {
font-family: helvetica;
font-size: 13px;
background-color: #FAFAFA;
}
.topleft, .topright, .bottomleft, .bottomright {
position: fixed;
}
.topleft {
top:2%;
left:2%;
}
.topright {
top:2%;
right:2%;
}
.bottomleft {
bottom:2%;
left:2%;
}
.bottomright {
bottom:2%;
right:2%;
}
/* Feature Typography */
.featuretitle, .featureinfo, .rotate {
}
.featuretitle {
}
.featureinfo {
}
/* FEATURE DETAIL */
.featuredetail {
font-family:'Courier New', Courier, monospace;
font-weight: normal;
letter-spacing: 1px;
text-align: left;
text-transform: uppercase;
font-size: 11px;
line-height: 18px;
overflow-x: hidden;
position: fixed;
bottom: 20px;
left: 20px;
width: 50%
}
/* Subjects */
#subject01 {
left: 30%;
top: 30%;
position: fixed;
color: #FE5722;
}
#subject02 {
right: 30%;
bottom: 30%;
position: fixed;
color: #3F51B5;
}
/* Feature */
.feature-img-wrap {
display:block;
}
*, *:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.targetDiv {
position:absolute;
width:100%;
visibility: hidden;
opacity:0;
-webkit-transition: all 250ms linear;
-o-transition: all 250ms linear;
transition: all 250ms linear;
}
.show:hover .targetDiv {
visibility: visible;
opacity:1;
}
.show:hover .stayhovered {
visibility: visible;
opacity:1;
}
答案 0 :(得分:3)
这是一项挑战,但我认为我已经掌握了它。
这里是小提琴: http://jsfiddle.net/n810v1se/
...以及代码段:
body {
font-family: helvetica;
font-size: 13px;
background-color: #FAFAFA;
}
.topleft {
top:2%;
left:2%;
}
.topright {
top:2%;
right:2%;
}
.bottomleft {
bottom:2%;
left:2%;
}
.bottomright {
bottom:2%;
right:2%;
}
/* Subjects */
#subject01 {
left: 30%;
top: 20%;
position: fixed;
color: #FE5722;
}
#subject02 {
right: 30%;
bottom: 30%;
position: fixed;
color: #3F51B5;
}
.targetDiv {
position:absolute;
width:100%;
visibility: hidden;
opacity:0;
-webkit-transition: all 250ms linear;
-o-transition: all 250ms linear;
transition: all 250ms linear;
}
.topleft, .topright, .bottomleft, .bottomright {
position: fixed;
opacity: 0;
-webkit-transition: all 250ms linear;
-o-transition: all 250ms linear;
transition: all 250ms linear;
}
.stayhovered {
opacity: 1;
}
h1:hover ~ div .topleft,
h1:hover ~ div .topright,
h1:hover ~ div .bottomleft,
h1:hover ~ div .bottomright
{
visibility: visible;
opacity: 1;
}
.show:hover .targetDiv {
visibility: visible;
position: fixed;
left: 0px;
top: 0px;
height: 100%;
opacity: 1;
z-index: -1;
}
.show:hover {
z-index: 0;
}
.show {
z-index: 1;
}
&#13;
<div class="show" id="subject01">
<h1>Subject 01</h1>
<div class="targetDiv info01">
<div class="topleft stayhovered">
<b>Subject 01, topleft<br/><u>Maintain hover</u> until subject02 is hovered</b></div>
<div class="topright">
Subject 01, topright<br/>Fadeout when cursor hovers out</div>
<div class="bottomleft stayhovered">
<b>Subject 01, bottomleft<br/><u>Maintain hover</u> until subject02 is hovered</b></div>
<div class="bottomright">
Subject 01, bottomright<br/>Fadeout when cursor hovers out</div>
</div> <!-- END | 01 target -->
</div> <!-- END | 01 show -->
<div class="show" id="subject02">
<h1>Subject 02</h1>
<div class="targetDiv info02">
<div class="topleft">
Subject 02, topleft<br/>Fadeout when cursor hovers out</div>
<div class="topright stayhovered">
<b>Subject 02, topright<br/><u>Maintain hover</u> until subject01 is hovered</b></div>
<div class="bottomleft">
Subject 02, bottomleft<br/>Fadeout when cursor hovers out</div>
<div class="bottomright stayhovered">
<b>Subject 02, bottomright<br/><u>Maintain hover</u> until subject01 is hovered</b></div>
</div> <!-- END | 02 target -->
</div> <!-- END | 02 show -->
&#13;
工作原理
角元素在targetDiv
范围内,默认情况下隐藏。将鼠标悬停在其父级targetDiv
类元素上时显示show
非常容易:
.show:hover .targetDiv {visibility: visible;}
即使show
- 类元素中的内容到达视口的每个角落,这些元素的实际宽度和高度也是其中h1
元素的宽度和高度。为什么?因为他们的角落元素有固定的定位。 (要查看此内容,请为show
类添加边框。)
这意味着只有h1
中的show
元素才会响应hover
事件。
当鼠标离开h1
元素时,hover
事件将被取消,导致targetDiv
消失。但是,在其他 targetDiv
元素悬停之前,我们希望h1
保持可见。
我们通过在targetDiv
悬停时show
占据整个屏幕来实现这一目标:
.show:hover .targetDiv {
...
position: fixed;
left: 0px;
top: 0px;
height: 100%;
}
(targetDiv
已经有100%的宽度,所以这里不需要。)
现在,当show
悬停时,其targetDiv
个孩子将占据整个视口,导致其停留。
从subject01
移动到subject02
时效果很好。但现在无法从subject02
subject01
移出subject02
。为什么?因为当show
的{{1}}父项悬停时,其内容会占用整个视口。由于subject02
在 subject01
之后被声明为,因此其父级基本上位于 subject01
之上,因此无法subject01
接收任何鼠标事件。
解决方案?为show
类提供一个默认的z-index为1.当它悬停时,将其z-index更改为0.这样,它总是落后于另一个show
- class element:
.show {z-index: 1;}
.show:hover {z-index: 0;}
现在面临的挑战是保持stayhovered
元素可见而其他角元素不可见。我们通过将opacity:1
添加到stayhovered
的默认样式,并将角落元素更改为opacity:1
,只有当他们之前的h1
兄弟徘徊时才会这样做:
.stayhovered {opacity: 1;}
h1:hover ~ div .topleft,h1:hover ~ div .topright,
h1:hover ~ div .bottomleft,h1:hover ~ div .bottomright {
opacity: 1;
}
这似乎一切都很好。 然而,现在targetDiv
占用了整个视口,并且它具有固定的定位,它基本上覆盖了h1
元素,使h1
成为不可能收到悬停事件!
解决方案?将否定z-index
添加到targetDiv
:
.show:hover .targetDiv {
...
z-index: -1;
}
这就是最后一块拼图。
答案 1 :(得分:0)
您可以在不改变css的情况下添加此javascript / jquery代码:
jsfiddle example http://jsfiddle.net/j9vtjk6j/1/
//on hover function for #subject01 function
$("#subject01").hover(function(){
//make the .targetDiv visible
$(this).find(".targetDiv").css("visibility","visible");
//make this .targetDiv visible
$(this).find(".targetDiv").css("opacity",1);
//make the #subject02 .targetDiv invisible
$("#subject02 .targetDiv").css("opacity",0);
});
//#subject02 hover function
$("#subject02").hover(function(){
//make the .targetDiv visible
$(this).find(".targetDiv").css("visibility","visible");
//make this .targetDiv visible
$(this).find(".targetDiv").css("opacity",1);
//make the #subject01 .targetDiv visible
$("#subject01 .targetDiv").css("opacity",0);
});