Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
57 views
in Technique[技术] by (71.8m points)

javascript - How to change the position in html

I have a section with list of div's with image and content, i want to interchange the position of image and content, i can do it for static content, but i am pulling the data from mysql table.

For example

Section 1
Left side - Image  | Right side Content
Section 2
Left side - Content | Right side Image 
Section 3
Left side - Image  | Right side Content
Section 4
Left side - Content | Right side Image 

<div class="section">
  <div class="service">
   <div> <img src="./img/1.jpg"></div>
   <div> <p> Content 1 </p></div>
  </div>

  <div class="service">
    <div> <p> Content 2 </p></div>
    <div> <img src="./img/2.jpg"></div>
  </div>

  <div class="service">
   <div> <img src="./img/3.jpg"></div>
   <div> <p> Content 3 </p></div>
  </div>

  <div class="service">
    <div> <p> Content 4 </p></div>
    <div> <img src="./img/4.jpg"></div>
  </div>

</div>

Above section is dynamic looped from the mysql table

Gallery

id    content       img
1      cxxxx        img1
2      cxxxx        img2
3      cxxxx        img3
4      cxxxx        img1
question from:https://stackoverflow.com/questions/65917158/how-to-change-the-position-in-html

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here 2 examples, the first with order and a second even more simple with column-reverse. Where we are targeting every odd .service:

.section > .service:nth-child(odd){
  display: flex;
  flex-direction: column;
}
.section > .service:nth-child(odd) > div:first-child{
  order: 2;
}
.section > .service:nth-child(odd) > div:last-child{
  order: 1;
}
<div class="section">
  <div class="service">
   <div> <img src="./img/1.jpg"></div>
   <div> <p> Content 1</div>
  </div>

  <div class="service">
    <div> <p> Content 2</div>
    <div> <img src="./img/2.jpg"></div>
  </div>

  <div class="service">
   <div> <img src="./img/3.jpg"></div>
   <div> <p> Content 3</div>
  </div>

  <div class="service">
    <div> <p> Content 4</div>
    <div> <img src="./img/4.jpg"></div>
  </div>

</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...