【发布时间】:2012-01-30 11:48:35
【问题描述】:
这看起来应该很简单,但由于某种原因,我无法完全理解它。我在“视口”div 中有一个图像,其中的溢出属性设置为隐藏。
我已经使用 jQuery UI 实现了一个简单的缩放和平移,但是我无法让缩放看起来源自视口的中心。我从 Photoshop 做了一个小截屏视频,试图重现我想要重现的效果:http://dl.dropbox.com/u/107346/share/reference-point-zoom.mov
在 PS 中,您可以调整缩放参考点,对象将从该点缩放。显然这对于 HTML/CSS/JS 是不可能的,所以我试图找到合适的左侧和顶部 CSS 值来模拟效果。
这里是有问题的代码,去掉了一些不必要的部分:
html
<div id="viewport">
<img id="map" src="http://dl.dropbox.com/u/107346/share/fake-map.png" alt="" />
</div>
<div id="zoom-control"></div>
javascript
$('#zoom-control').slider({
min: 300,
max: 1020,
value: 300,
step: 24,
slide: function(event, ui) {
var old_width = $('#map').width();
var new_width = ui.value;
var width_change = new_width - old_width;
$('#map').css({
width: new_width,
// this is where I'm stuck...
// dividing by 2 makes the map zoom
// from the center, but if I've panned
// the map to a different location I'd
// like that reference point to change.
// So instead of zooming relative to
// the map image center point, it would
// appear to zoom relative to the center
// of the viewport.
left: "-=" + (width_change / 2),
top: "-=" + (width_change / 2)
});
}
});
这是 JSFiddle 上的项目:http://jsfiddle.net/christiannaths/W4seR/
【问题讨论】:
标签: javascript jquery math scaling offset