jQuery在悬停时仍然保留其悬停样式
jQuery on hover out still retains its hover styles
我有一个jquery脚本,应该在按钮悬停时显示一个内容框。悬停的按钮类也打开了。当鼠标停留在刚刚触发显示的按钮div内时,按钮应该仍然保持其悬停样式。但是每当鼠标悬停时,.hover类也应该被移除。目前,当您将鼠标悬停在按钮上,然后将鼠标悬停在子元素上而不悬停时,.hover类仍然保留。
HTML代码如下:
<li><a href="#" class="login-btn">Login</a>
<div class="login-content">
<form name="login-form" action="" method="post">
<input type="email" name="email" value="" placeholder="E-mail" />
<input type="password" name="password" value="" placeholder="Password" />
<input type="submit" name="login" value="Login" class="form-login" />
</form>
</div>
</li>
jQuery代码如下:
$(document).ready(function () {
$(".login-btn").hover(
function() {
clearTimeout($(this).data('hoverTimeoutId'));
$(".login-content").show();
$(this).addClass('hovered');
},
function() {
clearTimeout($(this).data('hoverTimeoutId'));
$(this).data('hoverTimeoutId', setTimeout(function () {
$(this).removeClass('hovered');
$(".login-content").hide();
} ,500));
});
$('.login-content').hover(
function(){
clearTimeout($(".login-btn").data('hoverTimeoutId'));
},
function(){
$(".login-content").hide();
$(".login-btn").removeClass('hovered');
});
});
最初的问题是setTimeout
函数中this
的上下文不是悬停的元素。相反,应该先将上下文赋值给一个变量来持久化上下文。:
$(document).ready(function () {
$(".login-btn").hover(
function() {
clearTimeout($(this).data('hoverTimeoutId'));
$(".login-content").show();
$(this).addClass('hovered');
},
function() {
var $this = $(this);
clearTimeout($this.data('hoverTimeoutId'));
$this.data('hoverTimeoutId', setTimeout(function () {
$this.removeClass('hovered');
$(".login-content").hide();
} ,500));
});
$('.login-content').hover(
function(){
clearTimeout($(".login-btn").data('hoverTimeoutId'));
},
function(){
$(".login-content").hide();
$(".login-btn").removeClass('hovered');
});
});
我猜你在setTimeout
函数内失去了$(this)
范围。你能试试这个简单的替代品,看看是否有效果吗?
$(".login-btn").hover(
function() {
clearTimeout($(this).data('hoverTimeoutId'));
$(".login-content").show();
$(this).addClass('hovered');
},
function() {
clearTimeout($(this).data('hoverTimeoutId'));
$(this).data('hoverTimeoutId', setTimeout(function () {
$(".hovered").removeClass('hovered'); // change here
$(".login-content").hide();
} ,500));
});
如果页面上有多个.login-btn
,这可能不是最优雅的解决方案,因为它可能会蚕食其他元素的悬停。如果是这种情况,您可以尝试:
var $btn = 0;
$(".login-btn").hover(
function() {
clearTimeout($(this).data('hoverTimeoutId'));
$(".login-content").show();
$(this).addClass('hovered');
},
function() {
clearTimeout($(this).data('hoverTimeoutId'));
$btn = $(this);
$(this).data('hoverTimeoutId', setTimeout(function () {
$btn.removeClass('hovered'); // change here
$(".login-content").hide();
} ,500));
});
与其使用总是触发mouseout事件的.hover()
,不如试试.mouseenter.()
。
在一个事件中,您可以清除活动状态并将其应用于当前状态。
var initVideos = function () {
var divs = $("ul li a");
// bind your hover event to the divs
divs.mouseenter(function () {
flashembed("videoArea", $(this).prop("href"));
divs.removeClass("active");
$(this).addClass("active");
});
};
// once all is loaded
$(window).load(function () {
initVideos();
});
演示:http://jsfiddle.net/tive/Ff7Mq/
方法2:
var btn = $(".login-btn"),
content = $(".login-content"),
triggers = btn.add(content);
triggers.mouseenter(function() {
content.show();
triggers.addClass('hovered');
});
content.mouseleave(function(){
$(this).hide(500);
btn.removeClass('hovered');
});
演示:http://jsfiddle.net/tive/GkVN3/
相关文章:
- 分派点击事件并保留击键修饰符
- 当鼠标悬停在文本中的单词上时显示警报
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 手风琴可点击并悬停
- 如何使用jquery强制一个单词更改大小写等以保留品牌
- 提交后保留下拉选择的值
- 悬停功能触发器
- 如何在鼠标悬停时在另一个图像上滑动图像.
- 在悬停时保留和添加新的转换 (CSS/LESS)
- 如何使用ImageButtons在GridView TemplateField中保留图像超链接的样式/悬停功能
- 保留悬停在链接上的效果
- 在点击菜单项时保留悬停效果
- jQuery悬停时,元素仍然保留悬停类.当鼠标悬停时,应该删除这个类样式
- jQuery在悬停时仍然保留其悬停样式
- 悬停时保留子菜单
- CSS3 Flip Dropdown Menu-将鼠标悬停在子菜单上时保留主菜单悬停样式
- 如何在悬停时将类添加到元素中,但在离开鼠标时类会保留
- 在浏览HTML选择元素时保留悬停样式'的下拉菜单
- 使用 .replacewith 更改图像,但保留 id.之后,图像不再在悬停时触发功能
- jQuery在鼠标悬停时为链接添加背景颜色,当鼠标悬停子菜单时背景保留