简体   繁体   English

如何改变图像大小?

[英]How to change size of images?

I am trying to enlarge picture when user hovers to images and reduce the size of images after user mouseout the images. 当用户将鼠标悬停在图像上时,我试图放大图片,并在用户输出图像后缩小图像尺寸。

I have the following codes 我有以下代码

$('#image_layout').on('hover','img',function(){
    $(this).css('width','110%');
})

$('#image_layout').on('mouseout','img',function(){
    $(this).css('width','90%');
})

I want to enlarge the image from the center position of the image not from the top lefe position of the image. 我想从图像的中心位置放大图像,而不是从图像的顶部位置放大图像。

The one I have: 我拥有的那个:

 ----        ------
|    |  ->  |      |
 ----       |      |
             ------

The one I want: (enlarge the picture from center of the image) 我想要的那个:(从图像中心放大图片)

               ------
   ----       |      | 
  |    |  --> |      |
   ----       |      |
               ------

I am not sure how to achieve this. 我不知道如何实现这一目标。 Any tips? 有小费吗? Thanks a lot! 非常感谢!

Avoid using JavaScript for tasks that can be handled by simple CSS tweaks, don't make the weight of JavaScript on your page heavy. 避免将JavaScript用于可以通过简单的CSS调整处理的任务,不要让页面上的JavaScript重量太大。

#image_layout 
{
   width : /* initial width value */ 
   height: /* initial height value */
}


#image_layout : hover
{
   width : /* new width value */ 
   height: /* new height value */
   /* use of multiple tranistions */
   transition : all 0.2s ease-out 
}

Regarding the jQuery way,no need to apply .css() twice, just pass to it an object containing the properties with their values : 关于jQuery方式,不需要两次应用.css(),只需将包含属性及其值的对象传递给它:

$('#image_layout').on('mouseout','img',function(){
    $(this).css({'width':'90%', 'height':'100%'});
});

When you change the width and you haven't defined the height yet, the browser automatically keeps the proportion between them, so the height will also be 110% and 90% . 当您更改width但尚未定义height ,浏览器会自动保持它们之间的比例,因此height也将是110%90%

Try this code: 试试这段代码:

$('#image_layout').on('hover','img',function(){
    $(this).css('width','110%')
        .css('height','100%')
});
$('#image_layout').on('mouseout','img',function(){
    $(this).css('width','90%')
        .css('height','100%');
});

What you want to achieve is not as easy as it seems. 你想要实现的并不像看起来那么容易。 If you want to keep your origin in the center of the container you need to absolute position the image relative to the container. 如果要将原点保持在容器的中心,则需要将图像相对于容器绝对定位。 Try with this lil' plugin, it probably won't work for every case but it's worth a try. 尝试使用这个lil'插件,它可能不适用于所有情况,但值得一试。

$.fn.centerIt = function(){

    var height = this.outerHeight(),
        width = this.outerWidth();

    this.css({
        position: 'absolute',
        top: '50%',
        left: '50%',
        marginTop: -(height/2),
        marginLeft: -(width/2)
    })
    .parent()
    .css('position', 'relative');

    return this;    
};

$('img').css('width', '90%').centerIt();

Demo: http://jsfiddle.net/elclanrs/RMsSh/ 演示: http //jsfiddle.net/elclanrs/RMsSh/

Changing image size in JQuery is pretty easy. 在JQuery中更改图像大小非常简单。 You can take this or the CSS approach with .CSS() 您可以使用.CSS()获取此方法或CSS方法

$('#imageId').width(150).height(100);

为了做到这一点,您需要将图像水平和垂直居中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM