gpt4 book ai didi

javascript - JQuery,左位置错误

转载 作者:太空宇宙 更新时间:2023-11-04 04:38:49 24 4
gpt4 key购买 nike

我有这个代码:

$(this).closest("p").after('<strong>' + arMess[iCurIndex] + '</strong>').next().animate({
top: $(this).offset().top - 57,
left: -$(this).width() //[First value] Here i get 148px on chrome and 150px on firefox

}, 300,function(){
alert($(this).offset().top - 57);
alert(-$(this).width()); //[Second value] Here i get the true width the one i want to present the left
});

代码概览:在第一行 我为STRONG 元素设置了动画

如你所见,我从 -$(this).width() 得到了 2 个不同的值:

第一个值:我在动画过程中得到的。

第二个值:动画结束后得到

如果有人知道我得到 2 个不同值的原因,我将非常感激。

最佳答案

这里的问题是 javascript 关键字 this 在动画之后调用的内部函数中的值与设置宽度时的值不同。对于第一个值,this 是触发动画的对象。在调用警报的函数中,this 是被激活的对象。

试试这个:

var $strongElement = $(this).closest("p").after('<strong>' + arMess[iCurIndex] + '</strong>').next();

$strongElement.animate({
top: $(this).offset().top - 57,
left: -$strongElement.width()
}, 300, function(){
alert(-$(this).width());
});

参见 http://jsfiddle.net/vtyjf/

关于javascript - JQuery,左位置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15850040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com