$(".leftmenu_main li").hover(function () {
$(this).addClass('hover');
var menuTop = $(this).offset().top;
var itemsList = $(this).children(".sec_cd");
var itemsCount = itemsList.find('.sec_icon').length;
itemsList.css("top", subMenuTop).fadeIn("fast");
}, function () {
$(".sec_cd").hide();
$(this).removeClass('hover');
});
Give me a chestnut
So offset calculates not the height from the parent, but the top and left margins from the body.
html
css
js
offset().top is the final result of
层层向上
relative to the bodyNote that it is upward layer by layer. No matter how many parents are positioned in the middle, or how many top offsets there are in the middle, they will be superimposed. .
It can also be achieved with native JS:
It depends on whether you have written the positioning parent. If there is no positioning parent, it is the body
为什么不查 API。