2017-07-03 31 views
0

我在創建佈局時遇到問題,其中有兩個面板與左側面板相對定位,右側面板僅在特定滾動後才固定。另外,如果頁面滾動到達底部而不與頁腳部分的右側面板重疊,則需要調整其高度。使用動態內容進行某些滾動後,固定右側面板的2個面板佈局

design

到目前爲止,我已經做了this,但它打破怎麼把它高度計算時的內容刷新在右手側,或者相比於右側面板左側面板有較小的內容。

jQuery的

$(document).ready(function(){ 
 
\t $(window).on('scroll',function(){ 
 
    \t if($(window).scrollTop() > 120) { 
 
    \t $('.panelright').addClass('fixedpanel'); 
 
     
 
     
 
    } 
 
    else 
 
     \t $('.panelright').removeClass('fixedpanel'); 
 
    }); 
 
});
header{ 
 
    height: 100px; 
 
    border: 1px solid lightgray; 
 
    margin-bottom: 20px; 
 
} 
 
footer { 
 
    height:50px; 
 
    border: 1px solid lightgray; 
 
    clear:both; 
 
    margin-top: 20px; 
 
} 
 
.container { 
 
    width: 600px; 
 
    margin: 0 auto; 
 
    position: relative; 
 
} 
 

 
.panelleft, .panelright { 
 
    width: 45%; 
 
    float:left; 
 
    border: 1px solid lightgray; 
 
    position:relative; 
 
    display:block; 
 
    padding: 10px; 
 
} 
 

 
.fixedpanel { 
 
    position:fixed; 
 
    right:0px; 
 
    top: 10px; 
 
    bottom: 60px; 
 
    overflow: auto; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div> 
 
    <header></header> 
 
    <div class="container"> 
 
    <div class="panelleft"> 
 
     
 
     <p> 
 
     Lrambled 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="panelright"> 
 
     <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> 
 
     <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> 
 
    <footer></footer> 
 
</div>

有幾件事情想要做這裏,我沒有一個工作代碼,但只有我上面共享的小提琴。

  1. 要將右側面板設置爲固定定位後,它會在達到容器後進行特定的滾動。
  2. 當它到達底部時更新它的高度,使它不重疊頁腳。
  3. 只有當左側面板大於視口時才設置其位置。在這種情況下,將右側面板的高度設置爲與左側面板相同,而不考慮其中的內容,並將其溢出CSS屬性設置爲自動。

真的很感謝任何幫助。謝謝。

+0

已經回答了這裏:https://stackoverflow.com/questions/15850271/how-to-make-div-fixed-after-you-scroll-to-that-div –

+0

@ P.Iakovakis我的查詢那篇文章完全不同。我正在嘗試在滾動後修復右側面板,而不是導航欄。此外,問題m正在根據左側面板以及何時到達頁面底部來調整其高度。 – narcs

回答

0

你可以使用

.panelleft, .panelright{ 
    margin-bottom:10px; 

} 使用該網格節不與頁腳重疊。現在我只是解決與頁腳的重疊問題。 見你可以在頁面滾動適用高度,但我不認爲這是一個正確的解決方案.. 如果您有任何仍查詢請評論,我將盡力shortout

+0

感謝您的更新。當頁面沒有達到滾動結束時,我已經將底部邊距應用到10px,一旦到達結束,我應用60px的底部邊距。我的主要問題在於,當左側面板中的內容較少時,它不起作用。 – narcs

+0

謝謝隊友:) 因此,你可以在css .fixedpanel {0} {0} {0} height:70px; } /*這裏您可以根據頁面滾動*/ – Abhijeet

+0

中的內容添加動態高度,這仍然不是問題。我的問題是隻有在左側面板大於視口/用戶可見區域時纔將其設置爲固定位置。 – narcs

0

如果你想panelrightfixed只有當用戶滾動超過120px,也只有當panelleftviewport大,你需要另一個條件添加到if

參見下文或jsFiddle

$(document).ready(function() { 
 
    $(window).on('scroll', function() { 
 

 
    if ($(this).scrollTop() > 120 && $(this).height() < $('.panelleft').height()) { 
 
     $('.panelright').addClass('fixedpanel'); 
 

 
    } else { 
 
     $('.panelright').removeClass('fixedpanel'); 
 
    } 
 

 
    }); 
 
});
header { 
 
    height: 100px; 
 
    border: 1px solid lightgray; 
 
    margin-bottom: 20px; 
 
} 
 

 
footer { 
 
    height: 50px; 
 
    border: 1px solid lightgray; 
 
    clear: both; 
 
    margin-top: 20px; 
 
} 
 

 
.container { 
 
    width: 600px; 
 
    margin: 0 auto; 
 
    position: relative; 
 
} 
 

 
.panelleft, 
 
.panelright { 
 
    width: 45%; 
 
    float: left; 
 
    border: 1px solid lightgray; 
 
    position: relative; 
 
    display: block; 
 
    padding: 10px; 
 
} 
 

 
.fixedpanel { 
 
    position: fixed; 
 
    right: 0px; 
 
    top: 10px; 
 
    bottom: 60px; 
 
    overflow: auto; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div> 
 
    <header></header> 
 
    <div class="container"> 
 
    <div class="panelleft"> 
 

 
     <p> 
 
     Lrambled 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. Lrambled 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="panelright"> 
 
     <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> 
 
     <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> 
 
    <footer></footer> 
 
</div>