【问题标题】:How can I dynamically resize a div with a background image inside of it?如何动态调整内部带有背景图像的 div 的大小?
【发布时间】:2015-09-10 11:21:11
【问题描述】:

我想根据我所在的窗口大小动态调整 div 的大小。我正在构建一个单页向下滚动站点,其中包含 5 个部分(所有页面大小)站点,其中包含多个图像.在桌面上,每个页面被分成 2 个,带有 col-6s。在平板电脑/手机上,每半页都堆叠有 col-12s。

在我的桌面版本中,所有 5 个页面在页面左侧 50% 处都有图像,在右侧 50% 处有文本。在平板电脑/移动设备上,图像(又名左)div 堆叠在文本 div(又名右)的顶部。

换句话说,height: 100% 在桌面上看起来很棒,但是当我缩小时,图像比在桌面高度上的 % 越来越少。例如,在 ipad 上,背景图像在 70% div 高度时可能看起来很完美,而在移动设备上则为 50% 左右。我希望 div 始终是使图像看起来完美的高度,并使正确的 div 切换到相同的高度。例如,如果图像在 50% 高度时看起来最好,我希望 div 动态地成为该高度,以及文本 div。

为了更好地展示,这里有一些图片。

100% 高度的桌面版本 - http://i988.photobucket.com/albums/af6/jtbitt/24-inch-desktop-responsive_zps6mwocx8l.png

50% 高度的桌面版本 - http://i988.photobucket.com/albums/af6/jtbitt/desktop-50percent_zpsts2se3jo.png

100% 高度的移动版 - http://i988.photobucket.com/albums/af6/jtbitt/background-size%20cover_zpswc6knbqg.png

50% 高度的移动版 - http://i988.photobucket.com/albums/af6/jtbitt/mobile-50percent_zpsyuxcxdyr.png

HTML -

<section id="about" ng-controller="aboutController">    
  <div class="container-fluid">

    <div class="row about-row">

      <div class="about-left col-xs-12 col-md-6">
        <!-- <img src="assets/images/jay-ocean.jpg" class="col-xs-12"/> -->
      </div>

      <div class="about-right col-xs-12 col-md-6">
        <div class="about-content">
          <div class="about-content-title">
            <h1><strong>I'M JAY.</strong></h1>
          </div>

          <div class="about-content-info">
            <p ng-if="about.firstParagraph">An entrepenurial minded, Full Stack Developer. Whenever I'm up against a challenge that I care about, it gives me a rush. Focusing on the big picture is important to me, but I never forget the smaller details. Anything that is not challenging is boring, and makes me yawn. Anything that is seemingly impossible interests me a lot. I'm ready to get to work.</p>

            <p ng-if="!about.firstParagraph">Currently seeking a Javascript position, using the MEAN stack, in New York City. Being innovative, ambitious, and hard working are values that are very important to me. I want to join a company that has similar values and has goals of reaching ridiculous levels of success, not just modest realistic ones. I love working with a solid team.</p>
          </div>

          <div class="about-button">
            <button ng-if="about.firstParagraph" class="label label-success" ng-click="about.switchParagraph()">MORE =></button>
            <button ng-if="!about.firstParagraph" class="label label-success"><a href="/portfolio">VIEW SKILLS</a></button>
          </div>

          <div class="about-personal-info">
            <h4>Email: jaybittner@gmail.com</h4>
          </div>

          <div class="about-icon">
            <a href="{{ profile.url }}" ng-repeat="profile in about.profiles"><img ng-src="{{ profile.icon }}" /></a>
          </div>
        </div>

      </div>

    </div>

  </div>
</section>

CSS -

#about {
  height: 100%; 
  width: 100%;
  background: rgba(0,97,65,1);
  background: -moz-linear-gradient(left, rgba(0,97,65,1) 0%, rgba(54,135,95,1) 7%, rgba(36,123,85,1) 22%, rgba(0,97,65,1) 53%, rgba(34,121,84,1) 76%, rgba(54,135,95,1) 90%, rgba(54,135,95,1) 100%);
  background: -webkit-gradient(left top, right top, color-stop(0%, rgba(0,97,65,1)), color-stop(7%, rgba(54,135,95,1)), color-stop(22%, rgba(36,123,85,1)), color-stop(53%, rgba(0,97,65,1)), color-stop(76%, rgba(34,121,84,1)), color-stop(90%, rgba(54,135,95,1)), color-stop(100%, rgba(54,135,95,1)));
  background: -webkit-linear-gradient(left, rgba(0,97,65,1) 0%, rgba(54,135,95,1) 7%, rgba(36,123,85,1) 22%, rgba(0,97,65,1) 53%, rgba(34,121,84,1) 76%, rgba(54,135,95,1) 90%, rgba(54,135,95,1) 100%);
  background: -o-linear-gradient(left, rgba(0,97,65,1) 0%, rgba(54,135,95,1) 7%, rgba(36,123,85,1) 22%, rgba(0,97,65,1) 53%, rgba(34,121,84,1) 76%, rgba(54,135,95,1) 90%, rgba(54,135,95,1) 100%);
  background: -ms-linear-gradient(left, rgba(0,97,65,1) 0%, rgba(54,135,95,1) 7%, rgba(36,123,85,1) 22%, rgba(0,97,65,1) 53%, rgba(34,121,84,1) 76%, rgba(54,135,95,1) 90%, rgba(54,135,95,1) 100%);
  background: linear-gradient(to right, rgba(0,97,65,1) 0%, rgba(54,135,95,1) 7%, rgba(36,123,85,1) 22%, rgba(0,97,65,1) 53%, rgba(34,121,84,1) 76%, rgba(54,135,95,1) 90%, rgba(54,135,95,1) 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#006141', endColorstr='#36875f', GradientType=1 );
  background-repeat: no-repeat;
  color: #101010;
}

#about .container-fluid, #about .row {
  height: 100%;
}

.about-left {
  height: 50%;
  background-image: url('../../images/jay-ocean.jpg'); 
  background-repeat: no-repeat;
  background-size: cover;
  border-right: 3px solid #101010;
  border-bottom: 3px solid black;
}

/*.about-left img {
  height: auto;
  max-width: 100%;
}*/

.about-right {
  height: 50%;
  padding-top: 24vh;
  padding-left: 3.4vw;
  text-align: center;
}

.about-content {
  width: 50%;
  margin-left: auto;
  margin-right: auto;
}

.about-content-title {
  padding-bottom: -30px;
}

.about-content-title h1{
  font-size: 3.1vw;
  margin: 3px;
}

.about-content-info p {
  font-size: 1vw;
  word-spacing: 0.3vw;
}

.about-button button {
  color: gray;
  border: 1px solid #101010;
  background-color: #101010;
  font-size: 0.7vw;
}

.about-button a {
  color: gray;
}

.about-personal-info h4 {
  font-size: 1vw;
  word-spacing: 0.3vw;
}

.about-icon img {
  height: 3.5vh;
  width: 1.75vw;
  border-radius: 10px;
  border: 1px solid #101010;
  margin: 3px;
}

【问题讨论】:

    标签: html css


    【解决方案1】:

    我不完全确定我明白你在问什么,但是这样的事情有用吗?

    http://codepen.io/kjantzer/pen/NGGqNm

    关键是将background-image与:

    background-position: center center;
    background-size: cover;
    

    【讨论】:

    • 在我尝试过的所有方法中,这绝对是最好的。每个屏幕切割 50/50 看起来都不错,而且反应灵敏。它是自动的,几乎需要 0 次媒体查询,这正是我的目标。背景位置:center center;修复了平板电脑上的丑陋外观。非常感激。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    相关资源
    最近更新 更多