| 方案 | 優點 | 缺點 |
|---|---|---|
| 純 ExtJS 實現 | 無外部依賴,風格一致 | 代碼量大,需手寫拖拽、縮放邏輯 |
集成 jQuery 插件(如 jqZoom) | 功能豐富,成熟穩定 | 引入 jQuery 增加體積,事件機制需橋接 |
使用 Ext.ux.ImageZoom 擴展 | 原生組件,API 友好 | 社區維護較少,ExtJS 6+ 兼容性需驗證 |
Ext.ux.ImageZoom 的輕量封裝Ext.define('MyApp.view.ImageMagnifier', {
extend: 'Ext.container.Container',
alias: 'widget.imagemagnifier',
config: {
src: '',
zoomFactor: 2
},
initComponent: function() {
this.items = [{
xtype: 'image',
src: this.getSrc(),
listeners: {
render: function(img) {
new Ext.ux.ImageZoom(img.getEl(), {
zoom: this.getZoomFactor()
});
},
scope: this
}
}];
this.callParent();
}
});
// 使用
{
xtype: 'imagemagnifier',
src: 'resources/images/product.jpg',
zoomFactor: 3
}
position: relative; overflow: hidden;mousemove 事件中計算鼠標相對位置。div.lens(半透明矩形)跟隨鼠標。background-image: url(...); background-position: -x*zoom -y*zoom; 同步更新。lens 首次顯示時才 new Image() 載入大圖。requestAnimationFrame 節流 mousemove 回調。removeListener 避免內存洩漏。ExtJS 6.2+ 官方示例
examples/kitchensink/view/image-zoom.js已包含完整實現,可直接複製適配。