JavaScript HTML DOM အန်နီမေးရှင်း
JavaScript ကို အသုံးပြု၍ HTML ကာတွန်းများကို ဖန်တီးရန် လေ့လာပါ။
အခြေခံ ဝဘ်စာမျက်နှာ
JavaScript ဖြင့် HTML ကာတွန်းပုံများ ဖန်တီးနည်းကို သရုပ်ပြရန်၊ ရိုးရှင်းသော ဝဘ်စာမျက်နှာကို အသုံးပြုပါမည်။
ဥပမာ
<!DOCTYPE html>
<html>
<body>
<h1>My First
JavaScript Animation</h1>
<div id="animation">My animation will go here</div>
</body>
</html>
Animation Container ဖန်တီးပါ။
ကာတွန်းအားလုံးသည် ကွန်တိန်နာဒြပ်စင်တစ်ခုနှင့် ဆက်စပ်နေသင့်သည်။
ဥပမာ
<div id ="container">
<div id ="animate">My
animation will go here</div>
</div>
Elements များကို ပုံစံထုတ်ပါ။
ကွန်တိန်နာဒြပ်စင်အား style=" position: relative
" ဖြင့် ဖန်တီးသင့်သည်။
ကာတွန်းဒြပ်စင်ကို style=" position: absolute
" ဖြင့် ဖန်တီးသင့်သည်။
ဥပမာ
#container {
width: 400px;
height:
400px;
position: relative;
background: yellow;
}
#animate {
width: 50px;
height:
50px;
position: absolute;
background: red;
}
အန်နီမေးရှင်းကုဒ်
JavaScript ကာတွန်းများကို ပရိုဂရမ်ရေးဆွဲခြင်းဖြင့် ဒြပ်စင်တစ်ခု၏စတိုင်လ်တွင် ဖြည်းဖြည်းချင်း ပြောင်းလဲမှုများ ပြုလုပ်သည်။
ပြောင်းလဲမှုများကို timer ဟုခေါ်သည်။ timer ကြားကာလ သေးငယ်သောအခါ၊ လှုပ်ရှားပုံသည် အဆက်မပြတ်ဖြစ်နေသည်။
အခြေခံကုဒ်မှာ-
ဥပမာ
id = setInterval(frame, 5);
function frame() {
if (/*
test for finished */) {
clearInterval(id);
} else {
/* code to change the element style */
}
}
JavaScript ကို အသုံးပြု၍ Animation အပြည့်အစုံကို ဖန်တီးပါ။
ဥပမာ
function myMove() {
let id = null;
const elem = document.getElementById("animate");
let pos = 0;
clearInterval(id);
id = setInterval(frame, 5);
function frame() {
if (pos ==
350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + 'px';
elem.style.left = pos + 'px';
}
}
}