简体   繁体   English

平滑滚动插件未设置活动状态(JS修改)

[英]Active state not being set by smooth-scroll plugin (JS modification)

I'm having some trouble with a script which takes care of smooth scrolling as well as the active state on my main navigation. 我在使用脚本时遇到了麻烦,该脚本可以平滑滚动以及主导航上的活动状态。 Plugin: http://tinyurl.com/amz4kob 插件: http//tinyurl.com/amz4kob

Please note that the navigation bar is fixed so effectively has no height. 请注意,导航栏是固定的,因此实际上没有高度。

I've got two issues which I can't seem to overcome: 我有两个似乎无法克服的问题:

  1. On page load the active state is applied to the contact link. 在页面加载时,活动状态将应用于联系链接。 If you scroll down 1px the active state is correctly applied to the home link. 如果向下滚动1像素,则活动状态将正确地应用于主链接。
  2. I can't for the life of me figure out how to modify the script to pay attention to anchors within an element with a certain ID? 我一辈子都无法弄清楚如何修改脚本来注意具有特定ID的元素中的锚点? ie I only want this script to apply the active state to the elements within the tag. 即我只希望此脚本将活动状态应用于标签中的元素。

Any help would be greatly appreciated. 任何帮助将不胜感激。

@rrfive @rrfive

To make life easy here is the commented script: 为了使生活更轻松,这里是带注释的脚本:

$(document).ready(function() {
//Get Sections top position
function getTargetTop(elem){

    //gets the id of the section header
    //from the navigation's href e.g. ("#html")
    var id = elem.attr("href");

    //Height of the navigation
    var offset = 0;

    //Gets the distance from the top and subtracts the height of the nav.
    return $(id).offset().top - offset;
}

//Smooth scroll when user click link that starts with #
$('a[href^="#"]').click(function(event) {

    //gets the distance from the top of the section refenced in the href.
    var target = getTargetTop($(this));

    //scrolls to that section.
    $('html, body').animate({scrollTop:target}, 500);

    //prevent the browser from jumping down to section.
    event.preventDefault();
});

//Pulling sections from main nav.
var sections = $('a[href^="#"]');

// Go through each section to see if it's at the top.
// if it is add an active class
function checkSectionSelected(scrolledTo){
    //How close the top has to be to the section.
    var threshold = 54;

    var i;

    for (i = 0; i < sections.length; i++) {

        //get next nav item
        var section = $(sections[i]);

        //get the distance from top
        var target = getTargetTop(section);

        //Check if section is at the top of the page.
        if (scrolledTo > target - threshold && scrolledTo < target + threshold) {
            sections.removeClass("active");
            section.addClass("active");
        }
    };
}

//Check if page is already scrolled to a section.
checkSectionSelected($(window).scrollTop());

$(window).scroll(function(e){
    checkSectionSelected($(window).scrollTop())
});

}); });

The plugin you're using checks the position of the <div class="section"></div> elements on the page, but because you've made them display:none; 您使用的插件会检查<div class="section"></div>元素在页面上的位置,但是因为您已将它们display:none; , all the sections are returning "0 pixels" from the top of the page, and since the "CONTACT" section is the last on the page, it's stopping there. ,则所有部分都从页面顶部返回“ 0像素”,并且由于“接触”部分是页面的最后一个,因此它就在那里停止了。

So, simply remove display:none; 因此,只需删除display:none; from .section in your CSS and it'll work fine. 从CSS中的.section ,它将可以正常工作。

.section {
    /*display: none; <-- Comment this line out. */
    height: 100%;
    min-width: 990px;
}

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

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