是否可以让图片从中心向外动画,而不是从左到右(以及从上到下)?我试图达到的效果类似于lightbox,当你点击一张图片,它就会向外扩展。
谢谢!
发布于 2011-05-03 22:53:36
@Aron的解决方案是可以的,但它有一定的限制:你不能在文档流中包含图像。
我的解决方案实际上创建了一个绝对定位的图像克隆,并将其显示在原始图像的顶部。它使用.offset()计算原始图像的绝对位置。
此方法的缺点是,如果文档流发生更改(例如调整客户端窗口的大小时),则绝对定位的元素将保留在旧位置。这取决于您的页面布局,您是否可以使用此方法。
单击我的演示中的图像以切换效果。http://jsfiddle.net/Xhchp/3/
HTML:
<p>Some random text.</p>
<p>More blah. <img id="someImage" src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/16/Deletion_icon.svg/600px-Deletion_icon.svg.png"/> More blah.</p>
<p>Some random text.</p>CSS:
#someImage { width:32px; height:32px; }javascript:
function ZoomIn(){
var p = $(this).offset();
var w = $(this).width();
var h = $(this).height();
var $clone = $(this).clone();
$clone.css({
position: "absolute",
left: p.left + "px",
top: p.top + "px",
"z-index": 2
}).appendTo('body');
$clone.data("origWidth",w);
$clone.data("origHeight",h);
$clone.data("origTop",p.top);
$clone.data("origLeft",p.left);
$clone.animate({
top: "-=" + Math.floor(h * 0.5),
left: "-=" + Math.floor(w * 0.5),
width: Math.floor(w * 2),
height: Math.floor(h * 2)
},function(){
});
$clone.click(ZoomOut);
}
function ZoomOut(){
var w = $(this).data("origWidth");
var h = $(this).data("origHeight");
var t = $(this).data("origTop");
var l = $(this).data("origLeft");
$(this).animate({
top: t,
left: l,
width: w,
height: h
},function(){
$(this).remove();
});
}
$(function(){
$('img').click(ZoomIn);
});发布于 2011-05-03 22:24:04
这应该不会太难:
// increase factor
var factor = 2;
$('#foo').click(function() {
$(this).animate({
top: '-=' + $(this).height() / factor,
left: '-=' + $(this).width() / factor,
width: $(this).width() * factor
});
});如何实现这一点:
* 2,但我可以想象您想要做一些具有上限的智能操作。发布于 2011-05-03 22:48:15
如果我没理解错的话here is what you want
我用position: absolute显示了大图,这样你的布局就不会受到图片大小调整的影响。如果您对此解决方案有疑问,请在此处发表评论。
https://stackoverflow.com/questions/5870655
复制相似问题