萬盛學電腦網

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

jQuery UI Resize border-box Bug 修復方案:alsoresize 元素意外縮小問題

jQuery UI 的 resizable 組件在配合 Bootstrap 等設置 box-sizing: border-box 的框架時,存在一個長期未修復的 Bug:當啟用 alsoResize 選項同步調整其他元素尺寸時,被聯動元素每次 resize 會意外縮小幾像素(等於 border + padding 差值)。本文提供官方 PR 修復方案的本地化應用指南。

問題現象

// HTML
<div id="main" style="box-sizing: border-box; border: 5px solid red; padding: 10px;">
    <div id="also" style="box-sizing: border-box; border: 2px solid blue;"></div>
</div>

// JS
$("#main").resizable({
    alsoResize: "#also"
});

拖拽 #main 邊緣調整大小,#also 會**每次縮小 14px**(5+5+2+2),反覆拖拽最終消失。

根因分析

jQuery UI 內部計算目標尺寸時,使用 outerWidth/outerHeight 獲取包含 border+padding 的尺寸,但在應用到 alsoResize 目標時,直接設置 width/height(content-box 尺寸),未扣除目標元素自身的 border+padding。
當目標也為 border-box 時,width/height 已包含 border+padding,再次設置相同數值會導致 content 區域壓縮,視覺上表現為整體縮小。

官方修復方案(PR #1451)

修改 jquery.ui.resizable.js 中 _resize 方法內的 alsoResize 邏輯:

// 原始邏輯 (約第 1000 行)
if (o.alsoResize) {
    $(o.alsoResize).each(function() {
        var el = $(this);
        el.css({ width: el.width(), height: el.height() }); // 錯誤:未考慮 box-sizing
    });
}

// 修復後:讀取 computed style 判斷 box-sizing,按 content-box 尺寸設置
if (o.alsoResize) {
    $(o.alsoResize).each(function() {
        var el = $(this);
        var boxSizing = el.css("box-sizing");
        var bw = { 
            left: parseFloat(el.css("border-left-width")) || 0,
            right: parseFloat(el.css("border-right-width")) || 0,
            top: parseFloat(el.css("border-top-width")) || 0,
            bottom: parseFloat(el.css("border-bottom-width")) || 0
        };
        var pw = {
            left: parseFloat(el.css("padding-left")) || 0,
            right: parseFloat(el.css("padding-right")) || 0,
            top: parseFloat(el.css("padding-top")) || 0,
            bottom: parseFloat(el.css("padding-bottom")) || 0
        };
        var targetW = el.width() - (boxSizing === "border-box" ? (bw.left + bw.right + pw.left + pw.right) : 0);
        var targetH = el.height() - (boxSizing === "border-box" ? (bw.top + bw.bottom + pw.top + pw.bottom) : 0);
        el.css({ width: targetW, height: targetH });
    });
}

無修改源碼的外掛修復方案(推薦)

不改動庫文件,通過擴展 alsoResize 選項接受函數,在 resize 事件中手動同步:

$("#main").resizable({
    alsoResize: function() { return $("#also"); }, // 返回 jQuery 對象
    resize: function(event, ui) {
        var $also = $("#also");
        var boxSizing = $also.css("box-sizing");
        var bw = { l: parseFloat($also.css("border-left-width"))||0, r: parseFloat($also.css("border-right-width"))||0 };
        var pw = { l: parseFloat($also.css("padding-left"))||0, r: parseFloat($also.css("padding-right"))||0 };
        // 計算 content-box 寬高
        var w = ui.size.width - (boxSizing === "border-box" ? (bw.l + bw.r + pw.l + pw.r) : 0);
        var h = ui.size.height - (boxSizing === "border-box" ? (parseFloat($also.css("border-top-width"))||0 + parseFloat($also.css("border-bottom-width"))||0 + parseFloat($also.css("padding-top"))||0 + parseFloat($also.css("padding-bottom"))||0) : 0);
        $also.css({ width: w, height: h });
    }
});

此法利用 ui.size(已為 content-box 尺寸)反推目標元素在當前 box-sizing 下應設置的 width/height。

現代替代方案

  • InteractJS:輕量、無依賴、支持 resize、drag、gesture,原生處理 box-sizing 正確。
  • CSS Resize + MutationObserver:原生 resize: both; overflow: auto; 配合 ResizeObserver 監聽尺寸變化同步其他元素,零依賴、性能最優。
  • Vue/React 組件庫:Element Plus ElResizable、Ant Design Resizable 等已內部處理好 box-sizing。

版本狀態說明

  • jQuery UI 1.12.x (當前穩定版) **未包含此修復**。
  • 官方倉庫 PR #1451 已合併至主分支,將在 1.13.0 / 2.0.0 釋出(時間表不確定)。
  • 維護舊項目請使用「外掛修復方案」;新項目建議遷移至現代替代庫。

這個 Bug 是典型的「盒模型不一致」導致的尺寸計算錯誤,理解 content-box vs border-box 在 width/height/outerWidth/outerHeight 中的語義差異,是前端佈局調試的核心基本功。

copyright © 萬盛學電腦網 all rights reserved