Skip to content

Commit 4242d60

Browse files
committed
refactor: ♻️ refactor code
1 parent c00dbc9 commit 4242d60

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

js/main-en.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919
// 凱比機器人和中間的圓形 logo 定位
2020
const kebbi = document.getElementById('svgKebbi');
2121
const circle = document.querySelector('.circle');
22+
function imgPosition() {
2223
const ww = content.offsetWidth;
2324
const wh = content.offsetHeight;
24-
const ox = (ww - kebbi.offsetWidth) / 2;
25-
const oy = (wh * 0.8 - kebbi.offsetHeight) / 2;
26-
kebbi.style.left = `${ox}px`;
27-
kebbi.style.top = `${oy}px`;
25+
kebbi.style.left = `${(ww - kebbi.offsetWidth) / 2}px`;
26+
kebbi.style.top = `${(wh * 0.8 - kebbi.offsetHeight) / 2}px`;
2827
circle.style.width = `${ww * 0.4}px`;
2928
circle.style.height = `${ww * 0.4}px`;
3029
circle.style.left = `${(ww - circle.offsetWidth) / 2}px`;
3130
circle.style.top = `${(wh * 0.8 - circle.offsetHeight) / 2}px`;
31+
}
32+
33+
imgPosition();
3234

3335
// 預設值
3436
let list = {
@@ -292,10 +294,10 @@
292294
// 邊界在中心的周圍 1/3 小車寬/高的距離
293295
const kxCenter = kx + carSize.width / 2; // 車子中心點 x 座標
294296
const kyCenter = ky + carSize.height / 2; // 車子中心點 y 座標
295-
const leftSide = ww * 0.5 - carSize.width / 3; // 左邊界,小於這個值,判定車子移動到左邊
296-
const rightSide = ww * 0.5 + carSize.width / 3; // 右邊界,大於這個值,判定車子移動到右邊
297-
const topSide = oy + carSize.height / 6; // 上邊界,小於這個值,判定車子移動到上面
298-
const bottomSide = oy + 5 * carSize.height / 6; // 下邊界,小於這個值,判定車子移動到下面
297+
const leftSide = content.offsetWidth * 0.5 - carSize.width / 3; // 左邊界,小於這個值,判定車子移動到左邊
298+
const rightSide = content.offsetWidth * 0.5 + carSize.width / 3; // 右邊界,大於這個值,判定車子移動到右邊
299+
const topSide = (content.offsetHeight * 0.8 - kebbi.offsetHeight) / 2 + carSize.height / 6; // 上邊界,小於這個值,判定車子移動到上面
300+
const bottomSide = (content.offsetHeight * 0.8 - kebbi.offsetHeight) / 2 + 5 * carSize.height / 6; // 下邊界,小於這個值,判定車子移動到下面
299301

300302
if (kxCenter < leftSide) {
301303
if (!send.left) {
@@ -353,8 +355,8 @@
353355
drag = false;
354356
kebbi.classList.remove('target');
355357
kebbi.classList.add('reset');
356-
kebbi.style.left = `${ox}px`;
357-
kebbi.style.top = `${oy}px`;
358+
kebbi.style.left = `${(content.offsetWidth - kebbi.offsetWidth) / 2}px`;
359+
kebbi.style.top = `${(content.offsetHeight * 0.8 - kebbi.offsetHeight) / 2}px`;
358360
}
359361

360362
function updateCarSize() {
@@ -379,11 +381,6 @@
379381
}
380382

381383
// 處理中間圖片跟隨視窗大小移動位置
382-
window.addEventListener('resize', () => {
383-
kebbi.style.left = `${(window.innerWidth - kebbi.offsetWidth) / 2}px`
384-
kebbi.style.top = `${(window.innerHeight - kebbi.offsetHeight - document.getElementById('monsterBlock').offsetHeight) / 2}px`
385-
circle.style.left = `${(window.innerWidth - circle.offsetWidth) / 2}px`;
386-
circle.style.top = `${(content.offsetHeight * 0.8 - circle.offsetHeight) / 2}px`;
387-
});
384+
window.addEventListener('resize', imgPosition);
388385

389386
}();

js/main.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919
// 凱比機器人和中間的圓形 logo 定位
2020
const kebbi = document.getElementById('svgKebbi');
2121
const circle = document.querySelector('.circle');
22-
const ww = content.offsetWidth;
23-
const wh = content.offsetHeight;
24-
const ox = (ww - kebbi.offsetWidth) / 2;
25-
const oy = (wh * 0.8 - kebbi.offsetHeight) / 2;
26-
kebbi.style.left = `${ox}px`;
27-
kebbi.style.top = `${oy}px`;
28-
circle.style.width = `${ww * 0.4}px`;
29-
circle.style.height = `${ww * 0.4}px`;
30-
circle.style.left = `${(ww - circle.offsetWidth) / 2}px`;
31-
circle.style.top = `${(wh * 0.8 - circle.offsetHeight) / 2}px`;
22+
function imgPosition() {
23+
const ww = content.offsetWidth;
24+
const wh = content.offsetHeight;
25+
kebbi.style.left = `${(ww - kebbi.offsetWidth) / 2}px`;
26+
kebbi.style.top = `${(wh * 0.8 - kebbi.offsetHeight) / 2}px`;
27+
circle.style.width = `${ww * 0.4}px`;
28+
circle.style.height = `${ww * 0.4}px`;
29+
circle.style.left = `${(ww - circle.offsetWidth) / 2}px`;
30+
circle.style.top = `${(wh * 0.8 - circle.offsetHeight) / 2}px`;
31+
}
32+
33+
imgPosition();
3234

3335
// 預設值
3436
let list = {
@@ -287,15 +289,14 @@
287289
const kx = kebbi.offsetLeft; // 車子 left
288290
const ky = kebbi.offsetTop; // 車子 top
289291
const carSize = getCarSize();
290-
291292
// 注意,小車一開始的中心點 y 座標,不等於畫面的中心點 y 座標。
292293
// 邊界在中心的周圍 1/3 小車寬/高的距離
293294
const kxCenter = kx + carSize.width / 2; // 車子中心點 x 座標
294295
const kyCenter = ky + carSize.height / 2; // 車子中心點 y 座標
295-
const leftSide = ww * 0.5 - carSize.width / 3; // 左邊界,小於這個值,判定車子移動到左邊
296-
const rightSide = ww * 0.5 + carSize.width / 3; // 右邊界,大於這個值,判定車子移動到右邊
297-
const topSide = oy + carSize.height / 6; // 上邊界,小於這個值,判定車子移動到上面
298-
const bottomSide = oy + 5 * carSize.height / 6; // 下邊界,小於這個值,判定車子移動到下面
296+
const leftSide = content.offsetWidth * 0.5 - carSize.width / 3; // 左邊界,小於這個值,判定車子移動到左邊
297+
const rightSide = content.offsetWidth * 0.5 + carSize.width / 3; // 右邊界,大於這個值,判定車子移動到右邊
298+
const topSide = (content.offsetHeight * 0.8 - kebbi.offsetHeight) / 2 + carSize.height / 6; // 上邊界,小於這個值,判定車子移動到上面
299+
const bottomSide = (content.offsetHeight * 0.8 - kebbi.offsetHeight) / 2 + 5 * carSize.height / 6; // 下邊界,小於這個值,判定車子移動到下面
299300

300301
if (kxCenter < leftSide) {
301302
if (!send.left) {
@@ -353,8 +354,8 @@
353354
drag = false;
354355
kebbi.classList.remove('target');
355356
kebbi.classList.add('reset');
356-
kebbi.style.left = `${ox}px`;
357-
kebbi.style.top = `${oy}px`;
357+
kebbi.style.left = `${(content.offsetWidth - kebbi.offsetWidth) / 2}px`;
358+
kebbi.style.top = `${(content.offsetHeight * 0.8 - kebbi.offsetHeight) / 2}px`;
358359
}
359360

360361
function updateCarSize() {
@@ -379,11 +380,6 @@
379380
}
380381

381382
// 處理中間圖片跟隨視窗大小移動位置
382-
window.addEventListener('resize', () => {
383-
kebbi.style.left = `${(window.innerWidth - kebbi.offsetWidth) / 2}px`
384-
kebbi.style.top = `${(window.innerHeight - kebbi.offsetHeight - document.getElementById('monsterBlock').offsetHeight) / 2}px`
385-
circle.style.left = `${(window.innerWidth - circle.offsetWidth) / 2}px`;
386-
circle.style.top = `${(content.offsetHeight * 0.8 - circle.offsetHeight) / 2}px`;
387-
});
388-
383+
window.addEventListener('resize', imgPosition);
384+
389385
}();

0 commit comments

Comments
 (0)