【发布时间】:2016-09-02 23:27:39
【问题描述】:
我希望我的 div 在悬停在它们上方时改变颜色,但即使我悬停,代码也不会执行。我不完全确定为什么,但我认为我在想要悬停的类上使用 z-index 可能存在问题。
带有脚本的 HTML:
$(".eventContents").hover(
function() {
$(".eventContents").css("background-color", "yellow");
})
//making events square
var cw = $('.eventContain').width();
$('.eventContain').css({
'height': cw + 'px'
});
.eventContain {
position: relative;
margin-bottom: 10px;
z-index: -1;
background-size: cover;
}
.eventContents {
color: white;
padding: 5px;
position: absolute;
bottom: 0;
left: 0;
}
.eventContents h2 {
font-size: 2em;
font-family: 'Montserrat', sans-serif;
}
.eventContents p {
font-size: 1em;
font-family: 'Roboto', sans-serif;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="events">
<row>
<div class="col-sm-4">
<div class="eventContain" style="background-image:url(img/events/leaf.jpg)">
<div class="eventContents">
<h2 class="eventName">Title of Event</h2>
<p>short description goes about here.</p>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="eventContain" style="background-image:url(img/events/12.jpg)">
<div class="eventContents">
<h2 class="eventName">Title of Event</h2>
<p>short description goes about here.</p>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="eventContain" style="background-image:url(img/events/1.jpg)">
<div class="eventContents">
<h2 class="eventName">Title of Event</h2>
<p>short description goes about here.</p>
</div>
</div>
</div>
</row>
</section>
这是小提琴,这里的问题更加突出: https://jsfiddle.net/jakexia72/x7jLp17z/#&togetherjs=os0pjD0RNr
【问题讨论】:
-
浏览器控制台有错误吗?
-
@ronnyfm 根本没有。
-
当我执行 Run code sn -p 选项时颜色变为黄色。
-
似乎也对我有用.. jsfiddle.net/Hastig/4fjn0ndb 我想,不知道我悬停在它的所有空白上的是什么:P
-
我看到它也在工作,但奇怪的是,当我的所有其他代码都存在时,甚至没有调用悬停函数,即使它们不相关。另外,背景图像是否可能是它不起作用的原因?
标签: javascript jquery html css jquery-hover