admin管理员组文章数量:1405170
Using jQuery 2.1, Meyer's 2.0 CSS reset script, targeting IE9+ and modern browsers.
I've made two overlapping divs. I'm listening to mouseover
and mouseout
events on both of them. Fiddle: /
Expected behaviour: mouseover events fire on both divs.
Actual behaviour: mouseover event fires only on the top div.
How can I make the mouseover event bubble to the bottom div?
HTML
<div id="container">
<div id="top"></div>
<div id="below"></div>
</div>
jQuery
$("#top")
.mouseover(function () {
console.log("top");
})
.mouseout(function () {
console.log("top out");
});
$("#below")
.mouseover(function () {
console.log("bottom");
})
.mouseout(function () {
console.log("bottom out");
});
CSS
#top {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
border: 1px red solid;
}
#below {
position: absolute;
top: 0;
left: 0;
width: 32px;
height: 32px;
border: 1px blue solid;
}
Using jQuery 2.1, Meyer's 2.0 CSS reset script, targeting IE9+ and modern browsers.
I've made two overlapping divs. I'm listening to mouseover
and mouseout
events on both of them. Fiddle: http://jsfiddle/vbkjL/1/
Expected behaviour: mouseover events fire on both divs.
Actual behaviour: mouseover event fires only on the top div.
How can I make the mouseover event bubble to the bottom div?
HTML
<div id="container">
<div id="top"></div>
<div id="below"></div>
</div>
jQuery
$("#top")
.mouseover(function () {
console.log("top");
})
.mouseout(function () {
console.log("top out");
});
$("#below")
.mouseover(function () {
console.log("bottom");
})
.mouseout(function () {
console.log("bottom out");
});
CSS
#top {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
border: 1px red solid;
}
#below {
position: absolute;
top: 0;
left: 0;
width: 32px;
height: 32px;
border: 1px blue solid;
}
Share
Improve this question
edited Apr 30, 2014 at 9:34
SW4
71.3k20 gold badges136 silver badges139 bronze badges
asked Apr 30, 2014 at 9:32
HowieHowie
2,7786 gold badges35 silver badges61 bronze badges
1
- future visitors: Also make sure you don't have any box-shadows or anything like that on children that makes parent to be triggered on parts that are not overlapping children. – aderchox Commented May 30, 2022 at 10:31
1 Answer
Reset to default 7If you want the mouseover
on #below
to trigger #top
's one, make #below
a child of #top
.
<div id="container">
<div id="top">
<div id="below"></div>
</div>
</div>
In your current HTML, there's no relation of bubbling nor capturing between your two divs, as they're not nested.
A reminder of bubbling / from Quirksmode:
Event capturing
When you use event capturing
| | ---------------| |----------------- | element1 | | | | -----------| |----------- | | |element2 \ / | | | ------------------------- | | Event CAPTURING | -----------------------------------
the event handler of element1 fires first, the event handler of element2 fires last.
Event bubbling
When you use event bubbling
/ \ ---------------| |----------------- | element1 | | | | -----------| |----------- | | |element2 | | | | | ------------------------- | | Event BUBBLING | -----------------------------------
the event handler of element2 fires first, the event handler of element1 fires last.
Here's a demo, let me know if I misunderstood you.
本文标签: javascriptMouseOver doesn39t bubble on overlapping divsStack Overflow
版权声明:本文标题:javascript - MouseOver doesn't bubble on overlapping divs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744876145a2629926.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论