jQuery悬停时,元素仍然保留悬停类.当鼠标悬停时,应该删除这个类样式
jQuery on hover out the element still retains the hovered class. This class styles should be removed when the mouse is hovered out
我希望当鼠标悬停在.login-content上时,login-btn保持悬停状态。我已经尝试过了,现在它在悬停时显示和隐藏div,但是当.login-content悬停时,login-btn失去其悬停状态,而.login-content悬停时消失。
更新:
当前的问题是,如果鼠标悬停在登录上,然后直接悬停…hovering样式不会停留在子元素上,而是停留在子元素上。这是不应该的。
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');
});
});
如果需要进一步调试,也可以在http://www.domainandseo.com/portfolio/1may/index.html上找到该网页。
谢谢
Try
$(".login-btn").hover(
function() {
clearTimeout($(this).data('hoverTimeoutId'));
$(".login-content").show();
$(this).addClass('hovered');
},
function() {
clearTimeout($(this).data('hoverTimeoutId'));
$(this).data('hoverTimeoutId', setTimeout(function () {
$(".login-content").hide();
$(this).removeClass('hovered');
} ,500));
});
$('.login-content').hover(
function(){
clearTimeout($(".login-btn").data('hoverTimeoutId'));
},
function(){
$(".login-content").hide();
$(".login-btn").removeClass('hovered');
});
不使用:hover
psedoclass,而是使用hover
这样的类将悬停状态赋值给login-btn
如演示
演示:小提琴
使用setTimeout
$(".login-btn").hover(
function() {
clearTimeout($(this).data('animator'));
$(this).data('animator', setTimeout(function () {
$(".login-content").show();
} ,1000));
},
function() {
clearTimeout($(this).data('animator'));
$(this).data('animator', setTimeout(function () {
$(".login-content").hide();
} ,1000));
});
http://jsfiddle.net/uDm64/如果您不想担心用户将光标移开一会儿,那么我建议您进行更多的状态检查。
另外,如果你想让你的按钮显示为悬停,在你的CSS选择器'。Login-btn:hover',更改为:'。login-btn:悬停,.login-btn.hover '
小提琴:http://jsfiddle.net/qhAus/
(function() {
var timer = 0,
open = false,
loginBtn = $('.login-btn'),
loginContent = $('.login-content'),
startTimeout = function(state, duration) {
timer = setTimeout(function() {
timer = 0;
loginContent[state]();
open = state === 'show';
if(!open) {
// If it's closed, remove hover class
loginBtn.removeClass('hover');
}
}, duration || 1000);
};
loginBtn.hover(function(e) {
$(this).addClass('hover');
if(open && timer) {
// If about to close but starts to hover again
clearTimeout(timer);
}
if(!(open || timer)) {
// Don't start to open again if already in the process
startTimeout('show');
}
}, function() {
// When not hovering anymore, hide login content
startTimeout('hide', 2000);
});
loginContent.mousemove(function(e) {
// If cursor focus on the login content, stop any closing timers
if(timer) {
clearTimeout(timer);
}
});
loginContent.mouseleave(function(e) {
// When cursor leaves login content, hide it after delay
startTimeout('hide');
});
})();
相关文章:
- 在样式表中声明元素后,删除该元素的悬停属性
- 如何动画删除悬停后的背景
- 使用CSS将鼠标悬停在子对象上时,删除父对象上的悬停样式
- 从特定系列的谷歌可视化折线图中删除悬停工具提示
- 使用jquery更改颜色将删除悬停颜色
- 一次高亮显示一个城市,并在悬停其他城市时删除颜色填充
- 悬停事件上的删除按钮
- 如何删除<br>从prettyPhoto悬停
- 如何在通过 Angular 将鼠标悬停在另一个元素上时添加和删除一个元素的类
- jQuery 悬停意图创建和删除元素
- 删除触摸设备 CSS 上的悬停效果
- 悬停时删除和添加类
- 将鼠标悬停在图像上时删除图像标题
- 删除浏览器底部在 jQuery UI 选项卡的鼠标悬停上显示的 URL
- 如何从移动响应屏幕中删除悬停功能
- jQuery + CSS,在悬停时删除其他图像,并具有类似淡入淡出的过渡
- CSS 导航链接样式:删除其他元素的样式,并在将鼠标悬停在选定元素上时删除双边框
- 在悬停时删除剪辑路径
- 在悬停时添加和删除 CSS 类
- 使用jquery悬停删除当前选项卡