| z-indexを使用したキャラの移動 |
| 簡単なキャラの移動処理 |


.container{
position: sticky;
}
.layer{
width: 200px;
height: 200px;
position: absolute;
}
.layer1{
top: 0px;
left: 0px;
}
.layer2{
top: 100px;
left: 90px;
}
cssの「.layer」でpositionをabsoluteにすることで、このエリアを基準に
let x=90;//キャラクターの位置(初期値はキャラの初期座標位置)
const b1 = document.getElementById("btn1");
const b2 = document.getElementById("btn2");
let leftPosition=0;//枠の左側の位置
let rightPosition=200-100;//枠の幅 - 画像の幅
let moveDistance=10;//移動距離
//左へ
b1.addEventListener("click", () =>
{
moveCharacterImage(0);
});
//右へ
b2.addEventListener("click", () =>
{
moveCharacterImage(1);
});
//キャラクターの移動
//direction:
//0:左へ移動
//1:右へ移動
function moveCharacterImage(direction)
{
const obj = document.getElementById("hiyoko");
const img = document.getElementById("img");
if(direction==0)
{
//画像のパスを環境にあわせて変更してください
img.src="nawmin_sample6.png";
x-=moveDistance;
if(x < leftPosition)
{
x=-10;//微調整
}
obj.style.left =x +'px';
}
else
{
//画像のパスを環境にあわせて変更してください
img.src="nawmin_sample9.png";
x+=moveDistance;
if(x > rightPosition)
{
x=rightPosition+5;//微調整
}
obj.style.left =x +'px';
}
}
| [URL]イラスト:農民イラスト | |
| URL[https://nawmin.stores.jp][クリックすると開きます]) |
| Moving a character using z-index |
| Simple character movement |


.container{
position: sticky;
}
.layer{
width: 200px;
height: 200px;
position: absolute;
}
.layer1{
top: 0px;
left: 0px;
}
.layer2{
top: 100px;
left: 90px;
}
By setting the position to absolute in the CSS ".layer", it becomes possible to position the wheat and chicks linked to ".layer" based on this area.
let x=90;//Character position (initial value is the character's initial coordinate position)
const b1 = document.getElementById("btn1");
const b2 = document.getElementById("btn2");
let leftPosition=0;//Left position of frame
let rightPosition=200-100;//frame width - image width
let moveDistance=10;//Distance traveled
//left
b1.addEventListener("click", () =>
{
moveCharacterImage(0);
});
//right
b2.addEventListener("click", () =>
{
moveCharacterImage(1);
});
//Character movement
//direction:
//0:Move Left
//1:Move right
function moveCharacterImage(direction)
{
const obj = document.getElementById("hiyoko");
const img = document.getElementById("img");
if(direction==0)
{
//Please change the image path to suit your environment.
img.src="nawmin_sample6.png";
x-=moveDistance;
if(x < leftPosition)
{
x=-10;//Fine adjustment
}
obj.style.left =x +'px';
}
else
{
//Please change the image path to suit your environment.
img.src="nawmin_sample9.png";
x+=moveDistance;
if(x > rightPosition)
{
x=rightPosition+5;//Fine adjustment
}
obj.style.left =x +'px';
}
}
| [URL]illustration by nawmin.com | |
| URL[https://nawmin.stores.jp][click to open the homepage]) |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |