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
304 views
in Technique[技术] by (71.8m points)

javascript - 在小萤幕上使用insertBefore div,在大萤幕上使用insertAfter(insertBefore div on small screens and insertAfter on large screens)

In my example I'm trying to insert the Small Div above the Large Div on large screens only.(在我的示例中,我尝试仅在大屏幕上将“小Div”插入到“大Div”上方。)

Then on window resize I want to insert the same Small Div bellow the Medium Div on small screens only.(然后在窗口调整大小时,我只想在小屏幕上插入相同的“小div”,即“小div”。) The problem is that everytime I resize the window the Small Div keeps being added over and over and I want it to be added only once.(问题是,每次我调整窗口大小时,都会不断添加Small Div,而我只希望添加一次。)

Please note that I'm looking for a JS or jQuery solution.(请注意,我在寻找JS或jQuery解决方案。)

Regards.(问候。)

 var smallDiv = '<div class="small">Small Div</div>'; function moveDiv() { if ($(window).width() < 800) { $(smallDiv).insertBefore(".large"); } else { $(smallDiv).insertAfter(".medium"); } } moveDiv(); $(window).resize(moveDiv); <!-- begin snippet: js hide: false console: true babel: false --> 
 .large { width: 100%; height: 80px; background-color: darkgray; } .medium { width: 100%; height: 50px; background-color: goldenrod; } .small { width: 100%; height: 30px; background-color: aquamarine; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="large">Large Div</div> <div class="medium">Medium Div</div> 

  ask by Nesta translate from so


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

1 Reply

0 votes
by (71.8m points)

Renlado's answers is missing part of the question.(Renlado的答案缺少问题的一部分。)

You can use flex-box as suggested and order elements using order property.(您可以按照建议使用flex-box,并使用order属性来订购元素。)

 .container { display: flex; flex-direction: column; } .container>div { order: 1 } .container>.small { order: 0; } @media only screen and (max-width: 800px) { .container .small { order: 2; } } 
 <div class="container"> <div class="large">Large Div</div> <div class="medium">Medium Div</div> <div class="small">Small Div</div> </div> 


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

...