简体   繁体   English

当相应的div滚动到时,将类添加到适当的导航选项卡

[英]Add class to appropriate nav tab when cooresponding div is scrolled into

I have read similar questions and researched scrollspy, but I don't believe it will do quite what I'm looking for, since as far as I can tell it can only use bootstrap style highlighting. 我读过类似的问题并研究了scrollspy,但我不相信它会完全满足我的要求,因为据我所知,它只能使用bootstrap样式突出显示。 (If it can do more please let me know!) (如果可以做得更多,请告诉我!)

I have a 4-tab navbar (usually fixed top) and a single-page site. 我有一个4标签导航栏(通常固定在顶部)和一个单页网站。 Each tab corresponds to a different section of the page, and each section has a different background color. 每个选项卡对应于页面的不同部分,并且每个部分具有不同的背景色。 What I'd like to do is change the tab color to be the same as the corresponding section's background color whenever that region is scrolled to (so it will only change color once the new section's top reaches the navbar's bottom.) I have achieved this effect only when the tab is clicked, triggering a scroll event and adding an active class, however the active tab will then remain if clicking is not used, creating the problem. 我想做的是每当该区域滚动到该区域时,将选项卡的颜色更改为与相应部分的背景颜色相同(因此,只有当新部分的顶部到达导航栏的底部时,它才会更改颜色。)仅在单击选项卡,触发滚动事件并添加活动类时才有效,但是如果不使用单击,则活动选项卡将保留,从而产生了问题。

Is there a way to change a variable based off the current scroll location? 有没有办法根据当前滚动位置更改变量? I have tried what I can think of but it hasn't worked yet. 我已经尝试了我能想到的,但是还没有奏效。

JS JS

$(window).on('scroll', function () {
            if ($(window).scrollTop() >= $('#homeContainer').height()) {
                $('.menuDiv').addClass('fixed');
            } else {
                $('.menuDiv').removeClass('fixed');
            }
        });


$("#menuHomeButton").click(function(e){
            $('html, body').animate({
                scrollTop: $('#homeContainer').offset().top
            }, 'slow');
        });

        $("#menuAboutButton").click(function(e){
            $('html, body').animate({
                scrollTop: $('#aboutContainer').offset().top + 1
            }, 'slow');
        });

        $("#menuPortfolioButton").click(function(e){
            $('html, body').animate({
                scrollTop: $('#portfolioContainer').offset().top - $('.menuDiv').height() + 1
            }, 'slow');
        });

        $("#menuContactButton").click(function(e){
            $('html, body').animate({
                scrollTop: $('#contactContainer').offset().top - $('.menuDiv').height() + 1
            }, 'slow');
        });

HTML HTML

<div class="mainContainer">

    <div class="container blue" id="homeContainer">
    </div>
    <div class="menuDiv"><!--
            --><div class="menuItem" id="menuHomeButton" ng-class="{'active':selectedTab === 'home'}" ng-click="selectedTab = 'home'">
        <div class="menuTextDiv"><p>Home</p></div><div class="menuItemColor blue"></div>
    </div><!--
            --><div class="menuItem" id="menuAboutButton" ng-class="{'active2':selectedTab === 'about'}" ng-click="selectedTab = 'about'">
        <div class="menuTextDiv"><p>About</p></div><div class="menuItemColor blue2"></div>
    </div><!--
            --><div class="menuItem" id="menuPortfolioButton" ng-class="{'active3':selectedTab === 'portfolio'}" ng-click="selectedTab = 'portfolio'">
        <div class="menuTextDiv"><p>Portfolio</p></div><div class="menuItemColor blue3"></div>
    </div><!--
            --><div class="menuItem" id="menuContactButton" ng-class="{'active4':selectedTab === 'contact'}" ng-click="selectedTab = 'contact'">
        <div class="menuTextDiv"><p>Contact</p></div><div class="menuItemColor blue4"></div>
    </div><!--
        --></div>
    <div class="container blue2" id="aboutContainer">
    </div>
    <div class="container blue3" id="portfolioContainer">
    </div>
    <div class="container blue4" id="contactContainer">
    </div>
    <div class="container">
    </div>

</div>

Here is a fiddle , but for some reason I couldn't get the ng-click and ng-class to work on it, which changes the tab color. 这是一个小提琴 ,但是由于某种原因,我无法让ng-click和ng-class对其进行处理,这会更改选项卡的颜色。

Here are some images of what it looks like not on js fiddle: What I want and have: http://i.gyazo.com/3c7d6d80a9a490b31e795cacebbaa1a0.png http://i.gyazo.com/1bd597080bdba6ffa34fe18cf5462b74.png What I don't want but still also have: http://i.gyazo.com/d066effabd276d978e4775666a3b5d6c.png 以下是一些关于js小提琴的图片:我想要的东西: http : //i.gyazo.com/3c7d6d80a9a490b31e795cacebbaa1a0.png http://i.gyazo.com/1bd597080bdba6ffa34fe18cf5462b74.png我不想要的想要但仍然有: http : //i.gyazo.com/d066effabd276d978e4775666a3b5d6c.png

If anyone has a solution I'd be extremely greatful! 如果有人有解决方案,我将非常感激! Thank you! 谢谢!

Get the distance of the div from top: 从顶部获取div距离:

distance = $("div").scrollTop()

note: do not use var when declaring distance because than you can't access it inside a function 注意:声明距离时不要使用var ,因为这样您就无法在函数内部访问它

Then check if div has reached the top and add class : 然后检查div是否到达顶部并添加class

$(window).on('scroll', function () { 
    if(distance - $("div").scrollTop() >= distance ){
        //do something
    } 
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM