萬盛學電腦網

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

JavaScript 實現圖片旋轉翻轉效果(跨瀏覽器兼容方案)

本文提供一個純 JavaScript 實現的圖片旋轉功能,支援左轉、右轉 90 度增量旋轉,兼容 IE6+、Firefox、Chrome、Safari、Opera 等主流瀏覽器。核心原理:IE 使用 CSS Filter(BasicImage),現代瀏覽器採用 HTML5 Canvas 繪製旋轉後的圖像。

核心代碼邏輯

function rotate(imgId, direction) {
    var img = document.getElementById(imgId);
    if (!img || !direction) return false;

    // 取得當前旋轉步驟(0~3 對應 0°, 90°, 180°, 270°)
    var step = parseInt(img.getAttribute('step')) || 0;

    // 根據方向更新步驟
    if (direction === 'right') {
        step = (step === 3) ? 0 : step + 1;
    } else if (direction === 'left') {
        step = (step === 0) ? 3 : step - 1;
    }
    img.setAttribute('step', step);

    // IE 系列:使用 DXImageTransform.Microsoft.BasicImage filter
    if (document.all) {
        img.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=' + step + ')';
        // IE8 特殊處理:調整父容器高度防止圖片被截斷
        var parent = img.parentNode;
        switch (step) {
            case 0: parent.style.height = img.height + 'px'; break;
            case 1: parent.style.height = img.width + 'px'; break;
            case 2: parent.style.height = img.height + 'px'; break;
            case 3: parent.style.height = img.width + 'px'; break;
        }
    }
    // 現代瀏覽器:使用 Canvas 繪製
    else {
        var canvas = document.getElementById('canvas_' + imgId);
        if (!canvas) {
            // 首次旋轉時創建 Canvas 並隱藏原圖
            img.style.visibility = 'hidden';
            img.style.position = 'absolute';
            canvas = document.createElement('canvas');
            canvas.id = 'canvas_' + imgId;
            img.parentNode.appendChild(canvas);
        }
        var ctx = canvas.getContext('2d');
        // 根據步驟設定 Canvas 尺寸與繪製變換
        switch (step) {
            case 0:
                canvas.width = img.width; canvas.height = img.height;
                ctx.rotate(0); ctx.drawImage(img, 0, 0); break;
            case 1:
                canvas.width = img.height; canvas.height = img.width;
                ctx.rotate(90 * Math.PI / 180); ctx.drawImage(img, 0, -img.height); break;
            case 2:
                canvas.width = img.width; canvas.height = img.height;
                ctx.rotate(180 * Math.PI / 180); ctx.drawImage(img, -img.width, -img.height); break;
            case 3:
                canvas.width = img.height; canvas.height = img.width;
                ctx.rotate(270 * Math.PI / 180); ctx.drawImage(img, -img.width, 0); break;
        }
    }
}

HTML 結構範例

<ul class="clearfix">
    <li>
        <div class="caption">
            <input type="button" value="向左轉" onclick="rotate('pic_1','left')">
            <input type="button" value="向右轉" onclick="rotate('pic_1','right')">
        </div>
        <div class="cont">
            <img id="pic_1" src="1.gif" alt="可旋轉的圖片">
        </div>
    </li>
</ul>

關鍵點解析

  • 步驟狀態儲存:利用 img.getAttribute('step') 記錄當前旋轉狀態,避免全域變數汙染。
  • IE Filter 兼容性:BasicImage 的 rotation 參數接受 0~3,對應四個直角方向。
  • Canvas 繪製座標系:旋轉後需配合 translate 或負座標繪製,確保圖片顯示在可視區域內。
  • 首次創建 Canvas:僅在非 IE 瀏覽器首次旋轉時動態創建 Canvas,並隱藏原始 <img>,效能較佳。

適用場景

適合圖片預覽、上傳前調整方向、簡易圖片編輯器等前端交互場景。若需支援任意角度旋轉、縮放、裁剪等進階功能,建議引入成熟庫(如 Cropper.js、Fabric.js)。

copyright © 萬盛學電腦網 all rights reserved