【发布时间】:2017-01-29 21:38:06
【问题描述】:
所以我有这个元素#box,它需要有一个悬停效果,当悬停时会自行移动。该元素将在 jQuery 中使用 .hover 正确悬停,然后我需要单击它以显示某些内容,但是单击它后它应该不再具有悬停效果,因此我使用 .unbind 将其删除。现在,当用户重新单击元素时,它将隐藏信息,然后重新应用悬停效果。所以就像一个切换效果。我的问题是最干净的方法是什么。
HTML:
<div class="box" id="box">
<h1 class="headline">ABOUT ME</h1>
</div>
CSS:
.box {
height: 320px;
width: 320px;
background-color: rgba(0, 0, 0, 0.6);
position: relative;
left: 10px;
top: 10px;
transition: all 1s;
-webkit-transition: all 1s;
-moz-transition: all 1s;
-o-transition: all 1s;
}
.headline {
margin: 0 auto;
color: white;
text-align: center;
display: block;
padding-top: 130px;
font-family: 'Montserrat', sans-serif;
}
.box_hover {
left: 0px;
top: 0px;
height: 300px;
width: 300px;
}
JQuery:
$(".box").hover(
function() {
$(this).toggleClass("box_hover");
}
);
$('#box').click(function() {
$(this).unbind('mouseenter mouseleave');
});
这里是JSFiddle
编辑 1:
为了澄清我需要它在悬停时添加类,然后在单击时保持“mouseenter”外观,然后在重新单击时返回能够基于“mouseenter”悬停和移动, "鼠标离开"。
谢谢!
【问题讨论】:
标签: javascript jquery html css hover