如何在鼠标移开时隐藏div?
我正在尝试实现一个简单的悬停效果。当用户将鼠标悬停在图像(充满图像的视图)上时,它应该显示一个额外的 div。那里没问题。
但是当我想做相反的事情时,当用户离开 div 区域时隐藏相同的 div 时,它不起作用:离开 div 区域时 div 不会消失(默认情况下应该这样做)使用悬停,不是吗?)。
这是我使用的代码:
$(document).ready(function () {
$('.views-field-field-photo-fid').out(function () {
showDiv($(this), $('.views-field-title'));
});
$('.views-field-field-photo-fid').out(function () {
hideDiv($(this), $('.views-field-title'));
});
});
function showDiv(sender, object) {
$(object).show();
}
function hideDiv(sender, object) {
$(object).hide();
}
到目前为止,我尝试过 .mouseenter
、.mouseleave
、.hover
、.out< /code>,但离开 div 区域后没有任何方法可以隐藏 div。
更新
我或多或少找到了解决方案:
$(document).ready(function() {
$('#uicarousel-news-preview-block-2 .views-field-field-photo-fid').hover(
function() { $(this).find('.views-field-title').show(); },
function() { $(this).find('.views-field-title').hide(); }
)
});
但是由于有很多 .views-field-title
,它也隐藏/显示它们。所以我想使用find
,但没有解决方案。
有什么想法吗?
I'm trying to achieve a simple hover effect. When a user hovers an image (a view filled with images), it should show an extra div. No problem there.
But when I want to do the opposite thing, hide the same div when the user leaves the area of the div, it doesn't work: the div won't go away when leaving the div area (which should be done by default when using the hover, no?).
This is the code I use:
$(document).ready(function () {
$('.views-field-field-photo-fid').out(function () {
showDiv($(this), $('.views-field-title'));
});
$('.views-field-field-photo-fid').out(function () {
hideDiv($(this), $('.views-field-title'));
});
});
function showDiv(sender, object) {
$(object).show();
}
function hideDiv(sender, object) {
$(object).hide();
}
So far, I've tried .mouseenter
, .mouseleave
, .hover
, .out
, but nothing works to hide the div after leaving the div area.
UPDATE
I found the solution, more or less:
$(document).ready(function() {
$('#uicarousel-news-preview-block-2 .views-field-field-photo-fid').hover(
function() { $(this).find('.views-field-title').show(); },
function() { $(this).find('.views-field-title').hide(); }
)
});
But since there are a lot of .views-field-title
, it's hiding/showing them too. So I figured to use the find
, but no solution there.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有调用 jQuery 函数,我不确定你是否使用了插件。无论如何,正确的悬停声明应该是这样的:
我建议您看看 jQuery .hover 文档。
There is no jQuery function called out, I am not sure whether you are using a plugin or not. Anyway, this how a correct hover declaration should look like:
I suggest that you take a look at the jQuery.hover documentation.