使用onepage_scroll插件时,在JQuery中获取滚动位置

时间:2014-04-09 10:12:08

标签: javascript jquery scrolltop

我在网站上使用了很棒的onepage_scroll plugin。低于设定阈值,页面将恢复为正常滚动行为。但是在这一点上 - 当我尝试使用scrollTop()来获取距页面顶部的距离时,它总是返回0。

var vph = $(window).height();
var responsiveThreshold = 640;

$(".onepage_scroll").onepage_scroll({
   sectionContainer: "section",     // sectionContainer accepts any kind of selector in case you don't want to use section
   easing: "ease",                  // Easing options accepts the CSS3 easing animation such "ease", "linear", "ease-in", 
                                    // "ease-out", "ease-in-out", or even cubic bezier value such as "cubic-bezier(0.175, 0.885, 0.420, 1.310)"
   animationTime: 1000,             // AnimationTime let you define how long each section takes to animate
   pagination: false,                // You can either show or hide the pagination. Toggle true for show, false for hide.
   updateURL: true,                // Toggle this true if you want the URL to be updated automatically when the user scroll to each page.
   beforeMove: scrollCatchBefore,  // This option accepts a callback function. The function will be called before the page moves.
   afterMove: scrollCatchAfter,   // This option accepts a callback function. The function will be called after the page moves.
   loop: false,                     // You can have the page loop back to the top/bottom when the user navigates at up/down on the first/last page.
   keyboard: true,                  // You can activate the keyboard controls
   responsiveFallback: responsiveThreshold        // You can fallback to normal page scroll by defining the width of the browser in which
                                    // you want the responsive fallback to be triggered. For example, set this to 600 and whenever 
                                    // the browser's width is less than 600, the fallback will kick in.
});




// Fix menu if page is too small
if(vpw<responsiveThreshold) {
    $("#navigation").hide();

    /* Every time the window is scrolled ... */
    $('body').scroll( function(){

        var scrollPos = $('html').scrollTop();
        console.log(vph);
        console.log(scrollPos);

        if(scrollPos > vph) {
            $("#navigation").fadeIn();
        } else {
            $("#navigation").fadeOut();
        }
    });
}

我也尝试了以下两种方法:

$('body').scrollTop();
$('.onepage_scroll').scrollTop();
$(window).scrollTop();

2 个答案:

答案 0 :(得分:0)

看看onepage_scroll是如何工作的,它不会以典型的方式移动body或div。所以scrollTop不是这里的解决方案。

在以下div中使用translate3d ......

<div class="main onepage-wrapper">

在插件运行时使用以下修改的样式...

-webkit-transform: translate3d(0px, -100%, 0px);

这个答案可能对你有用...... Get translate3d values of a div?

答案 1 :(得分:0)

了解当前的部分索引:

var nCurSect = $("section.active", ".main").data("index");

然后你可以将它乘以屏幕高度并获得偏移。