简体   繁体   English

自动调整图像大小以适应屏幕

[英]Automatic image resizing to fit screen

I have a varying number of images that are display in a row. 我有不同数量的图像连续显示。 I want those images to automatically rescale to fit the screen. 我希望这些图像能够自动重新缩放以适应屏幕。

Example: If I have 1 image it should automatically rescale until it hits the max width or height of the screen (which ever comes first). 示例:如果我有1个图像,它应该自动重新缩放,直到它达到屏幕的最大宽度或高度(这是最先出现的)。 If there are 2 I want them both to fill half the screen. 如果有2个,我希望它们都填满屏幕的一半。

Is there a way to do it and how does it work? 有没有办法做到这一点,它是如何工作的? I did not find anything until now. 直到现在我才找到任何东西。 Thank you. 谢谢。

What I currently got: 我目前得到的:

<!DOCTYPE HTML>
<html>
<head>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>

<body>
     <script src="http://code.jquery.com/jquery.js"></script>
     <script src="js/bootstrap.min.js"></script>

    <div class="row" id="images">
        <div class="col-md-3">
             <img src=1 class="img-responsive">
             <img src=2 class="img-responsive">
             <img src=3 class="img-responsive">
             <img src=4 class="img-responsive">
        </div>
    </div>
</body>
</html>

Please see this fiddle http://jsfiddle.net/3dR3u/ 请看这个小提琴http://jsfiddle.net/3dR3u/

 document.addEventListener("DOMContentLoaded", calculate, false); window.addEventListener("resize", calculate, false); function calculate() { var imgs = document.querySelectorAll(".col-md-3 img"); var count = imgs.length; var wid = Math.floor(window.innerWidth / count); for (i = 1; i <= imgs.length; i++) { document.getElementById('img' + i).style.width = wid + 'px'; if (document.getElementById('img' + i).clientHeight > window.innerHeight) { document.getElementById('img' + i).style.height = window.innerHeight + 'px'; } } } 
 .col-md-3 img { float: left; } body { margin: 0px; } 
 <body> <div class="row" id="images"> <div class="col-md-3"> <img id="img1" src="http://farm4.staticflickr.com/3680/13191485343_88d5da5123.jpg"> <img id="img2" src="http://farm4.staticflickr.com/3680/13191485343_88d5da5123.jpg"> <img id="img3" src="http://farm4.staticflickr.com/3680/13191485343_88d5da5123.jpg"> <img id="img4" src="http://farm4.staticflickr.com/3680/13191485343_88d5da5123.jpg"> </div> </div> </body> 

使用jQuery的可能性是:

$('.col-md-3 img').css('width', 100/$('.col-md-3 img').length+"%");

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

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