|
19 | 19 | // 凱比機器人和中間的圓形 logo 定位 |
20 | 20 | const kebbi = document.getElementById('svgKebbi'); |
21 | 21 | 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(); |
32 | 34 |
|
33 | 35 | // 預設值 |
34 | 36 | let list = { |
|
287 | 289 | const kx = kebbi.offsetLeft; // 車子 left |
288 | 290 | const ky = kebbi.offsetTop; // 車子 top |
289 | 291 | const carSize = getCarSize(); |
290 | | - |
291 | 292 | // 注意,小車一開始的中心點 y 座標,不等於畫面的中心點 y 座標。 |
292 | 293 | // 邊界在中心的周圍 1/3 小車寬/高的距離 |
293 | 294 | const kxCenter = kx + carSize.width / 2; // 車子中心點 x 座標 |
294 | 295 | 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; // 下邊界,小於這個值,判定車子移動到下面 |
299 | 300 |
|
300 | 301 | if (kxCenter < leftSide) { |
301 | 302 | if (!send.left) { |
|
353 | 354 | drag = false; |
354 | 355 | kebbi.classList.remove('target'); |
355 | 356 | 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`; |
358 | 359 | } |
359 | 360 |
|
360 | 361 | function updateCarSize() { |
|
379 | 380 | } |
380 | 381 |
|
381 | 382 | // 處理中間圖片跟隨視窗大小移動位置 |
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 | + |
389 | 385 | }(); |
0 commit comments