2017-08-10 59 views
0

我正在尋找以CSS響應方式安排Intranet頁面。我試圖使用@media斷點重新排列四個基本要素(嵌套在兩個div)以下面的方式:在CSS中重新排列DIV從側面到自上而下

@media只有屏幕和(最小寬度:1100px)/ 桌面模式/ enter image description here

@media屏幕和(最大寬度:1099px)/ 移動設備/ enter image description here

正如我使用此代碼,其包括jQuery來某些小工具添加到個體的div我已經儘可能得到:

<script type="text/javascript"> 
 

 
var adhcal = '<iframe src="..."</iframe>' 
 
var clincal = '<iframe src="..."</iframe>'; 
 
var timeUrl = '<iframe id = "timeTrack" src="...</iframe>'; 
 

 
function start() 
 
{ 
 
    
 
var dOne = $('<div>').addClass('divOne'); 
 
var dTwo = $('<div>').addClass('divTwo'); 
 
$('body').append(dOne).append(dTwo); 
 
    
 
    
 
var timeDiv = $('<div id = "timeDiv"></div>').addClass('timeDiv'); 
 
dTwo.append(timeDiv); 
 
var timeTrack = $(timeUrl); 
 
timeDiv.append(timeTrack) 
 
timeTrack.load(function(){ 
 
    
 
try{ 
 
    var timeHeight = document.getElementById('timeTrack').contentWindow.document; 
 
    var body = timeHeight.body, html = timeHeight.documentElement; 
 
    var height = Math.max(body.scrollHeight, body.offsetHeight, 
 
          html.clientHeight, html.scrollHeight, html.offsetHeight); 
 
     } 
 
    catch(e){var height = 500;} 
 
    
 
    timeDiv.height(height);}); 
 
    
 
var adhDiv = $('<div id = "adhDiv"></div>').addClass('adhDiv'); 
 
dTwo.append(adhDiv); 
 
adhcal = adhcal.replace('height="100%"', 'height="'+((adhDiv.height() > 0)?adhDiv.height():'600')+'"'); 
 
adhDiv.append(adhcal); 
 
    
 
var clinDiv = $('<div id = "clinDiv"></div>').addClass('clinDiv'); 
 
dTwo.append(clinDiv); 
 
clincal = clincal.replace('height="100%"', 'height="'+((clinDiv.height() > 0) ? clinDiv.height():'600')+'"'); 
 
clinDiv.append(clincal); 
 
    
 
var postDiv = $('<div id = "postDiv"></div>').addClass('postDiv'); 
 
dOne.append(postDiv); 
 
for(var p = 0; p < DATA.posts.length; p++) 
 
{ 
 
    postDiv.append('<h2>'+DATA.posts[p].title 
 
    +'</h2><p class = "date">Published: '+moment(new Date(DATA.posts[p].date)).format('L') 
 
    +'</p><p class="post">' 
 
    +DATA.posts[p].html 
 
    +'</p>').append('<hr>'); 
 
} 
 
    
 
} 
 
</script>
<style> 
 

 

 
.body{ 
 
    overflow:auto; 
 
} 
 
    H2{ 
 
    font-size: 2.00rem; 
 
    font-family: 'Roboto', sans-serif; 
 
    font-weight: 300; 
 
     } 
 
     
 
    .date{ 
 
    font-size: 1.00rem; 
 
    color: #595959; 
 
    font-family: 'Roboto', sans-serif; 
 
    font-weight: 300; 
 
    
 
    } 
 
    .post{ 
 
    font-size: 1.50rem; 
 
    font-family: 'Roboto', sans-serif; 
 
    font-weight: 300; 
 
    
 
    } 
 

 
@media only screen and (min-width: 1100px) { 
 
      
 
    .divOne{ 
 
      float:left; 
 
      max-width:60% 
 
      } 
 

 
    .divTwo{ 
 
      float:right; 
 
      max-width: 30%; 
 
      } 
 

 
    .adhDiv { 
 
     max-width: 100%; 
 
      height: 400px; 
 

 
      } 
 

 
    .clinDiv { 
 
     max-width: 100%; 
 
      height: 400px; 
 
    
 
      } 
 

 
    .postDiv { 
 
     max-width: 100%; 
 
      height: 900px; 
 
     overflow: auto; 
 
     float: left; 
 
      } 
 

 
    .timeDiv { 
 
     max-width: 100%; 
 
      height: 200px; 
 

 
      } 
 
} 
 

 
@media only screen and (max-width: 1099px){ 
 

 
    .divOne{ 
 

 
      display: flex; 
 
      } 
 

 
    .divTwo{ 
 
      position: absolute; 
 
      top: 0; 
 
      display: flex; 
 
      width: 100%; 
 
      } 
 

 
    .adhDiv { 
 
     max-width: 100%; 
 
      height: 400px; 
 

 
      } 
 

 
    .clinDiv { 
 
     max-width: 100%; 
 
      height: 400px; 
 
    
 
      } 
 

 
    .postDiv { 
 
     max-width: 100%; 
 
      height: 900px; 
 
     overflow: auto; 
 
     float: left; 
 
      } 
 

 
    .timeDiv { 
 
     max-width: 100%; 
 
      height: 200px; 
 

 
      } 
 
} 
 

 
</style>
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <base target="_top"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
    <script>var DATA = <?!= JSON.stringify(dataFromServerTemplate) ?>;</script> 
 
    <?!= HtmlService.createHtmlOutputFromFile('CSS').getContent(); ?> 
 
    </head> 
 
    <body onload="start()"> 
 
    <?!= HtmlService.createHtmlOutputFromFile('JS-Intranet').getContent();?> 
 
    </body> 
 
</html>

目前,該代碼使得我想在桌面模式的方式,但1100px下,在2區內容的上半部分將被切斷,就好像它重疊屏幕頂部。我嘗試過的任何其他技巧只是將Div 1推到Div 2的左邊並離開屏幕。任何人都可以看到我失蹤的東西嗎?

謝謝!

回答

1

在您的媒體查詢中,將width: 100%添加到第一個DIV並刪除position: absolute來自第二個DIV。

此外,添加flex-direction: column;到第二個div得到垂直堆疊和flex-direction: column-reverse;其容器(它的類我提出了以下,但具有包住兩個div),以堆疊時,你得到的DIV的相反順序:

.container { 
    display: flex; 
    flex-direction: column-reverse; 
}   
.divOne { 
    display: flex; 
    width: 100%;    
} 
.divTwo { 
    display: flex; 
    flex-direction: column; 
    width: 100%; 
} 
1

這是一個簡化的展示,它將如何工作。

還要注意的是display:flex;不會在所有的瀏覽器 - 他們中的一些提供供應商前綴,請檢查:http://caniuse.com/#search=flexhttp://autoprefixer.github.io/

要了解更多關於Flexbox的,在這裏看到:https://css-tricks.com/snippets/css/a-guide-to-flexbox/

.container{ 
 
    display:flex; 
 
    flex-flow:row wrap; 
 
    text-align:center; // only for code snippet, not needed when contents are there 
 
} 
 
.divOne{ 
 
    order:10; 
 
    background:#F1D2E0; 
 
    width:100%; 
 
} 
 
.divTwo{ 
 
    order:5; 
 
    background:#C1E7CD; 
 
    width:100%; 
 
} 
 
.adhDiv, 
 
.clinDiv, 
 
.postDiv, 
 
.timeDiv { 
 
    height: 400px; 
 
    background:#C1DDE7; 
 
    margin:2em; 
 
} 
 

 
@media only screen and (min-width: 1100px) { 
 
    .divOne{ 
 
    flex:1; 
 
    flex-basis: 60%; 
 
    order:5; 
 
    } 
 
    .divTwo{ 
 
    flex:1; 
 
    flex-basis: 40%; 
 
    order:10; 
 
    } 
 
}
<div class="container"> 
 
    <div class="divOne"> 
 
    <h2>Div1</h2> 
 
    <div class="postDiv"> 
 
     <h3>Post</h3> 
 
    </div> 
 
    </div> 
 
    <div class="divTwo"> 
 
    <h2>Div2</h2> 
 
    <div class="timeDiv"> 
 
     <h3>Time</h3> 
 
    </div> 
 
    <div class="adhDiv"> 
 
     <h3>Adh</h3> 
 
    </div> 
 
    <div class="clinDiv"> 
 
     <h3>Clin</h3> 
 
    </div> 
 
    </div> 
 
</div>

-1

謝謝,托馬斯。我對代碼進行了修飾,並從JS小提琴中添加了一些,以便在下面找出我的解決方案。我喜歡它!謝謝!

<script type="text/javascript"> 
 

 
var adhcal = '<iframe src="..."></iframe>'; 
 
var clincal = '<iframe src="..."></iframe>'; 
 
var timeUrl = '<iframe src="..."></iframe>'; 
 
var scoreImg = '<img width = "94%" src="...">'; 
 
function start() 
 
{ 
 
    
 
var dOne = $('<div>').addClass('divOne'); 
 
var dTwo = $('<div>').addClass('divTwo'); 
 
var container = $('<div>').addClass('container'); 
 
container.append(dOne).append(dTwo); 
 
$('body').append(container); 
 
    
 
var timeDiv = $('<div id = "timeDiv"></div>').addClass('timeDiv'); 
 
dTwo.append(timeDiv); 
 
var timeTrack = $(timeUrl); 
 
timeDiv.append(timeTrack) 
 
timeTrack.load(function(){ 
 
    
 
try{ 
 
    var timeHeight = document.getElementById('timeTrack').contentWindow.document; 
 
    var body = timeHeight.body, html = timeHeight.documentElement; 
 
    var height = Math.max(body.scrollHeight, body.offsetHeight, 
 
          html.clientHeight, html.scrollHeight, html.offsetHeight); 
 
     } 
 
    catch(e){var height = 500;} 
 
    
 
    timeDiv.height(height);}); 
 
    
 
var adhDiv = $('<div id = "adhDiv"></div>').addClass('adhDiv'); 
 
dTwo.append(adhDiv); 
 
adhcal = adhcal.replace('height="100%"', 'height="'+((adhDiv.height() > 0)?adhDiv.height():'400')+'"'); 
 
adhDiv.append(adhcal); 
 
    
 
var clinDiv = $('<div id = "clinDiv"></div>').addClass('clinDiv'); 
 
dTwo.append(clinDiv); 
 
clincal = clincal.replace('height="100%"', 'height="'+((clinDiv.height() > 0) ? clinDiv.height():'400')+'"'); 
 
clinDiv.append(clincal); 
 
    
 
var wrapDiv = $('<div id = "wrapDiv"></div>').addClass('wrapDiv'); 
 
var postDiv = $('<div id = "postDiv"></div>').addClass('postDiv'); 
 
wrapDiv.append('<H1>Blog Title</H1>').append(postDiv); 
 
dOne.append(wrapDiv); 
 
for(var p = 0; p < DATA.posts.length; p++) 
 
{ 
 
    postDiv.append('<h2>'+DATA.posts[p].title 
 
    +'</h2><p class = "date">Published: '+moment(new Date(DATA.posts[p].date)).format('L') 
 
    +'</p><p class="post">' 
 
    +DATA.posts[p].html 
 
    +'</p>').append('<hr>'); 
 
} 
 
var scoreDiv = $('<div id = "scoreDiv"></div>').addClass('scoreDiv'); 
 
dTwo.append(scoreDiv); 
 
scoreDiv.append(scoreImg); 
 

 
} 
 
</script>
<style> 
 

 
    H2{ 
 
    font-size: 2.00rem; 
 
    font-family: 'Roboto', sans-serif; 
 
    font-weight: 300; 
 
     } 
 
    H1{ 
 
     font-size: 2.50rem; 
 
     font-family: 'Roboto', sans-serif; 
 
     font-weight: 325; 
 
    }  
 
.date{ 
 
    font-size: 1.00rem; 
 
    color: #595959; 
 
    font-family: 'Roboto', sans-serif; 
 
    font-weight: 300; 
 
    
 
    } 
 
    .post{ 
 
    font-size: 1.50rem; 
 
    font-family: 'Roboto', sans-serif; 
 
    font-weight: 300; 
 
    } 
 
    
 

 
    .postDiv, 
 
    { 
 
    margin:20em; 
 
    overflow-y: hidden; 
 
    overflow-x: hidden; 
 
    } 
 
    
 
    .timeDiv, 
 
    .scoreDiv,{ 
 
    height: 400px; 
 
    margin:2em; 
 
    width: 100%; 
 
      } 
 
      
 
    .adhDiv, 
 
    .clinDiv, 
 
    { 
 
    height: 200px; 
 
    margin:2em; 
 
    width: 100%; 
 
    } 
 

 
@media only screen and (min-width: 768px) { 
 
    .container{ 
 
      width:98%; 
 
      } 
 
    .divOne{ 
 
      float: left; 
 
      left:0; 
 
      max-width:69% 
 
      } 
 
    .divTwo{ 
 
      float: right; 
 
      max-width: 30%; 
 
      } 
 
    .wrapDiv { 
 
      position: relative; 
 
     max-height: 1200px; 
 
      padding: 0; 
 
      margin: 5% 5% 5% 5%; 
 
      overflow-y: auto; 
 
      overflow-x: hidden; 
 
      } 
 
} 
 
@media only screen and (max-width: 767px) { 
 

 
    .container{ 
 
    width:100%; 
 
    display:flex; 
 
    flex-flow:row wrap; 
 
    } 
 
    .divOne{ 
 
    order:5; 
 
    width:100%; 
 
    background-color: #f9f9f9; 
 
      } 
 
      
 
    .divTwo{ 
 
    display: inline-block; 
 
    width:100%; 
 
      } 
 
    
 
    .postDiv 
 
      {width: 100%} 
 
    .timeDiv { 
 
    
 
    .wrapDiv { 
 
    position: relative; 
 
    max-height: 600px; 
 
    padding: 0; 
 
    margin: 5% 5% 5% 5%; 
 
    overflow-y: auto; 
 
    overflow-x: hidden; 
 
} 
 

 
    height: 400px; 
 
    width: 100%; 
 
      } 
 
    .adhDiv, 
 
    .clinDiv, 
 
      { 
 
    position: relative; padding-bottom: 75%; height: 0; overflow: hidden; 
 
      } 
 
} 
 
</style>
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <base target="_top"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
    <script>var DATA = <?!= JSON.stringify(dataFromServerTemplate) ?>;</script> 
 
    <?!= HtmlService.createHtmlOutputFromFile('CSS').getContent(); ?> 
 
    </head> 
 
    <body onload="start()"> 
 
    <?!= HtmlService.createHtmlOutputFromFile('Scripts').getContent();?> 
 
    <?!= HtmlService.createHtmlOutputFromFile('JS-Intranet').getContent();?> 
 
    </body> 
 
</html>

+0

這是爲什麼downvoted?我剛剛分享了對我有用的東西.... –

+0

它的原因是其中有太多與該問題無關的其他代碼 - 因此很難閱讀並保持專注。 –