粘着的侧边栏手风琴无法正确滚动

时间:2018-07-12 15:00:09

标签: javascript html css

我制作了一个粘性侧边栏(如果看起来很熟悉,请使用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级,您必须向下滚动主窗口,直到侧边栏变得粘滞,然后滚动侧边栏链接。

非常感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:0)

这似乎有效,它检查菜单中的“面板”中的任何一个是否具有比#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");
       }
    }

 }
相关问题