[英]Sticky sidebar accordion won't scroll propperly
我制作了一個粘性側邊欄(如果看起來很熟悉,請使用w3schools的代碼),並在其中添加了一個手風琴菜單。
目的是使側邊欄永久存在,但不會占據整個屏幕,直到用戶向下滾動到標題下方為止。 如果用戶打開一個手風琴菜單,並且該菜單需要滾動條,則側邊欄應顯示一個單獨的滾動條。
但是,偶爾會發生奇怪的事情。 如果我的頁面內容太多,則僅在側邊欄粘滯時才會顯示側邊欄的滾動條。 如果內容適合一個屏幕(因此粘性方面永遠不會發揮作用),則不會出現側邊欄的滾動條。 相反,它是一個主窗口滾動條,它使我無法正常向下滾動邊欄。
這是代碼:
HTML:
<div id="sidebar" class="sidebar">
<button class="accordion">Grade 1</button>
<div class="panel">
<a href="Note-Types.html" style="border-top: solid silver;">Note Types</a>
<a href="Time-Signatures-Bar-Lines.html" style="border-top: solid silver;">Time Signatures and Bar Lines</a>
<a href="Beaming-Notes.html" style="border-top: solid silver;">Beaming Notes</a>
<a href="Note-Names.html" style="border-top: solid silver;">Note Names</a>
<a href="Rests.html" style="border-top: solid silver;">Rests</a>
<a href="Accidentals.html" style="border-top: solid silver;">Accidentals</a>
<a href="Dots-Ties.html" style="border-top: solid silver;">Dots and Ties</a>
<a href="Semitones-Tones.html" style="border-top: solid silver;">Semitones and Tones</a>
<a href="Scales.html" style="border-top: solid silver;">Scales</a>
<a href="Key-Signatures.html" style="border-top: solid silver;">Key Signatures</a>
<a href="Degrees.html" style="border-top: solid silver;">Degrees</a>
<a href="Intervals.html" style="border-top: solid silver;">Intervals</a>
<a href="Triads.html" style="border-top: solid silver;">Triads</a>
<a href="Foreign Terms.html" style="border-top: solid silver;">Foreign Terms</a>
</div>
<button class="accordion">Grade 2</button>
<div class="panel">
Coming Soon
</div>
</div>
CSS:
#sidebar {
padding-left:5px;
float: left;
width: 200px;
z-index: 1;
overflow-x: hidden;
overflow-y: auto;
height:100%;
border-radius: 20px;
border-top-left-radius:0px;
border-top-right-radius:0px;
font-family: 'Annie Use Your Telescope';
font-size: 18px;
font-weight: bold;
letter-spacing: 1.5px;
}
.accordion {
background-color: rgb(213,218,255);
color:black;
cursor: pointer;
padding: 15px 8px 15px 16px;
text-align: left;
border: none;
outline: none;
transition: 0.4s;
width: 100%;
font-size: inherit;
font-family: inherit;
font-weight: inherit;
letter-spacing: inherit;
}
.panel {
max-height: 0;
overflow: hidden;
transition: max-height 0.4s ease-out;
width: 100%;
font-size: 16px;
}
.sticky{
position: fixed;
top: 0;
width: 100%;
border-radius: 20px;
}
.sticky + .content {
padding-top: 60px;
}
Javascript:
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
window.onscroll = function() {myFunction()};
var navbar = document.getElementById("sidebar");
var sticky = navbar.offsetTop;
function myFunction() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky")
} else {
navbar.classList.remove("sticky");
}
}
該代碼當前正在上線的網站上使用,因此您可以檢查該代碼以方便使用。 唯一不可見的是一些側邊欄內容和CSS,它們分別位於單獨的文件中。 最好的簽出頁面是http://learningmusictheory.co.uk/index.html-單擊側邊欄中的1年級,然后嘗試向下滾動; 和http://learningmusictheory.co.uk/Note-Types.html-單擊側邊欄中的1年級,您必須向下滾動主窗口,直到側邊欄變得固定,然后滾動側邊欄鏈接。
感謝您的寶貴時間,不勝感激。
這似乎可行,它將檢查菜單中的任一“面板”的高度值是否大於#main-content div的高度值,如果是,則忽略該函數原始部分的其余部分:
function myFunction() {
if ((document.getElementsByClassName("panel")[0].clientHeight || document.getElementsByClassName("panel")[1].clientHeight) > document.getElementById("main-content").clientHeight){
return;
} else {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky")
} else {
navbar.classList.remove("sticky");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.