.draggable() 的问题以及更改不同尺寸的图像
我试图在 jQuery 中做一些“缩放”功能(加载更大的图像),同时能够在较小的 div 中拖动图像(.draggable)。但我遇到了一个小问题。
当我达到最大缩放(比方说第三张图片,其高度比第一张图片高约 300 像素+,宽度约 1600 像素+),然后将自己置于右下角,然后开始缩小:然后我就陷入了困境当我看到图像本身变小并给我一个白色的空白空间时,图像就不可拖动了(可以这么说)。
但是,如果我随后将 .css({'left': '0px', 'top': '0px'}) 添加到我要替换的图像中,我会飞回左上角,并且可以随意拖动。但显然我想留在我想要放大的地方。
如果我将(帖子中的代码较低)“遏制:[数据]”更改为“遏制:“父级”,我可以“停留”在我放大和缩小的特定区域,但我仍然得到当我进入角落时会出现白色的空白区域(但我可以再次拖动图片),并且代价是它不再是平滑的全景感觉,更像是卡在边缘上
。解决这个问题的简单方法是什么?无论如何,这对于我的问题来说是一个很好的解决方案,还是有更好的方法来实现这种东西,我已经尝试使用 .animate() 并使一张图片更大,但是在。最后我遇到了同样的问题
<div id="map"> // 900px in width and 300px in height and Overflow: hidden in css to keep larger images inside
<img id="map-image" src="images/hogf_zoom0.jpg" /> // same as #map until zoom1.jpg wich is slightly larger
</div>
<div id="zoomPanel">
<a href="#" class="zoomIn">+</a>
<a href="#" class="zoomOut">-</a>
</div>
-
var zoom = 0;
var maxZoom = 3;
$('.zoomIn').click(function() {
if (zoom >= 0 && zoom < maxZoom) {
zoom++;
$("#map-image").attr("src", "images/hogf_zoom" + zoom + ".jpg").css({'top': '-0px','left': '0px'});
if (zoom > 0) { $("#zoomPanel .zoomOut").show(); }
}
});
$('.zoomOut').click(function() {
if (zoom <= maxZoom && zoom > 0) {
zoom--;
$("#map-image").attr("src", "images/hogf_zoom" + zoom + ".jpg").css({'top': '-0px','left': '0px'});
if (zoom == 0) { $("#zoomPanel .zoomOut").hide(); }
}
});
$('#map img').load(function() {
var mapWidth = $("#map").width();
var mapHeight = $("#map").height();
var imgPos = $("#map-image").offset();
var mapPos = $("#map").offset();
var imgWidth = $("#map-image").width();
var imgHeight = $("#map-image").height();
var x1 = ((1 + mapWidth) - imgWidth) + mapPos.left;
var y1 = (11 + mapHeight) - imgHeight;
var x2 = imgPos.left;
var y2 = imgPos.top;
$("#map img").draggable({
containment: [x1,y1,x2,y2],
cursor: 'move'
});
});
感谢您的任何输入
I'm trying to do somewhat of a "zoom"-funktion (loads bigger images) in jQuery while being able to drag around in the images (.draggable) in a smaller div. But I've hit a slight problem.
When I reach to the maximum zoom (lets say third image, which is around 300px+ in height and 1600px+ in width than the first) and then place myself in the bottom right corner and then start to zoom out: I'm then stuck in that corner while I see the image itself turns smaller and giving me a white empty space, and the image is after that undraggable (so to speak).
But if I then add .css({'left': '0px', 'top': '0px'}) to the images I'm replacing I fly back to top-left and I can drag around all I want. But clearly I want to stay at the place that I want to zoom into.
If I change (code lower in post) "containment: [data]" to "containment: "parent" I'm able to "stay" in the certain area I've zoomed in and zoomed out from, but I still get the white empty area when I'm down into a corner (but I'm able to drag in the picture again) and with the cost that it's not the smooth panorama-feeling anymore, more like it snaps to the edges.
Is there some kind of easy way to go around this? And is this a good solution to my kind of problem anyways, or is there better ways to achieve this kind of stuff. I've tried using .animate() and make one picture larger, but in the end I get the same problem.
<div id="map"> // 900px in width and 300px in height and Overflow: hidden in css to keep larger images inside
<img id="map-image" src="images/hogf_zoom0.jpg" /> // same as #map until zoom1.jpg wich is slightly larger
</div>
<div id="zoomPanel">
<a href="#" class="zoomIn">+</a>
<a href="#" class="zoomOut">-</a>
</div>
-
var zoom = 0;
var maxZoom = 3;
$('.zoomIn').click(function() {
if (zoom >= 0 && zoom < maxZoom) {
zoom++;
$("#map-image").attr("src", "images/hogf_zoom" + zoom + ".jpg").css({'top': '-0px','left': '0px'});
if (zoom > 0) { $("#zoomPanel .zoomOut").show(); }
}
});
$('.zoomOut').click(function() {
if (zoom <= maxZoom && zoom > 0) {
zoom--;
$("#map-image").attr("src", "images/hogf_zoom" + zoom + ".jpg").css({'top': '-0px','left': '0px'});
if (zoom == 0) { $("#zoomPanel .zoomOut").hide(); }
}
});
$('#map img').load(function() {
var mapWidth = $("#map").width();
var mapHeight = $("#map").height();
var imgPos = $("#map-image").offset();
var mapPos = $("#map").offset();
var imgWidth = $("#map-image").width();
var imgHeight = $("#map-image").height();
var x1 = ((1 + mapWidth) - imgWidth) + mapPos.left;
var y1 = (11 + mapHeight) - imgHeight;
var x2 = imgPos.left;
var y2 = imgPos.top;
$("#map img").draggable({
containment: [x1,y1,x2,y2],
cursor: 'move'
});
});
Thanks for any kind of input.
As ordered, a demo:
http://miyao.eu/zoomzoom/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我花了一点时间,但如果你保存图像的中心位置并将其转换到下一个图像,你会得到相对较好的结果。我还必须检查结果位置以确保它位于视口内。
我将缩放链接更改为按钮,以便更容易点击。
这是一个演示。
It took me a bit, but if you save the center position of the image and translate that to the next image you get a relatively good result. I had to also check the resulting position to make sure it's inside the view port.
I changed the zoom links into buttons to make it easier to click.
Here is a demo.