【问题标题】:JQuery position() and offset() methods not workingJQuery position() 和 offset() 方法不起作用
【发布时间】:2021-10-19 03:48:57
【问题描述】:

我是一名初级程序员,我正在尝试创建这种效果,当我滚动经过 HTML 中的特定元素时,导航栏中按钮的颜色会改变颜色。我认为这样做的正确方法是 jQuery。我在获取页面中元素的位置时遇到问题。我尝试过使用 position() 和 offset() 方法。但似乎两者都不起作用。

我想获取 ID 为“info”和“security”的元素的垂直位置。我有这些方法在某些情况下不是很可靠,但我找不到替代方法。

这是我目前的代码:

   $(window).on('load', function(){ 
      window.loaded = true;
      console.log("LOADED");
    });
    $( window ).scroll(function() {
      if (window.loaded == true){  
        var scrollTop = $(window).scrollTop();
        var infopos = $( "#info" );
        var secpos = $("#security");

        if (scrollTop >= 0 && scrollTop < infopos-25) {
            $( "#navgeneral" ).addClass("active");
            $( "#navinfo" ).removeClass("active");
            $( "#navsecurity" ).removeClass("active");
        }
        else if (scrollTop >= infopos && scrollTop < secpos){
            $( "#navgeneral" ).removeClass("active");
            $( "#navinfo" ).addClass("active");
            $( "#navsecurity" ).removeClass("active");
        }
      }    
    });`

提前感谢您的建议!!

【问题讨论】:

    标签: javascript html jquery css


    【解决方案1】:

    给你。

        //Declare these first
        const infopos = $( "#info" ).scrollTop();
        const secpos = $("#security").scrollTop();
    
        $(window).on('load', function(){ 
          window.loaded = true;
          console.log("LOADED");
        });
        $( window ).scroll(function() {
          if (window.loaded === true){ 
     
            let scrollTop = $(window).scrollTop();
    
            if (scrollTop < infopos-25) {     //scrollTop >= 0 will always be true so skip it
                $( "#navgeneral" ).addClass("active");
                $( "#navinfo" ).removeClass("active");
                $( "#navsecurity" ).removeClass("active");
            }
            else if (scrollTop >= infopos && scrollTop < secpos){
                $( "#navgeneral" ).removeClass("active");
                $( "#navinfo" ).addClass("active");
                $( "#navsecurity" ).removeClass("active");
            }
          }    
        });`
    

    首先要充分了解变量的工作原理,并在声明变量时尽量使用let 而不是var。这将防止意外覆盖全局变量。

    【讨论】:

    • 嘿。谢谢你的回答,但我似乎无法让它工作。 scrollTop() 方法似乎给了我到该元素顶部的距离,但我需要一个元素相对于#content 容器的位置。有什么方法可以以不同的方式使用这种方法,还是我必须使用另一种方法?提前谢谢!
    • @HCook886 如果您想根据其父容器获取元素的位置。试试position(),同时查看position()offset()的文档。希望你能弄清楚。
    • 我可能没有说得很清楚。我的问题是 offset() 和 position() 方法对我不起作用,因为:“注意:jQuery 不支持获取隐藏元素的位置坐标或考虑 文档元素上设置的边距。”我正在寻找替代方案或解决方案。谢谢!
    • @HCook886 This 可能会有所帮助。
    【解决方案2】:

    提到的目标是让 NAV 栏根据您滚动到的位置发生变化。 首先,您需要获取目标 ID 滚动位置。使用下面的代码获取每个 Archor 元素 ID:

    $('#testB').position().top
    

    (在 jQuery 中,position 会返回给你 left 和 top 的值,这里我们只使用 top。) 二、获取当前滚动位置。使用下面的代码:

    currentHeight = $(window).scrollTop();
    

    第三,编写相关的jQuery代码来改变NAV元素。示例JS如下: 这里我们有 5 个 DIV(#testA 到 E)和 4 个导航按钮(#test1 到 4)

    $(window).scroll(function () {
    var currentHeight = $(window).scrollTop();
    if (currentHeight <= $('#testB').position().top) {
        $("#test1").addClass('active-blue');
        $("#test2").removeClass('active-blue');
        $("#test3").removeClass('active-blue');
        $("#test4").removeClass('active-blue');
    } else if (currentHeight <= $('#testC').position().top) {
        $("#test1").removeClass('active-blue');
        $("#test2").addClass('active-blue');
        $("#test3").removeClass('active-blue');
        $("#test4").removeClass('active-blue');
    } else if (currentHeight <= $('#testD').position().top) {
        $("#test1").removeClass('active-blue');
        $("#test2").removeClass('active-blue');
        $("#test3").addClass('active-blue');
        $("#test4").removeClass('active-blue');
    } else {
        $("#test1").removeClass('active-blue');
        $("#test2").removeClass('active-blue');
        $("#test3").removeClass('active-blue');
        $("#test4").addClass('active-blue');
    }
    

    });

    【讨论】:

    • 嗨!谢谢大家的帮助,但是程序的逻辑和我提出的一样。问题是 .position().top 方法不起作用。
    猜你喜欢
    • 2011-06-29
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    相关资源
    最近更新 更多