我有以下HTML:
<div class="post-box">
<a href='<?php the_permalink() ?>'>
<span class='overlay'></span>
<div class='post-img' style="background-image:url('<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)) ?>');"></div>
</a>
<a class='post-title' href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<p><?php the_excerpt(); ?></p>
<a class='read-more' href="<?php the_permalink(); ?>">Read more</a>
</div>
这是CSS
,会在悬停时导致图像叠加:
.post-img{
height:300px;
width: 100%;
background-size:cover;
transition : opacity 200ms ease-out;
-webkit-transition : opacity 200ms ease-out;
-moz-transition : opacity 200ms ease-out;
-o-transition : opacity 200ms ease-out;
}
.post-img:hover{
opacity:0.5;
}
span.overlay {
background-color: #f39300;
cursor: pointer;
height: 300px;
width: 585px;
position: absolute;
left: 0;
z-index: 10;
opacity: 0;
}
span.overlay:hover {
opacity: .5;
transition: opacity 200ms ease-in;
-webkit-transition: opacity 200ms ease-in;
-moz-transition: opacity 200ms ease-in;
-o-transition: opacity 200ms ease-in;
}
以上所有情况都很好,我很满意。
问题是我还有一个&#34;阅读更多&#34;标记中的按钮 - 当悬停在上面时 - 我也想显示叠加图像。我想我会用jQuery来实现这个目标。
我有以下脚本:
$(document).ready(function () {
$('#srch-term').click(function () {
if ($('#search-builder').is(":hidden")) {
$("#search-builder").slideDown("fast");
} else {
$("#search-builder").slideUp("fast");
}
});
$('.read-more').hover(
function () {
$img = $(this).closest('div').find('.post-img');
$overlay = $(this).closest('div').find('.overlay');
$img.css({
"opacity": "0.5"
});
$overlay.css({
"opacity": ".5",
"transition": "opacity 200ms ease-in",
"-webkit-transition": "opacity 200ms ease-in",
"-moz-transition": "opacity 200ms ease-in",
"-o-transition": "opacity 200ms ease-in"
});
}, function () {
$img = $(this).closest('div').find('.post-img');
$overlay = $(this).closest('div').find('.overlay');
$img.css({
"opacity": "1.0",
"height": "300px",
"width": "100%",
"background-size": "cover",
"transition": "opacity 200ms ease-out",
"-webkit-transition": "opacity 200ms ease-out",
"-moz-transition": "opacity 200ms ease-out",
"-o-transition": "opacity 200ms ease-out"
});
$overlay.css({
" background-color": "#f39300",
"cursor": "pointer",
"height": "300px",
"width": "585px",
"position": "absolute",
"left": "0",
"z-index": "10",
"opacity": "0"
});
});
});
该脚本效果很好,但当我将off
按钮悬停并尝试将on
图像悬停时,叠加层不再显示。
总结一下:
当我使用类div
将鼠标悬停在.post-img
上时,我会使用CSS来显示叠加效果。当我将鼠标悬停在类.read-more
的链接上时,我使用jQuery显示叠加层 - 它可以正常工作 - 但是当我将鼠标悬停时,当它悬停在div.post-img
关于我哪里出错的任何想法?
答案 0 :(得分:1)
在hover()
“退出”功能中,您直接在元素上设置opacity
等:
$overlay.css({
// ...
"opacity": "0"
});
设置后,这些非常具体的样式会覆盖CSS中的任何内容,包括:hover
样式。
相反,创建一个与:hover
共享相同样式的类,并在链接悬停时添加/删除该类。这也使您不得不保持jQuery和CSS样式同步。
$(document).ready(function() {
$('#srch-term').click(function() {
if ($('#search-builder').is(":hidden")) {
$("#search-builder").slideDown("fast");
} else {
$("#search-builder").slideUp("fast");
}
});
$('.read-more').hover(
function() {
$img = $(this).closest('div').find('.post-img');
$overlay = $(this).closest('div').find('.overlay');
$img.addClass('hovered');
$overlay.addClass('hovered');
},
function() {
$img = $(this).closest('div').find('.post-img');
$overlay = $(this).closest('div').find('.overlay');
$img.removeClass('hovered');
$overlay.removeClass('hovered');
});
});
.post-img {
height: 300px;
width: 100%;
background-size: cover;
transition: opacity 200ms ease-out;
-webkit-transition: opacity 200ms ease-out;
-moz-transition: opacity 200ms ease-out;
-o-transition: opacity 200ms ease-out;
}
.post-img:hover, .post-img.hovered {
opacity: 0.5;
}
span.overlay {
background-color: #f39300;
cursor: pointer;
height: 300px;
width: 585px;
position: absolute;
left: 0;
z-index: 10;
opacity: 0;
}
span.overlay:hover, .overlay.hovered {
opacity: .5;
transition: opacity 200ms ease-in;
-webkit-transition: opacity 200ms ease-in;
-moz-transition: opacity 200ms ease-in;
-o-transition: opacity 200ms ease-in;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="post-box">
<a href='#'>
<span class='overlay'></span>
<div class='post-img' style="background-image:url('http://placehold.it/200');"></div>
</a>
<a class='post-title' href="#">Title</a>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<a class='read-more' href="#">Read more</a>
</div>