首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置ajax加载图片的宽度?

如何设置ajax加载图片的宽度?
EN

Stack Overflow用户
提问于 2013-06-24 21:41:34
回答 1查看 1.1K关注 0票数 1

我有一个图片网站,并已实现无限滚动图片。问题是,我不知道如何在加载图像后使用jQuery调整它们的大小。

我使用的是带有jQuery版本1.5的drupal。

这是到目前为止我的代码不能工作的代码:

代码语言:javascript
复制
jQuery(body).delegate("#region-content","onchange",function() {
jQuery('#region-content img').each(function() {
    var desiredWidth = 520; // Max width for the image
    var ratio = 0;  // Used for aspect ratio
    var width = jQuery(this).width();    // Current image width
    var height = jQuery(this).height();  // Current image height



    if(width != desiredWidth){
        ratio = desiredWidth / width;   // get ratio for scaling image
        jQuery(this).css("width", desiredWidth); // Set new width
        jQuery(this).css("height", height * ratio);  // Scale height based on ratio
        height = height * ratio;    // Reset height to match scaled image
        width = width * ratio;    // Reset width to match scaled image
    }

});
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-24 21:47:04

要保持纵横比不变,请仅设置宽度。您可以在css中执行以下操作:

css

代码语言:javascript
复制
#region-content{
  height: 520px;
  overflow: auto;
}

#region-content img{
  max-width: 520px; /* only width */
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17276975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档