ورود

View Full Version : سوال: حرکت اشبا در جاوا اسکریپت



refugee
پنج شنبه 28 دی 1391, 14:33 عصر
سلام یک دایو دارم میخواهم وقتی رو پلی کلیک شد حرکت کنه و و قتی استپ شد واسته ./

کد ها رو نوشتم و همه جی کامله ولی فقط مقدار اولیه رو تکرار میکنه و جلو تر نمیره . من میخواهم تا اخر صفحه سمت راست حرکت کنه ...


ممنون میشم ببینید و کمکم کنید .

//javascript code
var box;
var anim;

function play(){
box = document.getElementById ("box");
if(box.style.left == undefined) box.style.left = '0px';
anim = setInterval (animation,100);
}
function animation(){
box.style.left = ((+box.style.left)+10)+"px";
console.log(box.style.left);
if ((+box.style.left)>=window.innerWidth-box.clientWidth)
stop();
}

function stop(){
clearInterval (anim);

}



<!DOCTYPE html>
<html>
<head>
<title>Go To Right</title>
<script type="text/javascript" src="goright.js"></script>
</head>
<body>
<button onclick="play()">Play</button>
<button onclick="stop()">Stop</button>

<div id="box" style="background-color:#333; width:100px; height:100px;position:absolute"></div>

</body>
</html>

refugee
پنج شنبه 28 دی 1391, 21:06 عصر
یعنی هیشکی یاد نداشت ؟ هاهاها

pouyan.hosseini
جمعه 29 دی 1391, 02:32 صبح
do ta + ghabel box.style.left niazi nist.
if(box.style.left == undefined)
hamishe dare undefined bar migardoone vase hamin hey left margin 0 mikone shoma fk mikone dare tekrar mikone.

2undercover
جمعه 29 دی 1391, 07:38 صبح
do ta + ghabel box.style.left niazi nist.
if(box.style.left == undefined)
hamishe dare undefined bar migardoone vase hamin hey left margin 0 mikone shoma fk mikone dare tekrar mikone.

لطفا فارسی بنویسید! اینجوری توی جستجو ها هم راحت تر جواب ها پیدا میشه! ممنون!



موفق باشید...!

refugee
جمعه 29 دی 1391, 15:45 عصر
do ta + ghabel box.style.left niazi nist.
if(box.style.left == undefined)
hamishe dare undefined bar migardoone vase hamin hey left margin 0 mikone shoma fk mikone dare tekrar mikone.

نخیر مال این نبود ./


تست کردم ج نداد ... لطفا راهنمایی کنید ...

Variable
شنبه 30 دی 1391, 01:43 صبح
شما مشکل کارت اینه که میخای مقدار چپ و راست رو بگیری به فرمت 10px مثلا .
بعد با یه عدد جمع کنی و بعد ازش استفاده کنی.
ا
<!DOCTYPE html>
<html>
<head>
<title>Go To Right</title>
<script type="text/javascript" src=""></script>
</head>
<body>
<button onclick="play()">Play</button>
<button onclick="stop()">Stop</button>

<div id="box" style="background-color:#333; width:100px; height:100px;position:absolute"></div>
<script type="text/javascript">
var box;
var anim;

function play(){
box = document.getElementById ("box");
leftpos=parseInt(box.style.left);
if(isNaN(leftpos))box.style.left = '0px'
console.log(box.style.left);
anim = setInterval (animation,100);
}
function animation(){
box.style.left = ((parseInt(box.style.left)+10))+"px";
if (parseInt(box.style.left)>=window.innerWidth-box.clientWidth)
stop();
}

function stop(){
clearInterval (anim);

}
</script>
</body>
</html>