gpt4 book ai didi

jquery - 我无法使用 jquery 获取元素的当前位置

转载 作者:行者123 更新时间:2023-12-01 06:46:10 25 4
gpt4 key购买 nike

我只是想使用 jquery animate 将一个元素移动到另一个元素附近。我尝试了元素的 possition() 和 offest() 来完成我的任务,但没有用。

检查我的现场演示我尝试了什么http://jsfiddle.net/z4aLva34/23/

从演示中,我只想在匹配时将老鼠移动到猫附近(在动画位置之后)。

如何做到这一点?

$('.dropzone').click(function() {
var x, y;
var Ans = $(this).attr('id');
$(this).animate({
left : "100px"
}, "slow");
$('#textid').val(Ans);

});

var i = 1;

$('.item').click(function() {
var Ques = $(this).attr('id');
var lname = $(this).attr('name');
$(this).animate({
right : '100px'
});
var Ans = $('#textid').val();

if (Ques == Ans) {
// alert("matched");
T = $("#" + Ans).offset().top;
L = $("#" + Ans).offset().left;
$(this).animate({
bottom : T + 'px'
});
$(this).animate({
right : L + 'px'
});
// here i want to move this elemnt to near mathced element but i cant!
if (i == 1) {

$("#" + Ans).css({
'background-color' : 'red',
'color' : 'white'
});

$('label[name=' + lname + ']').css({
'background-color' : 'red',
'color' : 'white'
});

}

else if (i == 2) {

$("#" + Ans).css({
'background-color' : 'green',
'color' : 'white'
});

$('label[name=' + lname + ']').css({
'background-color' : 'green',
'color' : 'white'
});

} else if (i == 3) {

$("#" + Ans).css({
'background-color' : 'yellow',
'color' : 'black'
});

$('label[name=' + lname + ']').css({
'background-color' : 'yellow',
'color' : 'black'
});

}
i++;

} else {
$("#" + Ans).animate({
left : "0px"
});
$(this).animate({
right : "0px"
});
}
});

最佳答案

我已经改正

$('.dropzone').click(function(){
var x,y;
var Ans = $(this).attr('id');
$(this).animate({left:"100px"},"slow" );
$('#textid').val(Ans);

});

var i=1;

$('.item').click(function(){
var Ques =$(this).attr('id');
var lname = $(this).attr('name');
var Ans =$('#textid').val();

if(Ques==Ans)
{
//alert("matched");
T = $("#"+Ans).offset().top;
L = $("#"+Ans).offset().left;
M = $(this).offset().top;
N = $(this).offset().left;
M-=T;
N-=(L+70);
$(this).animate({bottom:M+'px'});
$(this).animate({right:N+'px'});
//here i want to move this elemnt to near mathced element but i cant!
if(i==1){

$("#"+Ans).css({'background-color':'red','color':'white'});

$('label[name='+lname+']').css({'background-color':'red','color':'white'});

}

else if(i==2){

$("#"+Ans).css({'background-color':'green','color':'white'});

$('label[name='+lname+']').css({'background-color':'green','color':'white'});

}
else if(i==3){

$("#"+Ans).css({'background-color':'yellow','color':'black'});

$('label[name='+lname+']').css({'background-color':'yellow','color':'black'});

}
i++;


}
else
{
$("#" + Ans).animate({left:"0px"});
$(this).animate({right:"0px"});
}
});

关于jquery - 我无法使用 jquery 获取元素的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26794656/

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