admin管理员组文章数量:1420938
I want to animate the image and keep it exactly in the center of the currently opened window. I have tried the following, but it is not working please suggest how to improve the code to get it working.
// get image dimensions
var h = $(this).height();
var w = $(this).width();
//get div dimensions
var div_h =$('#imgContainer').height();
var div_w =$('#imgContainer').width();
var pY = Math.round((div_h - h) / 2) + 'px';
var pX = Math.round((div_w - w) / 2) + 'px';
$(this).animate({
opacity:"1",
top: pY+"px",
left: pX+"px",
zoom: '500%'
}, 'medium')
I want to animate the image and keep it exactly in the center of the currently opened window. I have tried the following, but it is not working please suggest how to improve the code to get it working.
// get image dimensions
var h = $(this).height();
var w = $(this).width();
//get div dimensions
var div_h =$('#imgContainer').height();
var div_w =$('#imgContainer').width();
var pY = Math.round((div_h - h) / 2) + 'px';
var pX = Math.round((div_w - w) / 2) + 'px';
$(this).animate({
opacity:"1",
top: pY+"px",
left: pX+"px",
zoom: '500%'
}, 'medium')
Share
Improve this question
edited Oct 8, 2012 at 8:28
DrColossos
13.1k3 gold badges48 silver badges68 bronze badges
asked Oct 8, 2012 at 8:25
AbhinavAbhinav
9612 gold badges11 silver badges17 bronze badges
2 Answers
Reset to default 3It really depends on the rest of your markup. Your code works for me this way:
(check the demo)
HTML:
<div id="imgContainer">
<img src="your-image.jpg">
</div>
CSS:
html, body, #imgContainer {
width:100%; height:100%;
}
#imgContainer > img {
position:absolute; top:0; right:0; bottom:0; left:0;
margin:auto;
width:200px;
max-width:100%;
max-height:100%;
}
jQuery:
$('#imgContainer > img').on('click',function(){
var h = $(this).height();
var w = $(this).width();
//get div dimensions
var div_h =$('#imgContainer').height();
var div_w =$('#imgContainer').width();
var pY = Math.round((div_h - h) / 2) + 'px';
var pX = Math.round((div_w - w) / 2) + 'px';
$(this).animate({
opacity:"1",
zoom: '500%'
}, 1000)
});
You can use jQuery zoom plugin. Following is the usage example:
// Example:
$(document).ready(function(){
$('a.photo').zoom({url: 'photo-big.jpg'});
});
// Using ColorBox with Zoom
$(document).ready(function(){
$('a.photo').zoom({
url: 'photo-big.jpg',
callback: function(){
$(this).colorbox({href: this.src});
}
});
});
本文标签: javascriptZoom out image in center using JQUERYStack Overflow
版权声明:本文标题:javascript - Zoom out image in center using JQUERY? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745339418a2654190.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论