【问题标题】:Switch position between left and right左右切换位置
【发布时间】:2018-04-09 06:13:26
【问题描述】:

我有多篇文章,每篇文章都有一张图片,我正在使用 Flexbox 来设置页面样式,我想做的是每隔一行在文章和图片之间切换,所以第一行应该是像 (article-image) 然后 (image-article) 然后再 (article-image) 等等 .. 这是第一篇带有样式的文章代码,如果我做错了什么或者你有更好的建议,请告诉我.

div {
  border: 1px dotted red;
  display: block;
}

.article {
  display: -webkit-flex;
  display: flex;
  flex-direction: row;
  -webkit-flex-direction: row;
  width: 100%;
  overflow: hidden;
  padding: 10px;
}

.article-content {
  height: 100%;
  width: 48.6666666%;
  text-align: left;
}

.img-control {
  margin-left: 3.5%;
  width: 48%;
  overflow: hidden;
  align-self: stretch;
}

.img-control .image {
  height: 100%;
  width: 100%;
  background-size: cover;
  background-position: center center;
}
<div class="article">
  <div class="article-content">
    <h3>What is Lorem Ipsum?</h3>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </div>
  <div class="img-control">
    <div class="image" style="background-image: url(//via.placeholder.com/490x338);"></div>
  </div>
</div>

【问题讨论】:

    标签: html image css responsive-design flexbox


    【解决方案1】:

    您可以拥有一个包含两个静态 div 的动态 div,一个带有 article-image 和一个带有 image-article。像这样:

    <div id='dynamic div'>
    <div class="article">
        <div class="article-content">
            <div class="img-control">
            </div>
        </div>
    </div>
    
    <div class="article">
        <div class="img-control">
        </div>
        <div class="article-content">
        </div>
    </div>
    

    【讨论】:

    • 那么动态和 div 类的样式是什么?
    • 样式将与您的一样,尽管 LGSons 的回答是更少且“更干净”的代码。
    【解决方案2】:

    您使用 nth-child 和 Flexbox 的 order 来完成此操作,其中您使用 even 可以每秒(2、4、..)article,然后将 .img-control 放在文本之前,使用order: -1

    div {
      border: 1px dotted red;
      display: block;
    }
    
    .article {
      display: -webkit-flex;
      display: flex;
      flex-direction: row;
      -webkit-flex-direction: row;
      width: 100%;
      overflow: hidden;
      padding: 10px;
    }
    
    .article-content {
      width: 48.6666666%;
      text-align: left;
    }
    
    .article:nth-child(even) .img-control {
      order: -1;
      margin-right: 3.5%;
      margin-left: 0;
    }
    
    .img-control {
      margin-left: 3.5%;
      width: 48%;
      overflow: hidden;
      align-self: stretch;
    }
    
    .img-control .image {
      height: 100%;
      width: 100%;
      background-size: cover;
      background-position: center center;
    }
    <div class="article">
      <div class="article-content">
        <h3>What is Lorem Ipsum?</h3>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
          has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
          publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
      </div>
      <div class="img-control">
        <div class="image" style="background-image: url(//via.placeholder.com/490x338);"></div>
      </div>
    </div>
    <div class="article">
      <div class="article-content">
        <h3>What is Lorem Ipsum?</h3>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
          has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
          publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
      </div>
      <div class="img-control">
        <div class="image" style="background-image: url(//via.placeholder.com/490x338);"></div>
      </div>
    </div>
    <div class="article">
      <div class="article-content">
        <h3>What is Lorem Ipsum?</h3>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
          has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
          publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
      </div>
      <div class="img-control">
        <div class="image" style="background-image: url(//via.placeholder.com/490x338);"></div>
      </div>
    </div>
    <div class="article">
      <div class="article-content">
        <h3>What is Lorem Ipsum?</h3>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
          has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
          publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
      </div>
      <div class="img-control">
        <div class="image" style="background-image: url(//via.placeholder.com/490x338);"></div>
      </div>
    </div>

    【讨论】:

    • 我不知道为什么它在我的页面上不起作用!甚至出于某种原因选择了 .img-control div。我还有其他方法可以做到这一点吗?
    • @Abdal 显示页面链接
    • @Abdal 在该代码中,您还有一个额外的元素&lt;div class="clear under-line"&gt;&lt;/div&gt;,这使得上述nth-child(even) 不起作用,请改用nth-child(3n+3)jsfiddle.net/39j6995s/5
    • @Abdal 今天一觉醒来才发现他们在上面的更新中搞错了,如果超过3条应该是.article:nth-child(4n+3)...jsfiddle.net/39j6995s/6...让我知道这对你有用
    • @Abdal 因为每 1、3、5 等是你的 article,每 2、4、6 等是你的 underline,3 表示从第三个元素开始(即第 2 篇文章)和 4n 平均目标每 4 个元素(将是每 2 篇文章)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多