萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript 判斷訪客螢幕解析度判斷壁紙適用性

應用場景

壁紙下載站常見需求:依訪客螢幕解析度,自動提示圖片是否適合做桌布(完美符合、需裁剪、尺寸過小)。

核心判斷邏輯

function checkWallpaperSuitability(imgWidth, imgHeight) {
    const sw = screen.width;
    const sh = screen.height;
    const imgRatio = imgWidth / imgHeight;
    const scrRatio = sw / sh;

    // 1. 完美符合
    if (imgWidth === sw && imgHeight === sh) {
        return { class: 'green', msg: '完全適合做您的桌面' };
    }

    // 2. 圖片較大或等寬/高
    if (imgWidth >= sw && imgHeight >= sh) {
        if (imgRatio === scrRatio) {
            return { class: 'green', msg: '適合做您的桌面(比例相同)' };
        }
        return { class: 'yellow', msg: '不完全適合,您可以自己動手裁剪' };
    }

    // 3. 圖片較小
    if (imgWidth < sw || imgHeight < sh) {
        return { class: 'red', msg: '尺寸略小,不適合做您電腦的桌布' };
    }
}

完整頁面範例

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>壁紙適用性檢測</title>
    <style>
        .green { color: #2e7d32; font-weight: bold; }
        .yellow { color: #f57f17; font-weight: bold; }
        .red { color: #c62828; font-weight: bold; }
        .result { margin-top: 20px; font-size: 18px; }
    </style>
</head>
<body>
    <h2>壁紙適用性檢測</h2>
    <p>當前螢幕解析度:<span id="screenInfo"></span></p>
    <p>測試圖片:1920 x 1200</p>
    <div id="result" class="result"></div>

    <script>
        // 顯示螢幕資訊
        document.getElementById('screenInfo').textContent = 
            `${screen.width} x ${screen.height} (可用區域: ${screen.availWidth} x ${screen.availHeight})`;

        // 執行判斷
        const result = checkWallpaperSuitability(1920, 1200);
        const el = document.getElementById('result');
        el.innerHTML = `<span class="${result.class}">${result.msg}</span>`;

        function checkWallpaperSuitability(imgWidth, imgHeight) {
            const sw = screen.width, sh = screen.height;
            const imgRatio = imgWidth / imgHeight;
            const scrRatio = sw / sh;

            if (imgWidth === sw && imgHeight === sh) {
                return { class: 'green', msg: '完全適合做您的桌面' };
            }
            if (imgWidth >= sw && imgHeight >= sh) {
                if (Math.abs(imgRatio - scrRatio) < 0.01) { // 浮點數比較容差
                    return { class: 'green', msg: '適合做您的桌面(比例相同)' };
                }
                return { class: 'yellow', msg: '不完全適合,您可以自己動手裁剪' };
            }
            return { class: 'red', msg: '尺寸略小,不適合做您電腦的桌布' };
        }
    </script>
</body>
</html>

關鍵細節補充

  • 浮點數比較:比例判斷用 Math.abs(a - b) < 0.01 避免精度誤差。
  • 動態取得圖片尺寸:實際專案中 imgWidth/imgHeight 應由後端 API 或 new Image() 預載取得:
function getImageSize(url, callback) {
    const img = new Image();
    img.onload = function() { callback(img.width, img.height); };
    img.src = url;
}

// 使用
getImageSize('wallpaper.jpg', (w, h) => {
    const result = checkWallpaperSuitability(w, h);
    // 更新 UI...
});

延伸:PHP + JS 協作方案

原文提到 PHP 傳遞尺寸易造成跳轉/死循環。現代做法:

  1. 後端輸出 JSON-LD 或 data-* 屬性內嵌圖片尺寸
  2. 前端讀取屬性直接判斷,無需額外請求
<!-- 後端模板渲染 -->
<img id="wallpaper" src="wallpaper.jpg" data-width="1920" data-height="1200">

<script>
const img = document.getElementById('wallpaper');
const result = checkWallpaperSuitability(img.dataset.width, img.dataset.height);
// ...
</script>

總結

壁紙適用性判斷核心為「尺寸比較」與「比例比較」。純前端即可完成,無需後端介入。關鍵在正確讀取 screen.width/height 並處理浮點數比較容差。現代專案建議將尺寸資料內嵌 HTML 屬性,避免額外請求與同步問題。

copyright © 萬盛學電腦網 all rights reserved