简体   繁体   English

将数据类型值设置为到达视口顶部的元素

[英]Setting data type value to an element when it reaches the top of the viewport

I have a hex colour value stored on each section and when a section reaches the top of the screen (-180px for the header) I want to assign a css property to the header element in order to change the text colour as you scroll through the sections. 我在每个部分上存储了一个十六进制颜色值,并且当一个部分到达屏幕顶部时(标题为-180px),我想为标题元素分配一个css属性,以便在滚动浏览时更改文本颜色部分。 I am not getting any errors and I am having trouble debugging this issue. 我没有收到任何错误,并且在调试此问题时遇到了麻烦。

http://www.amypreston.co.uk/ http://www.amypreston.co.uk/

$(window).load(function() {
    var $header = $("header");
    var numberOfSections = $("section").length;   
    var sectionOffsets = [];
    var sectionColour = $("section").eq(i).data("colour");

    for(var i = 0; i < numberOfSections; i++) {
        sectionOffsets.push($("section").eq(i).offset().top);
    }            

    $(window).scroll(function() {
        var scrollTop = $(this).scrollTop();            

        for(var i = 0; i < numberOfSections; i++) {
            if(scrollTop > sectionOffsets[i] - 180) {
                $header.css('color', 'sectionColour');
            }
        }
    });
});

I dont know if it happened by accident, but the line 我不知道这是不是偶然发生的,但是

var sectionColour = $("section").eq(i).data("colour");

is out of place. 不合适。 it uses a variable i which is only defined in the window scroll handler. 它使用仅在窗口滚动处理程序中定义的变量i

notice that you need to retrieve the section color each time the scroll handler runs, and not only on window load. 请注意,每次滚动处理程序运行时,您不仅需要在窗口加载时,还需要获取节的颜色。 you need to place this line from above in the loop inside the scroll handler. 您需要从上方将这一行放在滚动处理程序内的循环中。

Plus, as stated on the comments, you need to use the sectionColour as a variable, and not as a string like you do now. 另外,如注释中所述,您需要将sectionColour用作变量,而不是像现在一样使用字符串。 the single quote marks must be removed, so 'sectionColour' turns into sectionColour . 必须删除单引号,因此'sectionColour'变为sectionColour

here is your fixed code: 这是您的固定代码:

$(window).load(function() {
    var $header = $("header");
    var numberOfSections = $("section").length;   
    var sectionOffsets = [];

    for(var i = 0; i < numberOfSections; i++) {
        sectionOffsets.push($("section").eq(i).offset().top);
    }            

    $(window).scroll(function() {
        var scrollTop = $(this).scrollTop();            

        for(var i = 0; i < numberOfSections; i++) {
            if(scrollTop > sectionOffsets[i] - 180) {
                var sectionColour = $("section").eq(i).data("colour");
                $header.css('color', sectionColour);
            }
        }
    });
});

On a side note, you could shorten your code into this: 附带一提,您可以将代码缩短为:

$(window).scroll(function () {
     $("section").each(function () {
        if ($(window).scrollTop() > $(this).offset().top - 180) {
            $("header").css('color', $(this).data("colour"));
        }
    });
}).scroll();

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

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