查看随附的jsfiddle:
http://jsfiddle.net/muncherelli/dLsgokva/
我有三个不同的链接:相关文本链接,相关图像链接和不相关的文本链接。相关链接转到一个网址,不相关的链接转到另一个网址。
如果您当前将鼠标悬停在单个三个元素上,则每个元素都将自行悬停。我希望相关图像和相关链接都可以在其中任何一个悬停时进入“悬停”状态。当您将鼠标悬停在除了不相关的链接之外的任何其他内容时,不相关的链接不应悬停。是否有原生的CSS方式来做到这一点?我也有jQuery可供使用。
请注意,一个页面上会有多个这样的容器,每个容器都有不同的URL。他们需要能够独立工作,因为他们会去不同的链接。
这是我的HTML:
<div class="container rounded-bg">
<a href="#related-url"><img src="http://placehold.it/400x300?text=.related-img" class="related-img effects rounded" />
<div class="content">
<div class="content-top">
<a href="#unrelated-url" class="unrelated effects">Unrelated Link</a>
</div>
<div class="content-bottom">
<a href="#related-url" class="related effects"><h1>Related Link</h1></a>
</div>
</div>
</div>
<br /><br />
<div class="container rounded-bg">
<a href="#related-url"><img src="http://placehold.it/400x300?text=.related-img" class="related-img effects rounded" />
<div class="content">
<div class="content-top">
<a href="#unrelated-url" class="unrelated effects">Unrelated Link</a>
</div>
<div class="content-bottom">
<a href="#related-url" class="related effects"><h1>Related Link</h1></a>
</div>
</div>
</div>
<br /><br />
<div class="container rounded-bg">
<a href="#related-url"><img src="http://placehold.it/400x300?text=.related-img" class="related-img effects rounded" />
<div class="content">
<div class="content-top">
<a href="#unrelated-url" class="unrelated effects">Unrelated Link</a>
</div>
<div class="content-bottom">
<a href="#related-url" class="related effects"><h1>Related Link</h1></a>
</div>
</div>
</div>
<br /><br />
<div class="container rounded-bg">
<a href="#related-url"><img src="http://placehold.it/400x300?text=.related-img" class="related-img effects rounded" />
<div class="content">
<div class="content-top">
<a href="#unrelated-url" class="unrelated effects">Unrelated Link</a>
</div>
<div class="content-bottom">
<a href="#related-url" class="related effects"><h1>Related Link</h1></a>
</div>
</div>
</div>
我的CSS:
body {
font-family: sans-serif;
}
div.container {
position: relative;
background-color: #000;
display: inline-block;
height: 300px;
width: 400px;
}
div.content {
height: 100%;
width: 100%;
}
div.content-top {
position: absolute;
right: 20px;
top: 20px;
}
div.content-top {
position: absolute;
right: 20px;
top: 20px;
}
div.content-bottom {
position: absolute;
left: 20px;
bottom: 0;
}
.related-img:hover {
opacity: .9;
}
a.unrelated {
background-color: #ccc;
background-color: rgba(0,0,0,0.4);
color: #fff;
font-size: 13px;
padding: 5px 15px;
text-transform: uppercase;
}
a.unrelated:hover {
background-color: rgba(0,0,0,0.7);
}
a.related {
text-decoration: none;
color: #999;
}
a.unrelated {
text-decoration: none;
}
a.related:hover {
color: #000;
}
.effects {
-moz-transition: background-color 500ms, opacity 500ms, color 500ms ease-out;
-ms-transition: background-color 500ms, opacity 500ms, color 500ms ease-out;
-o-transition: background-color 500ms, opacity 500ms, color 500ms ease-out;
-webkit-transform: translateZ(0px);
-webkit-transition: background-color 500ms, color 500ms ease-out;
transition: background-color 500ms, opacity 500ms, color 500ms ease-out;
}
.rounded {
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
}
.rounded-bg {
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}
答案 0 :(得分:1)
我不确定它是否可以在纯CSS中使用,因为CSS没有父选择器。假设您的标记可以接受jQuery解决方案,这将有效:(请注意对CSS .related-img.active
和.related.active
的更改
$('.container').on('mouseenter', '.related, .related-img', function(){
$link = $(this).closest('.container').find('.related, .related-img').addClass('active');
});
$('.container').on('mouseleave', '.related, .related-img', function(){
$link = $(this).closest('.container').find('.related, .related-img').removeClass('active');
});
body {
font-family: sans-serif;
}
div.container {
position: relative;
background-color: #000;
display: inline-block;
height: 300px;
width: 400px;
}
div.content {
height: 100%;
width: 100%;
}
div.content-top {
position: absolute;
right: 20px;
top: 20px;
}
div.content-top {
position: absolute;
right: 20px;
top: 20px;
}
div.content-bottom {
position: absolute;
left: 20px;
bottom: 0;
}
.related-img.active{
opacity: .9;
}
a.unrelated {
background-color: #ccc;
background-color: rgba(0,0,0,0.4);
color: #fff;
font-size: 13px;
padding: 5px 15px;
text-transform: uppercase;
}
a.unrelated:hover {
background-color: rgba(0,0,0,0.7);
}
a.related {
text-decoration: none;
color: #999;
}
a.unrelated {
text-decoration: none;
}
a.related.active{
color: #000;
}
.effects {
-moz-transition: background-color 500ms, opacity 500ms, color 500ms ease-out;
-ms-transition: background-color 500ms, opacity 500ms, color 500ms ease-out;
-o-transition: background-color 500ms, opacity 500ms, color 500ms ease-out;
-webkit-transform: translateZ(0px);
-webkit-transition: background-color 500ms, color 500ms ease-out;
transition: background-color 500ms, opacity 500ms, color 500ms ease-out;
}
.rounded {
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
}
.rounded-bg {
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container rounded-bg">
<a href="#related-url"><img src="http://placehold.it/400x300?text=.related-img" class="related-img effects rounded" />
<div class="content">
<div class="content-top">
<a href="#unrelated-url" class="unrelated effects">Unrelated Link</a>
</div>
<div class="content-bottom">
<a href="#related-url" class="related effects"><h1>Related Link</h1></a>
</div>
</div>
</div>
<br /><br />
<div class="container rounded-bg">
<a href="#related-url"><img src="http://placehold.it/400x300?text=.related-img" class="related-img effects rounded" />
<div class="content">
<div class="content-top">
<a href="#unrelated-url" class="unrelated effects">Unrelated Link</a>
</div>
<div class="content-bottom">
<a href="#related-url" class="related effects"><h1>Related Link</h1></a>
</div>
</div>
</div>
<br /><br />
<div class="container rounded-bg">
<a href="#related-url"><img src="http://placehold.it/400x300?text=.related-img" class="related-img effects rounded" />
<div class="content">
<div class="content-top">
<a href="#unrelated-url" class="unrelated effects">Unrelated Link</a>
</div>
<div class="content-bottom">
<a href="#related-url" class="related effects"><h1>Related Link</h1></a>
</div>
</div>
</div>
<br /><br />
<div class="container rounded-bg">
<a href="#related-url"><img src="http://placehold.it/400x300?text=.related-img" class="related-img effects rounded" />
<div class="content">
<div class="content-top">
<a href="#unrelated-url" class="unrelated effects">Unrelated Link</a>
</div>
<div class="content-bottom">
<a href="#related-url" class="related effects"><h1>Related Link</h1></a>
</div>
</div>
</div>