简体   繁体   English

自动调整图像大小以适合Flex Slider

[英]Automatically resize image to fit Flex Slider

I am learning how to use Flex Slider to make image sliders and carousels. 我正在学习如何使用Flex Slider制作图像滑块和旋转木马。 However, Flex only seems to be good when all the images are of the exact same width and height. 然而,当所有图像具有完全相同的宽度和高度时,Flex似乎只是好的。

The problem can be seen in the following website: 问题可以在以下网站看到:

https://dl.dropboxusercontent.com/u/785815/bastion/index.html https://dl.dropboxusercontent.com/u/785815/bastion/index.html

The images are quite big, so please be patient while loading. 图像非常大,所以请在装载时保持耐心。

My test website includes several images, with different width and height. 我的测试网站包括几个不同宽度和高度的图像。 To fix this I have to re-size them to have the same width and height, and I also need to center them because of the width-height ratios. 为了解决这个问题,我必须将它们重新调整为相同的宽度和高度,并且由于宽高比的原因,我还需要将它们居中。

So, I would like to know if: 所以,我想知道是否:

  1. Is there a way for Flex Slider to do this? Flex Slider有没有办法做到这一点?
  2. Is there a way to do it without changing the library's code? 有没有办法在更改库代码的情况下执行此操作?

To answers this question, I found the follow articles: 为了回答这个问题,我找到了以下文章:

I appreciate any possible help, Pedro. 我很感激任何可能的帮助,佩德罗。

To solve this problem I'm using CSS - fastest & simplest way I think... 为了解决这个问题,我正在使用CSS - 我认为最快最简单的方法......

@media only screen and (max-width: 480px) {
    #your_slider_id_or_image_id {width:000px;height:000px;}
}
@media only screen and (min-width: 480px) and (max-width: 768px) {
    #your_slider_id_or_image_id {width:000px;height:000px;}
}
@media only screen and (min-width: 768px) and (max-width: 959px) {
    #your_slider_id_or_image_id {width:000px;height:000px;}
}
@media only screen and (min-width: 959px) {
    #your_slider_id_or_image_id {width:000px;height:000px;}
}

Well, I wasted a lot of time with this, so I might as well give it a try in my own question. 好吧,我浪费了很多时间,所以我不妨试试我自己的问题。 I am solving this issue with css3: 我用css3解决了这个问题:

@media only screen and (min-width: 959px) {
    .img {
        max-width: 50%;
        max-height: 50%;
        display: block;
        margin-left: auto;
                margin-right: auto;
    }
}

In this case, if the physical screen of the user had width > 959px (mine has) this will be the css applied to all the images. 在这种情况下,如果用户的物理屏幕宽度> 959px(我有),这将是应用于所有图像的css。 The following HTML illustrates this: 以下HTML说明了这一点:

<ul class="slides">
                    <li>
                            <img src="image/Bastion_Digital_Art_Pack_PNG/Bastion_0_Foreward.png" />
                    </li>
                    <li>
                        <img src="image/Bastion_Digital_Art_Pack_PNG/Bastion_1_Calamity.png" />
                    </li>
                    <li>
                        <img src="image/Bastion_Digital_Art_Pack_PNG/Bastion_1_CalamityKid.png" />
                    </li>
                    <li>
                        <img src="image/Bastion_Digital_Art_Pack_PNG/Bastion_1_ColdWarKid.png" />
                    </li>
                    <li>
                        <img src="image/Bastion_Digital_Art_Pack_PNG/Bastion_1_KidThinking.png" />
                    </li>
                    <li>
                        <img src="image/Bastion_Digital_Art_Pack_PNG/Bastion_1_KidVsScumbag.png" />
                    </li>
                </ul>
            </div>

This will resize ALL IMAGES (ignoring size difference) to 50% of their original size, and will only center then HORIZONTALLY. 这会将所有图像(忽略大小差异)调整为其原始大小的50%,并且仅在HORIZONTALLY中心。 Turns out that to center them vertically, and to resize them depending on their t 结果是将它们垂直居中,并根据它们的t来调整它们的大小

Alternatively you can crop your images by adding the following properties to existing selectors : 或者,您可以通过向现有选择器添加以下属性裁剪图像:

.flexslider .slides > li {
  overflow:hidden;
  position:relative;
  height:400px; /* desired height here */
}
.flexslider .slides img {
  height:auto;
}

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

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