简体   繁体   English

在DIV中滚动时的DIV位置

[英]DIV position while scrolling in a DIV

I have a question for you. 我有一个问题问你。 I'm using this plugin called jScrollPane . 我正在使用名为jScrollPane的插件。 It's pretty cool, I love it. 非常酷,我喜欢它。

Now, I have to get the top position of a div somewhere in the page when I scroll up/down INSIDE the jscrollpaned DIV. 现在,当我在jscrollpaned DIV中向上/向下滚动时,必须在页面的某处获得div的最高位置。 If I use $(window).scroll(); 如果我使用$(window).scroll(); it's just working. 它只是在工作。 But this function is not what I need. 但是这个功能不是我所需要的。 I need a function which called when I scroll in that DIV. 我需要在该DIV中滚动时调用的函数。

So $('.scroll-pane').scroll(); 所以$('.scroll-pane').scroll(); does not work (neither any of its container). 不起作用(两个容器都不起作用)。

Here is the code to understand. 这是要理解的代码。 This is working: 这正在工作:

$(window).scroll(function() {
    var head_pos = $('.lister-head').offset();
});

I stucked. 我卡住了。 Can somebody help me, please? 有人可以帮我吗?

here is the workaround, 这是解决方法,

<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>

And the script, 还有脚本

$(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');
}

});

You can use % height also or even don't use it. 您也可以使用%height,甚至不使用它。 No matter what you will get the position. 无论您将获得什么职位。 For better understanding I have specified the height. 为了更好地理解,我指定了高度。

I think you will get an idea to bend it your way. 我认为您会有所想法的。 Cheers :). 干杯:)。

Without a clear example, hard to help, but i wanted to have a look to that library, so i can maybe help now : 没有一个清晰的例子,很难帮助,但是我想看看那个图书馆,所以我现在可以提供帮助:

peduarte was surely right, your answer is around the API, and more specifically around event fired by that library , which is quite simple: peduarte肯定是正确的,您的答案是关于API的,更具体地说,是关于那个库引发的事件的 ,这很简单:

You should bind the event for scrolling, depending on the way you are scrolling, and you have then access to the context of the scrollpane object almost everywhere. 您应该绑定事件进行滚动,具体取决于滚动方式,然后几乎可以在任何地方访问滚动窗格对象的上下文。

I played a bit in JSFiddle, have a look : JSFiddle JsScrollPane sandbox ;) 我在JSFiddle中玩了一点,看看: JSFiddle JsScrollPane沙箱 ;)

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

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