4

我有一个侧面带有垂直菜单的页面,该菜单链接到页面上的部分元素。我要做的是li在元素滚动到以及部分顶部到达页面顶部时给出一个“活动”类。

所有这一切都很好 - 但是,页面上的最后一个部分元素永远不会到达顶部,因此活动类保留在前一个元素上。

滚动到时如何使最后一个元素成为活动的类?

菜单:

<ul class="program-list">
<li class="program-list program-menu-item menu-item-toddlers">Toddlers</li>
<li class="program-list program-menu-item menu-item-elementry">Elementry</li>
<li class="program-list program-menu-item menu-item-preschool">Preschool</li>
<li class="program-list program-menu-item menu-item-adults">Adults</li>
</ul>

jQuery

<script type="text/javascript">
     jQuery(window).scroll(function(){
                    var scrollTop = jQuery(window).scrollTop();
                    var programTop = jQuery('#programs').offset().top;
                    if(scrollTop >= programTop) {jQuery('ul.program-list li').removeClass('active');} 

                    var toddlersTop = jQuery('#toddlers').offset().top - 10;
                    var elementryTop = jQuery('#elementry').offset().top - 10;
                    var preschoolTop = jQuery('#preschool').offset().top - 10;
                    var adultsTop = jQuery('#adults').offset().top - 10;

                    if(scrollTop >= toddlersTop) {jQuery('ul.program-list li').removeClass('active'); jQuery('li.menu-item-toddlers').addClass('active');}
                    if(scrollTop >= elementryTop){jQuery('ul.program-list li').removeClass('active'); jQuery('li.menu-item-elementry').addClass('active');}
                    if(scrollTop >= preschoolTop){jQuery('ul.program-list li').removeClass('active'); jQuery('li.menu-item-preschool').addClass('active');}
                    if(scrollTop >= adultsTop){jQuery('ul.program-list li').removeClass('active'); jQuery('li.menu-item-adults').addClass('active');}
 });
    </script>
4

2 回答 2

1

adults当您将页面滚动到末尾时,此代码设置为活动状态:

if (scrollTop == jQuery(document).height() - jQuery(window).height())
    {jQuery('ul.program-list li').removeClass('active'); jQuery('li.menu-item-adults').addClass('active');}
于 2013-08-01T21:01:38.897 回答
0

可选地,您可以尝试在底部放置一些东西(底部边距可能有效)或

//html
    <ul class="program-list">
    <li class="program-list program-menu-item menu-item-toddlers">Toddlers</li>
    <li class="program-list program-menu-item menu-item-elementry">Elementry</li>
    <li class="program-list program-menu-item menu-item-preschool">Preschool</li>
    <li class="program-list program-menu-item menu-item-adults">Adults</li>
    <li class="menu-item-x"></li>    
    </ul>



//css
    .menu-item-x
    {
    list-style-type:none;
    }
于 2013-08-08T05:30:51.570 回答