【发布时间】:2014-05-27 05:10:50
【问题描述】:
当我点击article.hope 时,我希望我之前给它们的mouseenter 和mouseleave 功能关闭。这我得到了它的工作,但一旦我单击article.hope 兄弟姐妹之一,我就无法重新打开此功能。
这是我目前所拥有的。
$("article.hope").mouseenter(function(){
$(this).children('div').fadeIn(0);
$(this).children('aside').fadeOut(0);
});
$("article.hope").mouseleave(function(){
$(this).children('div').fadeOut(0);
$(this).children('aside').fadeIn(0);
});
$("article.rama").mouseenter(function(){
$(this).children('div').fadeIn(0);
$(this).children('aside').fadeOut(0);
});
$("article.rama").mouseleave(function(){
$(this).children('div').fadeOut(0);
$(this).children('aside').fadeIn(0);
});
$("article.hope").click(function(){
$(this).off('mouseenter mouseleave');
$(this).siblings('article').on('mouseenter mouseleave')
$('section.hope').slideDown(0500, 'swing')
$('section.hope').siblings('section').fadeOut(0)
$(this).siblings('article').fadeIn(0);
});
$("article.rama").click(function(){
$(this).off('mouseenter mouseleave');
$(this).siblings('article').on('mouseenter mouseleave')
$('section.rama').slideDown(0500, 'swing')
$('section.rama').siblings('section').fadeOut(0)
$(this).siblings('article').fadeIn(0);
});
所以当我点击article.hope 时mouseenter 和mouseleave 关闭就好了,但我似乎无法重新打开它们。
编辑:.removeclass/.addclass
HTML
<article class="hope">
<aside><!--background-image--></aside>
<div>
<h2>projet personnel</h2>
<p>First contact</p>
</div>
</article>
CSS
article {
width:100%;
height:210px;
/*background-color:#f7383e;*/
color:#f0f0f0;
text-decoration: none;
margin:0 auto;
text-align:center;
overflow:hidden;
}
article div {
/*max-width:980px;*/
margin:0 auto;
text-align:center;
overflow:hidden;
position:relative;
top: 50%;
transform: translateY(-50%);
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
display:none;
}
article div h2 {
letter-spacing:2px;
font-size:130%;
font-family:'ostrich_sansmedium', Arial, Helvetica, sans-serif;
}
article p {
margin:10px 0;
padding:5px;
display:inline-block;
font-family:'nevis',Arial, Helvetica, sans-serif;
text-transform:uppercase;
font-size:150%;
border-top:2px solid #f0f0f0;
}
article.hope aside {
height:210px;
background-image:url(../images/hope.jpg);
background-position: 50% 50%;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
background-repeat:no-repeat;
}
JQUERY
$("article.hope").mouseenter(function(){
$(this).children('div').fadeIn(0);
$(this).children('aside').fadeOut(0);
});
$("article.hope").mouseleave(function(){
$(this).children('div').fadeOut(0);
$(this).children('aside').fadeIn(0);
});
$("article.hope").click(function(){
$(this).removeclass("hope");
$(this).siblings('article').addclass("hope");
$('section.hope').slideDown(0500, 'swing')
$('section.hope').siblings('section').fadeOut(0)
$(this).siblings('article').fadeIn(0);
});
我很确定我需要在某个地方开设一个新课程才能让它工作,但我不确定在哪里......
【问题讨论】:
-
那是因为你用
.on()附加了事件处理程序,但没有指定函数。 -
一旦你分离了事件,你必须明确地重新绑定它,它不仅仅记得它曾经是怎样的。这就像使用橡皮擦并删除它。这是
unbound。所以你需要做的就是把它变成一个函数,比如function do_mouseenter($ele),然后你就可以像do_mouseenter($(this));一样调用它。 -
一个更简单的方法就是向相关元素添加/删除一个类。因此,当您将鼠标悬停时,请检查该类是否存在并执行所需的任何操作。您可以完全避免解除绑定/重新绑定
-
@Huangism 我试过了,由于某种原因可以让它工作。我想这是因为我的
mouseenter mouseleave和我的click共享同一个班级。 -
@Ohgodwhy 感谢您的快速响应!这很有意义,但是你能给我一个使用我的代码的例子吗?我对此有点陌生。
标签: jquery