简体   繁体   English

jQuery,通过保持恒定高度的宽度来调整图像大小

[英]Jquery, resize image by width keeping constant height

How can I go about resizing/cropping an image so the height is constant but its width gets smaller as the page is resized? 如何调整图像大小/裁剪图像,以使高度不变,但宽度随着页面大小的改变而变小?

The same effect as you can see in the carousel here: 与您在轮播中看到的效果相同:

http://dai.com/ http://dai.com/

Thanks, 谢谢,

Did you looked at the website image closely? 您是否仔细查看了网站图片? Because it's not resizing! 因为它没有调整大小! It's just centered in the middle of the img element as an background property. 它只是作为background属性位于img元素的中间。 When you change the width of the screen you will see that the image center is adjusted.. 更改屏幕宽度时,您会看到图像中心已调整。

It's pretty simple to accomplish this effect by using only the background property: 仅使用background属性即可轻松实现此效果:

background: url("img path") 50% 0 no-repeat;

The background image will get the height of the element. 背景图像将获取元素的高度。

Example: here 示例: 这里

you should bind a function to the "resize" event, 您应该将函数绑定到“调整大小”事件,
and give the image max-width and max-height instead of height and width, 并指定图片的最大宽度和最大高度,而不是高度和宽度,

 function imageresize() {  
 if (($(window).width()) < '800'){  
 $('#img').css('max-width',$(window).width());  
 } else {  
 $('#img').css('max-width','');  
 }  
 }  

 imageresize();

 $(window).resize(function(){
 imageresize();  
 });

I attached a jsFiddle: http://jsfiddle.net/gGBRF/ 我附上了jsFiddle: http//jsfiddle.net/gGBRF/

正如@Sven所说,它并没有被调整大小,只是被居中并调整了浏览器的大小,使得它看起来像是从两边溢出。

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

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