javascript - jQuery 视差导航不会滚动到目标

标签 javascript jquery parallax scrolltop

我已经制作了一个视差滚动演示,但在内容之间导航时遇到问题。

  • 页眉页脚已修复,其余部分则未修复。

  • 关于变量section3Top section4Top我猜高度计算不正确。

我看不到问题。

请看看我的演示并尝试单击菜单。它应该通过单击菜单、滚动和调整大小来工作。

演示:http://fiddle.jshell.net/Zza7t/

JS:

function redrawDotNav(){
    var section1Top =  0;
    var section2Top =  $('#BuyKeep').offset().top - (($('#Rentals').offset().top - $('#BuyKeep').offset().top) / 2);
    var section3Top =  $('#Rentals').offset().top - (($('#WaystoWatch').offset().top - $('#Rentals').offset().top) / 2);
    var section4Top =  $('#WaystoWatch').offset().top - (($(document).height() - $('#WaystoWatch').offset().top) / 2);

    $('nav#primary a').removeClass('active');

    if($(document).scrollTop() >= section1Top && $(document).scrollTop() < section2Top){
        $('nav#primary a.about').addClass('active');
    } else if ($(document).scrollTop() >= section2Top && $(document).scrollTop() < section3Top){
        $('nav#primary a.BuyKeep').addClass('active');
    } else if ($(document).scrollTop() >= section3Top && $(document).scrollTop() < section4Top){
        $('nav#primary a.Rentals').addClass('active');
    } else if ($(document).scrollTop() >= section4Top){
        $('nav#primary a.WaystoWatch').addClass('active');
    }
}

function scrollFooter(scrollY, heightFooter) {

    if(scrollY >= heightFooter) {
      $('#WaystoWatch').css({
          'bottom' : '0px'
      });
    }
    else {
      $('#WaystoWatch').css({
          'bottom' : '-' + heightFooter + 'px'
      });
    }
}

    function heightsCalculator(){
        var windowHeight    = $(window).height(),
        footerHeight    = $('#WaystoWatch').height(),
        heightDocument  = (windowHeight) + ($('#BuyKeep').height()) + ($('#Rentals').height()) + ($('#WaystoWatch').height()) - 0;

        $('#scroll-animate, #scroll-animate-main').css({
            'height' :  heightDocument + 'px'
        }); 
        $('#about').css({
            'height' : windowHeight + 'px'
        });
        $('.wrapper-parallax').css({
            'margin-top' : windowHeight + 'px'
        });

        scrollFooter(window.scrollY, footerHeight);

        window.onscroll = function(){
        var scroll = window.scrollY;

        $('#scroll-animate-main').css({
            'top' : '-' + scroll + 'px'
        });

        $('#about').css({
            'background-position-y' : 50 - (scroll * 100 / heightDocument) + '%'
        });

        scrollFooter(scroll, footerHeight);

        }
  }

最佳答案

唯一对我不起作用的链接是“页脚”。因为你已经固定了它的位置,所以当你上下滚动页面时,它的偏移量将会改变。您想要对标题执行相同的操作,并滚动页面的特定位置,在本例中为底部。还有一些东西会重新调整您的“页脚”高度,我无法找到您在哪里这样做或为什么这样做,但我认为这是没有必要的。

$('a.WaystoWatch').click(function(){
    $('html, body').animate({
        scrollTop:$('body').height()
    }, 1000, function() {
        parallaxScroll(); 
    });
    return false;
});

JSFIDDLE HERE

关于javascript - jQuery 视差导航不会滚动到目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24657350/

相关文章:

jquery - 按日期对对象数组进行排序

javascript - 将视差与图像幻灯片一起使用

css - 视差将背景图像滚动到永久 View 中

javascript - 制作我自己的 Jquery 视差效果

javascript - jQuery 追加预格式化的连接字符串

javascript - 提交后,信息应发送至电子邮件

Javascript:从嵌套函数内部访问父属性

javascript - 单击文本区域时,Twitter-Bootstrap 下拉登录关闭

javascript - 如何使 <tr> 作为链接而不是单元格可点击?

javascript - jQuery 正则表达式字母数字防止符号和空格