使用 jQuery 动态调整容器内图像的大小
我有一个 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类。
I have a div container, and I call it "content_container". This container is able to drag and resize using jQuery UI. Inside this container, I implemented TinyMCE (content text editor). My problem is:
If the user inserts a 2000 pixels x 2000 pixels image, the container max-width is 1000 pixels. Then it will look like this:
____________________
| Container header |
----------------------
| text [image...................image]
| [image...................image]
|____________________|
(I am sorry, I am still developing it in my localhost, and I haven't found a web hosting company yet, thus I can't give you the direct link to see the demo).
Okay, the container is still resizeable, just that, the image size is always 2000 pixels x 2000 pixels. My question is: Is it possible when I resize the "content_container", the image will auto resize and fit into the container width?
If yes, how do I do it?
If no, is there another solution to solve this?
Code
Before TinyMCE, the container code:
<div id="content_container">
<div id="header">The header</div>
<div id="content">
<div id="inlineEditor"></div>
</div>
</div>
After the user enters content (for example, insert the image), the container will become:
<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>
As you can see, I can only manipulate the inlineEditor class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个答案是基于 CSS 的。您是否尝试过像这样对您的图像应用一个类?
还有你的图片:
这是一个示例(在 Chrome 中测试)。
This answer is CSS based. Have you tried applying a class to your image like so?
And your image:
Here's an example (tested in Chrome).
试试这个:
JavaScript 代码:
Try this:
JavaScript code: