html - 如何使div 内的图像始终为div 的大小?

标签 html css

我已将幻灯片 div 设置为 90% 宽度,因此它始终占据屏幕宽度的 90%,因此它永远不会超过屏幕宽度。唯一的问题是里面的图像不会缩放到 div 的大小。如果源图像较小,它们也较小,或者如果图像较大,则它们只会显示图像的一部分。如何让我的幻灯片中的图像始终是它们所在的 div 的宽度和高度?

.GalleryTXT {
        text-align: center;
        padding: 20px 150px 0px 150px;
    }

    #cssSlider {
        width: 90%;
        height: 400px;
        overflow: hidden;
        border: 4px solid #0DD587;
        margin: 40px auto;
    }

    #sliderImages {
        max-width: 4000px;
        height: 400px;
        overflow: hidden;
        /* animation duration: 16s : 5x ~3s display image + 5x ~0.2s slide */
        animation: slide 16s infinite;
        -moz-animation: slide 16s infinite;
        -webkit-animation: slide 16s infinite;
        -o-animation: slide 16s infinite;
    }

    #sliderImages img {
        float: left;
    }

    @keyframes slide {
        0% {
            margin-left: 0px;
        }
        17% {
            margin-left: 0px;
        }
        20% {
            margin-left: -800px;
        }
        37% {
            margin-left: -800px;
        }
        40% {
            margin-left: -1600px;
        }
        57% {
            margin-left: -1600px;
        }
        60% {
            margin-left: -2400px;
        }
        77% {
            margin-left: -2400px;
        }
        80% {
            margin-left: -3200px;
        }
        97% {
            margin-left: -3200px;
        }
        100% {
            margin-left: 0px;
        }
    }

    @-moz-keyframes slide {
        0% {
            margin-left: 0px;
        }
        17% {
            margin-left: 0px;
        }
        20% {
            margin-left: -800px;
        }
        37% {
            margin-left: -800px;
        }
        40% {
            margin-left: -1600px;
        }
        57% {
            margin-left: -1600px;
        }
        60% {
            margin-left: -2400px;
        }
        77% {
            margin-left: -2400px;
        }
        80% {
            margin-left: -3200px;
        }
        100% {
            margin-left: -3200px;
        }
    }

    @-webkit-keyframes slide {
        0% {
            margin-left: 0px;
        }
        17% {
            margin-left: 0px;
        }
        20% {
            margin-left: -800px;
        }
        37% {
            margin-left: -800px;
        }
        40% {
            margin-left: -1600px;
        }
        57% {
            margin-left: -1600px;
        }
        60% {
            margin-left: -2400px;
        }
        77% {
            margin-left: -2400px;
        }
        80% {
            margin-left: -3200px;
        }
        100% {
            margin-left: -3200px;
        }
    }

    @-o-keyframes slide {
        0% {
            margin-left: 0px;
        }
        17% {
            margin-left: 0px;
        }
        20% {
            margin-left: -800px;
        }
        37% {
            margin-left: -800px;
        }
        40% {
            margin-left: -1600px;
        }
        57% {
            margin-left: -1600px;
        }
        60% {
            margin-left: -2400px;
        }
        77% {
            margin-left: -2400px;
        }
        80% {
            margin-left: -3200px;
        }
        100% {
            margin-left: -3200px;
        }
    }
<div id="cssSlider">
    <div id="sliderImages">
      <img src="http://f.cybrox.eu/codepen/slider/slide4.jpg" alt="" />
      <img src="http://f.cybrox.eu/codepen/slider/slide2.jpg" alt="" />
      <img src="http://f.cybrox.eu/codepen/slider/slide3.jpg" alt="" />
      <img src="http://f.cybrox.eu/codepen/slider/slide4.jpg" alt="" />
      <img src="http://f.cybrox.eu/codepen/slider/slide5.jpg" alt="" />
      <div style="clear:left;"></div>
    </div>
  </div>

  <div class="GalleryTXT">
    <p><b>"Ordered this case just the other day & it arrived the very next day in perfect condition. The case is absolute beautiful & just makes the phone look even better which I thought wasn't even possible!, the lovely dark case helps emphasise the phones design. The case is not slippery at all & I have yet to get any marks/ damage onto it" (★★★★★)</b> - Hellen View (Google Pixel)</p><br>

    <p><b>"Ordered this case just the other day & it arrived the very next day in perfect condition. The case is absolute beautiful & just makes the phone look even better which I thought wasn't even possible!, the lovely dark case helps emphasise the phones design. The case is not slippery at all & I have yet to get any marks/ damage onto it" (★★★★★)</b> - Jordan Kean (iPhone X)</p><br>

    <p><b>"Ordered this case just the other day & it arrived the very next day in perfect condition. The case is absolute beautiful & just makes the phone look even better which I thought wasn't even possible!, the lovely dark case helps emphasise the phones design. The case is not slippery at all & I have yet to get any marks/ damage onto it" (★★★★★)</b> - Nuur Hassan (Samsung S8)</p>
  </div>

  <div id="cssSlider">
    <div id="sliderImages">
      <img src="http://f.cybrox.eu/codepen/slider/slide4.jpg" alt="" />
      <img src="http://f.cybrox.eu/codepen/slider/slide2.jpg" alt="" />
      <img src="http://f.cybrox.eu/codepen/slider/slide3.jpg" alt="" />
      <img src="http://f.cybrox.eu/codepen/slider/slide4.jpg" alt="" />
      <img src="http://f.cybrox.eu/codepen/slider/slide5.jpg" alt="" />
      <div style="clear:left;"></div>
    </div>
  </div>

  <div class="GalleryTXT">
    <p><b>"The MWS Bench case fits my new phone perfectly & is just gorgeous!, the wood has such a natural look to it but doesn't have that rough texture you get from natural wood, just lovely, very pleased with my purchase from MWS, will be coming back for more cases in the future. The website is also very easy to manage & just looks great, well done." (★★★★★)</b> - Shiva Sivanesan (iPhone 8)</p><br>
    <p><b>"This is my second purchase from MWS & it's just as good as my first purchase with the company. The case just looks amazing & fits the phone perfectly down to the smallest detail. The case feels very protective but doesn't feel like it could easily get damaged, very happy with purchase." (★★★★★)</b> - Shiva Sivanesan (iPhone 8)</p>
  </div>

  <div id="cssSlider">
    <div id="sliderImages">
      <img src="http://f.cybrox.eu/codepen/slider/slide1.jpg" alt="" />
      <img src="http://f.cybrox.eu/codepen/slider/slide2.jpg" alt="" />
      <img src="http://f.cybrox.eu/codepen/slider/slide3.jpg" alt="" />
      <img src="http://f.cybrox.eu/codepen/slider/slide4.jpg" alt="" />
      <img src="http://f.cybrox.eu/codepen/slider/slide5.jpg" alt="" />
      <div style="clear:left;"></div>
    </div>
  </div>

代码也可用here

最佳答案

添加 Jquery进入您的元素。

然后试试这个:

$(document).ready(function() {
  debugger;

  var height = $("#sliderImages").height();
  var width = $("#sliderImages").width(); // Here you can make it 90% by just computing a width

  $("#sliderImages img").css({ height: height, width: width });
});

关于html - 如何使div 内的图像始终为div 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48446156/

相关文章:

javascript - 按下 JQM 列表中的按钮

javascript - 淡入后滚动的问题

html - 如何在没有 JS 或 flexbox 的情况下根据 CSS 中的总数选择所有列表元素?

jquery ui 自动完成 css

css - 模具 |在组件中使用 CSS "+ Selector "

jQuery 工具选项卡——改变背景颜色

php - 重新设计页面 : changing the PHP code that generates the HTML

semantic-markup - 是否存在语义差异 <span >'s and <div>' s?

html - 如何在 Rails3 中解析上传的 txt 文件

html - iOS 将 HTML 实体解码为字符串