简体   繁体   English

使用不同大小的图像调整大小的滑块

[英]Resize slider with different sized images

I have a mobile first slider with 3 types of images: tall, horizontally long and square. 我有一个带有3种图像类型的移动式第一滑块:高,水平长和正方形。 I want the horizontally long image determine the size of slider and then scale and center other ones to fit it's size. 我希望水平的长图像确定滑块的大小,然后缩放并居中放置其他大小以适合其大小。 Using width:100%; height:auto; 使用width:100%; height:auto; width:100%; height:auto; in my CSS code so images will be loaded with same width(100%) and different heights. 在我的CSS代码中,因此图像将以相同的宽度(100%)和不同的高度加载。 Then using JS I want to get height of the image which has max width/height ratio and use it as the height of all images. 然后使用JS,我想获取具有最大宽度/高度比的图像的高度,并将其用作所有图像的高度。 And in the end using width:auto; height:100%; 最后使用width:auto; height:100%; width:auto; height:100%; for images so all of them will fit the height. 用于图像,因此所有图像都适合高度。 Here is the code I used to achieve this: 这是我用来实现此目的的代码:

every slide has the following HTML: 每张幻灯片具有以下HTML:

<div class="img">
      <img src="" alt="">
 </div>

CSS code: CSS代码:

 .img{
     position: relative;
     vertical-align: middle;
}

 .img img{
     width:100%;
     height:auto; 
     position: absolute;
     top: 0;  
     bottom: 0;  
     left: 0;  
     right: 0;  
     margin: auto;
}

JS code: JS代码:

        $(".img img").width = "100%";
        $(".img img").height = "auto";;
        var img_h , Imgs_heights=[] , ratio=[];
        slider.item.find(".img img").each(function(){
            Imgs_heights.push($(this).height());
            ratio.push($(this).width()/(($(this).height())));
        });
        img_h = Math.max.apply(Math,ratio);
        var i = r.indexOf(img_h );
        $(".img").height(Imgs_heights[i]);
        $(".img img").css("height", "100%");
        $(".img img").css("width", "auto");

Everything works just fine but I have problem when I resize the window. 一切正常,但调整窗口大小时出现问题。 In resizing the height of images does not change and keeps the height calculated before. 在调整大小时,图像的高度不会改变,并保持之前计算的高度。 And I need to refresh page to see the result I want. 我需要刷新页面才能看到所需的结果。 How can I get the height to change? 如何获得高度变化? I added the two first lines in JS to force it take width:100%; height:auto; 我在JS中添加了第一行,以强制width:100%; height:auto; width:100%; height:auto; again in resized window but it does not work. 再次在调整大小的窗口中,但是它不起作用。

I will appreciate any help. 我将不胜感激。

If I'm understanding you correctly, can you simply use the Javascript window.resize event to recalculate the image sizes? 如果我对您的理解正确,是否可以仅使用Javascript window.resize事件重新计算图像大小?

From Mozilla https://developer.mozilla.org/en-US/docs/Web/Events/resize 从Mozilla https://developer.mozilla.org/en-US/docs/Web/Events/resize

The resize event fires when the document view (window) has been resized. 调整文档视图(窗口)的大小时,将触发resize事件。

Your code might become something like: 您的代码可能类似于:

function resizeImages(){
   $(".img img").width = "100%";
   $(".img img").height = "auto";
   var img_h , Imgs_heights=[] , ratio=[];
   slider.item.find(".img img").each(function(){
       Imgs_heights.push($(this).height());
       ratio.push($(this).width()/(($(this).height())));
   });
   img_h = Math.max.apply(Math,ratio);
   var i = r.indexOf(img_h );
   $(".img").height(Imgs_heights[i]);
   $(".img img").css("height", "100%");
   $(".img img").css("width", "auto");
}

window.onload = function(){
  resizeImages();
};

window.onresize  = function(){
  resizeImages();
}; 

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

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