Svg 自动调整大小以填充它的列容器。完全加载 svgPanZoom 后,我以较小的图像结束:
如果我对容器应用高度,它会调整大小,在每一侧留下空白。
问题是当我再次放大和缩小时,图像不再居中:
我认为有两种方法可以解决这个问题: 1. 在 zoomOut 上重新定位图像。2. 使用 viewBox preserveAspect 使图像正确调整大小,而无需通过 CSS 应用大小。
对我来说,这两个有点令人困惑,经过几个小时的尝试,我无法让它发挥作用。这是我的代码:
HTML:
...
<div class="col-sm-9">
<div class="title">
<h1>Floor: {{ floor }}</h1>
<div id="map-container"></div>
</div>
</div>
...
JS:
function createNewEmbed(data){
var embed = document.createElement('object');
embed.setAttribute('id', 'map');
embed.setAttribute('style', 'background-color: blue');
embed.setAttribute('type', 'image/svg+xml');
embed.setAttribute('data', data);
document.getElementById('map-container').appendChild(embed)
lastEventListener = function(){
// Zoom and Pan
panZoomInstance = svgPanZoom(embed, {
zoomEnabled: true,
controlIconsEnabled: false,
fit: true,
center: true,
minZoom: 1,
zoomScaleSensitivity: 0.4,
beforePan: beforePan
});
// Zoom out
// panZoomInstance.zoom(0.2)
$(window).resize(function(){
panZoomInstance.resize();
panZoomInstance.fit();
panZoomInstance.center();
})
embed.addEventListener('load', lastEventListener)
return embed
}
function removeEmbed(){
// Destroy svgpanzoom
svgPanZoom(lastEmbed).destroy()
// Remove event listener
lastEmbed.removeEventListener('load', lastEventListener)
// Null last event listener
lastEventListener = null
// Remove embed element
document.getElementById('map-container').removeChild(lastEmbed)
// Null reference to embed
lastEmbed = null
}
function beforePan (oldPan, newPan){
var stopHorizontal = false
, stopVertical = false
// Computed variables
, sizes = this.getSizes()
, gutterWidth = sizes.width
, gutterHeight = sizes.height
, leftLimit = -((sizes.viewBox.x + sizes.viewBox.width) * sizes.realZoom) + gutterWidth
, rightLimit = sizes.width - gutterWidth - (sizes.viewBox.x * sizes.realZoom)
, topLimit = -((sizes.viewBox.y + sizes.viewBox.height) * sizes.realZoom) + gutterHeight
, bottomLimit = sizes.height - gutterHeight - (sizes.viewBox.y * sizes.realZoom)
customPan = {}
customPan.x = Math.max(leftLimit, Math.min(rightLimit, newPan.x))
customPan.y = Math.max(topLimit, Math.min(bottomLimit, newPan.y))
return customPan
}
一开始我正在做:
window.onload = function(){
var lastEventListener = null;
var lastEmbedSrc = '/assets/images/6e2.svg'
, lastEmbed = createNewEmbed(lastEmbedSrc)
;