作者:as2520_526 | 来源:互联网 | 2023-09-17 15:11
I would like to show one div2 slowly when I hover a div1 associated in the same div. I do not see where the problem is in my jquery...
当我将同一div中关联的div1悬停时,我想慢慢显示一个div2。我没有看到问题在我的jquery中...
$(".div").hover(function() {
$(this).find(".div2").animate({
opacity: "1"
}, {
queue: false
});
}, function() {
$(this).find(".div2").animate({
opacity: "0"
}, {
queue: false
});
});
.div2 {
opacity: 0;
}
4 个解决方案
3
You are targeting $('.div')
and not $('.div1')
or $('div')
. There is no element with class .div
in your code, so this is probably a typo.
您的目标是$('。div')而不是$('。div1')或$('div')。代码中没有类.div的元素,所以这可能是一个错字。
You also have to change .find()
to .next()
if you want to trigger the event when hovering over a '.div1'
element, since you want to target the next sibling, and not a child.
如果要在悬停在'.div1'元素上时触发事件,则还必须将.find()更改为.next(),因为您要定位下一个兄弟,而不是孩子。
Here's a working sample you can go by:
以下是您可以使用的工作示例:
https://jsbin.com/vipifolahe/edit?html,js,output
HTML
CSS
.div1 {
height: 50px;
background-color: red;
}
.div2 {
height: 50px;
background-color: green;
}
.div2 {
opacity: 0;
}
JS
$(".div1").hover(function () {
$(this).next(".div2").animate({
opacity: "1"
}, {
queue: false
});
}, function () {
$(this).next(".div2").animate({
opacity: "0"
}, {
queue: false
});
});
If you want to trigger the event when the parent div is hovered over, it could look like so:
如果你想在父div悬停时触发事件,它可能如下所示:
HTML
JS
$(".parent").hover(function () {
$(this).find(".div2").animate({
opacity: "1"
}, {
queue: false
});
}, function () {
$(this).find(".div2").animate({
opacity: "0"
}, {
queue: false
});
});