【问题标题】:DIV position while scrolling in a DIV在 DIV 中滚动时的 DIV 位置
【发布时间】:2013-06-05 20:38:00
【问题描述】:

我有一个问题要问你。 我正在使用这个名为jScrollPane 的插件。挺好看的,很喜欢。

现在,当我在 jscrollpaned DIV 内向上/向下滚动时,我必须在页面某处获取 div 的顶部位置。如果我使用$(window).scroll();,它就可以工作。但是这个功能不是我需要的。我需要一个在我滚动该 DIV 时调用的函数。

所以$('.scroll-pane').scroll(); 不起作用(它的任何容器都不起作用)。

这是要理解的代码。这是有效的:

$(窗口).scroll(函数() { var head_pos = $('.lister-head').offset(); });

我卡住了。有人可以帮帮我吗?

【问题讨论】:

  • 你看过jScrollPane API吗?他们可能有一些可以帮助你的东西。
  • 我当然做了,但它们都不适合我。

标签: jquery html scroll position


【解决方案1】:

没有一个明确的例子,很难提供帮助,但我想看看那个库,所以我现在也许可以提供帮助:

peduarte 肯定是对的,您的答案是围绕 API,更具体地说是围绕 event fired by that library,这很简单:

您应该根据滚动方式绑定滚动事件,然后您几乎可以在任何地方访问滚动窗格对象的上下文。

我在 JSFiddle 中玩了一下,看看:JSFiddle JsScrollPane sandbox ;)

【讨论】:

    【解决方案2】:

    这里是解决方法,

    <div style="position:fixed; margin-left:300px; font-size:24px; text-decoration:none; font-weight:bold; font-style:normal;">
    <a href="#whatisit">What is it</a>
    <a href="#desc">Description</a>
    <a href="#faq">FAQ</a>
    </div>
    
    <div id="clockbox" style="height:4000px;">
    <h1 id="whatisit" style="height:400px;">What is it</h1>
    <h1 id="desc" style="height:400px;">Description</h1>
    <h1 id="faq" style="height:400px;">FAQ</h1>
    
    
    </div>
    

    还有脚本,

    $(window).scroll(function() {
    
    if(parseInt($('#whatisit').offset().top)  + $('#whatisit').height() >= ($(window).scrollTop())){ 
        $('a').css('color','blue');
        $('a[href="#whatisit"]').css('color','black');
    }
    else if(parseInt($('#desc').offset().top) + $('#desc').height()>= ($(window).scrollTop())){ 
        $('a').css('color','blue');
        $('a[href="#desc"]').css('color','black');
    }
    else if(parseInt($('#faq').offset().top) + $('#faq').height() >= ($(window).scrollTop())){ 
        $('a').css('color','blue');
        $('a[href="#faq"]').css('color','black');
    }
    
    });
    

    您也可以使用 % height 甚至不使用它。不管你会得到什么职位。为了更好地理解,我指定了高度。

    我想你会想出一个办法来改变它。干杯:)。

    【讨论】:

      猜你喜欢
      • 2012-02-24
      • 2015-06-11
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 2015-12-30
      相关资源
      最近更新 更多