[英]wrong offset in relative position
I have this markup. 我有这个标记。
<div class="row">
<div class="col-md-4">
<img src="imgsrc.jpg" />
</div>
</div>
<div class="row">
<div class="col-md-4">
<img src="imgsrc.jpg" />
</div>
</div>
With .col-md-4
has relative position, when I try to attach mouseover event to the .col-md-4
and get position of its offset it's return wrong offset for second .col-md-4
in second row. 使用.col-md-4
具有相对位置,当我尝试将mouseover事件附加到.col-md-4
并获取其偏移量的位置时,它为第二行中的第二个.col-md-4
返回错误的偏移量。 Here is the script 这是脚本
$('.col-md-4').hover(function(){
var offset = $(this).position();
var height = $(this).height();
console.log(offset); // this gives the wrong offset when hovering over second col-md-4 in second row
$('#shadow2').stop(true, true).animate({
'left': offset.left,
'top': offset.top + height + 10
});
}, function(){
$('#shadow2').animate({
'left': '-350px'
});
});
try using .offset() , it gives position of element relative to document, change 尝试使用.offset() ,它给出元素相对于文档的位置,更改
var offset = $(this).position();
to 至
var offset = $(this).offset();
Update:: 更新::
$('.col-md-4').hover(function(){
var childPosition = $(this).offset();
var parentPosition = $(this).parent().offset();
var actualOffset = {
top: childPosition.top - parentPosition.top,
left: childPosition.left - parentPosition.left
}
var height = $(this).height();
console.log(offset);
$('#shadow2').stop(true, true).animate({
'left': actualOffset.left,
'top': actualOffset.top + height + 10
});
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.