<!DOCTYPE?html>
<html>
????<head>
????????<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/>
????????<style?type="text/css">
????????????div?{
????????????????width:90px;
????????????????height:95px;
????????????????background-color:red;
????????????????position:absolute;
????????????????top:50px;
????????</style>
????????<script?src="http://libs.baidu.com/jquery/1.9.0/jquery.js"?type="text/javascript"></script>
????????<title>jQuery動(dòng)畫(huà)特效</title>
????</head>
????
????<body>
????<div?id="move"></div>
????<input?type="button"?id="right"?value="右移"></input>
????<input?type="button"?id="left"?value="左移"></input>
????<script>
?????????????var?btnR?=?$("#right");
?????????????var?btnL?=?$("#left");
?????????????var?div?=?$("#move");
?????????????//var?pos?=parseInt(div.style.left);
????????????btnR.bind("click",function(){
????????????????div.animate({
????????????????????left:"+=50px"
????????????????????//left:pos?+?50?+?"px"
????????????????},
????????????????200);
????????????});
????????????btnL.bind("click",function(){
????????????????div.animate({
????????????????????left:"-=50px"
????????????????????//left:pos?+?50?+?"px"
????????????????},
????????????????200);
????????????});
????????????
????????????????
????</script>????
????</body>
</html>
2016-03-01
btnR.bind("click",function(){
? ? ? ? ? ? ? ? console.log(div.style);//這里輸出的是undefined
? ? ? ? ? ? ? ? var pos =parseInt(div.offset().left);//這里
? ? ? ? ? ? ? ? //console.log(pos);
? ? ? ? ? ? ? ? //var newPos = pos + 50;
? ? ? ? ? ? ? ? //console.log(newPos);
? ? ? ? ? ? ? ? div.animate({
? ? ? ? ? ? ? ? ? ? //left:"+=50px"
? ? ? ? ? ? ? ? ? ? left:pos+50
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? 200);
? ? ? ? ? ? });
方法這樣重寫(xiě)就可以了。具體看控制臺(tái)打印出的日志就會(huì)明白,"div.style.left"根本不存在,應(yīng)該這樣獲取“div.offset().left”,具體看代碼
2016-03-01
你刷新試試。。我復(fù)制你的這段代碼運(yùn)行。。一點(diǎn)問(wèn)題都沒(méi)有啊。