【问题标题】:Resize an image inside a container dynamically using jQuery使用 jQuery 动态调整容器内的图像大小
【发布时间】:2011-05-25 01:35:24
【问题描述】:

我有一个 div 容器,我称之为“content_container”。此容器可以使用jQuery UI 进行拖动和调整大小。在这个容器中,我实现了TinyMCE(内容文本编辑器)。我的问题是:

如果用户插入 2000 x 2000 像素的图像,则容器的最大宽度为 1000 像素。然后它看起来像这样:

 ____________________
| Container header   |
----------------------
| text [image...................image]
|      [image...................image]
|____________________|

(对不起,我还在本地开发它,我还没有找到一个网络托管公司,所以我不能给你直接的链接看演示)。

好的,容器仍然可以调整大小,只是图像大小始终为 2000 像素 x 2000 像素。我的问题是:当我调整“content_container”的大小时,图像会自动调整大小并适合容器宽度吗?

如果是,我该怎么做?

如果没有,是否有其他解决方案来解决这个问题?

代码

TinyMCE之前的容器代码:

<div id="content_container">
    <div id="header">The header</div>
    <div id="content">
        <div id="inlineEditor"></div>
    </div>
</div>

用户输入内容后(比如插入图片),容器会变成:

<div id="content_container">
    <div id="header">The header</div>
    <div id="content">
        <div class="inlineEditor">
            <p>some text <img alt="test" src="../usrXX/img/imgname.jpg"></p>
        </div>
    </div>
</div>

如你所见,我只能操作 inlineEditor 类。

【问题讨论】:

  • 我理解这个问题。但是,发布一些您拥有的代码将有助于更快地获得更好的答案。
  • @dheerosaur:用代码编辑了我的问题

标签: jquery image resize


【解决方案1】:

试试这个:

JavaScript 代码:

/* Start Image Resize Code */
function image_resize() {
    $("img").each(function () {

        /* Max width for the image */
        var maxWidth = 230;

        /* Max hieght for the image */
        var maxHeight = 230;

        /* Used for aspect ratio */
        var ratio = 0;

        /* Current image width */
        var width = $(this).width();

        /* Current image height */
        var height = $(this).height();

        /* Check if the current width is larger than the max */
        if (width > maxWidth) {

            /* Get ratio for scaling image */
            ratio = (maxWidth / width);

            /* Set New hieght and width of Image */
            $(this).attr({
                width : maxWidth,
                height : (height * ratio),
                alt : "your-alt-text-you-can-remove-this"
            });
            /* Reset height to match scaled image */
            height = (height * ratio);
            /* Reset width to match scaled image */
            width = (width * ratio);
            /* Check if current height is larger than max */
            if (height > maxHeight) {

                /* Get ratio for scaling image*/
                ratio = (maxHeight / height);

                /* Set new height and width */
                $(this).attr({
                    height : maxHeight,
                    width : (width * ratio),
                    alt : "your-alt-text-you-can-remove-this"
                });
            }
        }
    });
}
/* End Image Resize Code */

/* How to use with DOM Ready */
$(document).ready(function () {

    /* Call function for image resize (not for a Webkit browser) */
    image_resize();
});

/* How to use with window load (for Webkit browser like Safari and Chrome) */
$(window).load(function () {
    image_resize();
});

/* How to use on Window resize */
$(window).resize(function () {
    image_resize();
});

【讨论】:

    【解决方案2】:

    这个答案是基于 CSS 的。您是否尝试过像这样将类应用于您的图像?

    .fluid-img{width:90%;}
    

    还有你的图片:

    <img src="your_image.png" class="fluid-img">
    

    Here's an example(在 Chrome 中测试)。

    【讨论】:

    • 感谢您的意见。但是,这只会将图像宽度设置为 90%。如果我的容器是1000px,图片宽度是5000px,那么90%的图片宽度还是在1000px以上。我是否错误地解释了您的代码?如果用户将容器的大小调整为 100px 宽度,那么图像将完全超出容器。 (对不起我的英语不好)
    • 将图像上的宽度设置为百分比将其设置为父容器宽度的百分比(不是图像的原始大小)。这是另一个示例:jsbin.com/uxoce4/2 其中最外面的容器设置为 600 像素。我希望我能正确理解这个问题。如果没有,对不起!
    • 哦,我明白了...但是我无法设置 id,因为它是由 tinymce 呈现的。用户插入图片后,它只会呈现为:,其中“alt”属性是可选的。
    • 有一种方法可以将其应用于所有图像。通过执行 .inlineEditor img{}... 下面是一个示例:jsbin.com/uxoce4/4 -- 这样做的缺点是您会将添加的任何图像(无论是小还是大)设置为基于百分比的宽度。
    • 我明白了。我已经尝试过了,但它仍然不能解决我的问题。感谢您的时间和回答 =)
    猜你喜欢
    • 2011-03-16
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多