简体   繁体   English

使用javascript和jquery设置di​​v内图像的高度和宽度

[英]Set height and width of image inside div using javascript and jquery

I have a problem were I want to set the height and width of image inside the div using only Javascript and JQuery. 我有一个问题,我想只使用Javascript和JQuery设置di​​v内的图像的高度和宽度。

Here is my code: 这是我的代码:

<div class="myGallery">
    <img class="replaced" src="myimage.jpg" style="display: inline;">
</div>

.myGallery {
    display: table-cell;
    height: 220px;
    text-align: center !important;
    vertical-align: middle !important;
    width: 110px;
}

.myGallery img {
    text-align: center;
    vertical-align: middle;
    width: auto !important;
}


function ChangeHW(hdnValue)
{

    if (hdnValue==1)
    {
        //i want to remove height and width attribute of image
            //this is working..
            $('div.myGallery > img').removeAttr('width');
            $('div.myGallery > img').removeAttr('height');
    }
    else
    {
        //i want to set height and width attribute of image 
            //this is not working..i cant set width-height using this line od code
            $('div.myGallery > img').css('width', '110px');
            $('div.myGallery > img').css('height', '220px');
    }

}

Any ideas? 有任何想法吗?

Attributes are different than style. 属性与样式不同。 Say your current image tag looks like this: 假设您当前的图片代码如下所示:

<img src="foo.png" width="200" height="200" />

After $('div.myGallery > img').removeAttr('width') , it will look like this: $('div.myGallery > img').removeAttr('width') ,它将如下所示:

<img src="foo.png" height="200" />

You are removing the width attribute . 您正在删除width 属性 Now after $('div.myGallery > img').css('width', '440px') , it'll look like this: 现在在$('div.myGallery > img').css('width', '440px') ,它看起来像这样:

<img src="foo.png" height="200" style="width: 440px;" />

You're adding in width, but it's the CSS style and not the attribute. 你正在添加宽度,但它是CSS样式而不是属性。 Try this instead: 试试这个:

$('div.myGallery > img').attr('width', '440');

This is a function to fit an image to 586x586 div tag. 这是一个将图像拟合到586x586 div标签的功能。

function img_fit(img_id){

    var img_width = $("#"+img_id).width();
    var img_height= $("#"+img_id).height();
    var max_side = Math.max(img_width, img_height);
    var top = 0, left = 0, scale = 0;
    var new_width, new_height,new_scale;
    scale = (max_side == img_height)? img_height/586 : img_width/586;

    if(max_side == img_height){
         new_width = img_width / scale;
         new_height = 586;
         top = 0;
         left = Math.round((586 - new_width)/2);
         $("#"+img_id).animate({
            'width':new_width,
            'height':new_height, 
            'top':0,
            'left':left,
            'duration':300
        });
         //$("#"+img_id).width(new_width);
         //$("#"+img_id).height(586);
         new_scale = 586*scale/img_height;
    }
    else{
        new_height  = img_height / scale;
        new_width = 586;
        top = Math.round((586 - new_height)/2);
        //top = 0;
        left = 0;
        $("#"+img_id).animate({
            'width':new_width,
            'height':new_height, 
            'top':top,
            'left':0,
            'duration':300
        });

    }

}

Try this: 尝试这个:

$('div.myGallery > img').css('width', '110px !important');

$('div.myGallery > img').css('height', '220px !important');

You look to be setting the image width larger than the containing div, could that be the problem? 您希望将图像宽度设置为大于包含div,这可能是问题吗?

Yo remove the height and width removeAttr would work if it was set in the image tag <img src="puppies.jpg" width="100" height="100"> , otherwise, to change it in the css, I would set width and height = auto , or if you want the container to determine the size try something like width:100% height:auto . 哟删除高度和宽度removeAttr将在图像标签中设置<img src="puppies.jpg" width="100" height="100"> ,否则,要在css中更改它,我会设置widthheight = auto ,或者如果你想让容器确定大小,请尝试width:100% height:auto To add height and width your .css('width','440px') should work. 要添加高度和宽度,您的.css('width','440px')应该有效。 However as mentioned the containing div is smaller. 但是如上所述,包含div更小。

Maybe this could help: 也许这可以帮助:

.newCssClass { height : 110px !important; width : 440px !important; }

$("div.myGallery img").addClass("newCssClass");

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

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