简体   繁体   English

根据视口宽度动态调整图像高度

[英]Resize an image height dynamically according to viewport width

How can I use jQuery to dynamically change the height of an image using the width of viewport. 我如何使用jQuery通过视口的宽度动态更改图像的高度。

For a example 举个例子

When website width is 450px i want to change image width from 300 to 100. 当网站宽度为450px时,我想将图像宽度从300更改为100。

Also I'm using timthumb to resize my images. 另外,我正在使用timthumb调整图像大小。

HTML code HTML代码

<img src="timthumb.php?src=myimage.jpg&amp;h=300&amp;w=750&amp;q=100" class="MyImages">

How can I use jQuery to do this? 我如何使用jQuery来做到这一点?

This should work. 这应该工作。 Just add the src-attribute after jQuery gets the viewport width and update it on window resize. 只需在jQuery获取视口宽度后添加src属性,然后在调整窗口大小时对其进行更新。

<img id="img1" src="" class="MyImages">
<script type="text/javascript">
$(function() {
  update_image();
  $( window ).resize(function() {
    update_image();
  });
});
function update_image(){
  var width = $( window ).width();
  if(width<450) { var picwidth=100; } else { var picwidth=300; }
  $('#img1').attr('src','timthumb.php?src=myimage.jpg&h=300w='+picwidth+'&q=100');
}
</script> 

To answer your question: Edit your images to look like this: 回答您的问题:编辑图像使其看起来像这样:

<img src="" class="MyImages" data-src="myimage.jpg">

Now you get the right sizes using jQuery: 现在,您可以使用jQuery获得正确的尺寸:

<script type="text/javascript">
$(function() {
  $('img').each(function(){
    update_image(this);
  });
  $( window ).resize(function() {
    $('img').each(function(){
      update_image(this);
    });
  });
});
function update_image(e){
  var width = $( window ).width();
  if(width<450) { var picheight=100; } else { var picheight=300; }
  var image = $(e).attr('data-src');
  $(e).attr('src','timthumb.php?src='+image+'&h='+picheight+'&q=100');
}
</script> 

Like this all your images are updated when you resize the viewport. 这样,当您调整视口大小时,所有图像都会更新。

you can write CSS3 media queries if you don't compatibility of CSS2 based old browsers (like IE7). 如果您不兼容基于CSS2的旧浏览器(例如IE7),则可以编写CSS3媒体查询。

for Eg: 例如:

@media (max-width: 450px) {
    .MyImages{width:100px;}
}

if you want pure javascript / jQuery based solution: 如果您想要纯基于javascript / jQuery的解决方案:

function viewport() {
    var e = window, a = 'inner';
    if (!('innerWidth' in window )) {
        a = 'client';
        e = document.documentElement || document.body;
    }
    return { width : e[ a+'Width' ] , height : e[ a+'Height' ] };
}

$(function(){
   if(viewport().width<=450){
     $(".MyImages").css("width":"100px");
   }
});

if you want to make sure it should work even on browser resize: 如果要确保即使在调整浏览器大小时也可以使用:

$(window).on("resize",function(){
   if(viewport().width<=450){
     $(".MyImages").css("width":"100px");
   }
});

Caution: JavaScript way is more expensive than CSS3 solution on Browser. 注意:JavaScript方式比Browser上的CSS3解决方案昂贵。

You don't need jQuery for that. 您不需要jQuery。 Just set the width and height property of your image to the values you want it to be: 只需将图像的width和height属性设置为所需的值即可:

 <img src="myimage.jpg" id="varimage" width="100" height="200">

 ....


 var img = getElementById("varimage");
 img.height = 400;
 img.width = 200;

and jQuery (as that's what you asked for): 和jQuery(这就是您所要求的):

 $("#varimage").width(400);
 $("#varimage").height(200);

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

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