简体   繁体   English

JavaScript-“向下滚动”按钮-从着陆页滚动到标题顶部

[英]JavaScript - “Scroll Down” Button - Scroll From Landing Page to Top of the Header

I'm using the same "scroll down" method that's being used here: 我使用的是此处使用的“向下滚动”方法:

https://codepen.io/nxworld/pen/OyRrGy https://codepen.io/nxworld/pen/OyRrGy

Of course, the JavaScript in that example doesn't take you to a specific place on the page - it just takes you to the next section. 当然,该示例中的JavaScript不会将您带到页面上的特定位置-只会带您到下一部分。 What I'm trying to do is, instead of the "scroll down" button taking us from here: 我想要做的是,而不是通过“向下滚动”按钮将我们从此处移开:

在此处输入图片说明

to here (this is what's happening currently): 到这里(这是当前正在发生的事情): 在此处输入图片说明

we want the scroll down button to take us to where the top of the header touches the top of the browser's window (like this): 我们希望向下滚动按钮将我们带到标题顶部与浏览器窗口顶部的位置(如下所示):

在此处输入图片说明

Here is what I currently have in my JS file: 这是我当前在JS文件中拥有的内容:

//javascript functions
(function ($, root, undefined) {
$(function () {     
    'use strict';       
    // DOM ready, take it away      
}); 
})(jQuery, this);

//landing page text delay
window.onload = function() {
  $("p").each(function(index) {
  $(this).css({
    'animation-delay': (index + 1) * .7 + 's'
  });
  });
}

//scroll to top functions, found here: 
http://plnkr.co/edit/DCnukHPNWa6Z1zOX53xp?p=preview
//scroll to top (linear)
function scrollToTop(scrollDuration) {
    var scrollStep = -window.scrollY / (scrollDuration / 15),
      scrollInterval = setInterval(function(){
      if ( window.scrollY != 0 ) {
        window.scrollBy( 0, scrollStep );
      }
      else clearInterval(scrollInterval); 
  },15);
}

//scroll to top (ease in and out)
function scrollToTop(scrollDuration) {
const   scrollHeight = window.scrollY,
        scrollStep = Math.PI / ( scrollDuration / 15 ),
        cosParameter = scrollHeight / 2;
var     scrollCount = 0,
        scrollMargin,
        scrollInterval = setInterval( function() {
        if ( window.scrollY != 0 ) {
            scrollCount = scrollCount + 1;  
            scrollMargin = cosParameter - cosParameter * Math.cos( scrollCount * scrollStep );
            window.scrollTo( 0, ( scrollHeight - scrollMargin ) );
        } 
        else clearInterval(scrollInterval); 
        }, 15 );
    }

//scroll down
(function($) {
$('a[href*=#]').on('click', function(e) {
    console.log( $(".container").offset().top)
    e.preventDefault();
    $('html,body').animate({       
        scrollTop: $("#menu-main").offset().top - 6}, 1700);
    //$('html, body').animate({ scrollTop: 
$($(this).attr('href')).offset().top}, 900, 'linear');
});
})(jQuery);

$(function() {
$('a[href*=#]').on('click', function(e) {
    e.preventDefault();
    $('html, body').animate({ scrollTop: 
$($(this).attr('href')).offset().top}, 500, 'linear');
});
});

window.onload =(function($) {
$(function(){
    // Check the initial Poistion of the Sticky Header
    var stickyHeaderTop = $('#menu-main').offset().top;

    $(window).scroll(function(){
            if( $(window).scrollTop() >= stickyHeaderTop ) {
                    $('#menu-main').css({
                        position: 'fixed',
                        width: '100%',
                        top: '0px',
                        'z-index': 99999,
                        height: '45px'
                    });
                    $('#menu-main-navigation').css('margin-top', '-7px');
                    $('#logo').css({
                        'font-size':'40px',
                        'margin-top': '-20px'
                    });
                    $('#stickyalias').css('display', 'block');
            } else {
                    $('#menu-main-navigation').css('margin-top', '0');
                    $('#logo').css({
                        'font-size': '50px',
                        'margin-top': '-15px'
                    });
                    $('#menu-main').css({
                        position: '',
                        top: '',
                        'z-index': 0,
                        height: '57px'
                    });
                    $('#stickyalias').css('display', 'none');
            }
    });
  });
})(jQuery)

You can see that I already have multiple functions for scrolling to the top of the page ( scrollToTop ) and I figure I can use the same kind of logic to scroll down. 您可以看到我已经具有用于滚动到页面顶部的多个功能( scrollToTop ),而且我认为可以使用相同的逻辑向下滚动。 Since my current scrollDown function isn't working properly, I tried to comment it out and instead write something like this: 由于我当前的scrollDown函数无法正常工作,因此我尝试将其注释掉,然后编写如下代码:

//scroll down (ease in and out)
function scrollToHeader(scrollDuration) {
const   scrollHeight = window.scrollY, //distance from scroll down button to the top of the header
        scrollStep = Math.PI / ( scrollDuration / 15 ),
        cosParameter = scrollHeight / 2;
var     scrollCount = 0,
        scrollMargin,
        scrollInterval = setInterval( function() {
        if ( window.scrollY != 0 ) {
            scrollCount = scrollCount + 1;
            scrollMargin = cosParameter - cosParameter * Math.cos (scrollCount * scrollStep);
            window.scrollTo( 0, ( scrollHeight - scrollMargin ) );
        }
        else clearInterval(scrollInterval);
        }, 15 );    
}

Then I applied that function to the scroll button in the HTML: 然后,我将该功能应用于HTML中的滚动按钮:

 <section id="section5" class="demo">
    <a href="#section5"><span onclick="scrollToTop(1000);"></span></a>
</section>

But when I tried this it caused the scroll button to shift to the left side of the page, and it actually took me up instead of down - so I re-commented out the function in my JS and got rid of the changes I made in my HTML (header-front-page.php). 但是,当我尝试这样做时,它导致滚动按钮移动到页面的左侧,并且实际上使我上移而不是下移-因此我重新注释了JS中的函数,并且摆脱了我在JS中所做的更改我的HTML(header-front-page.php)。 You can see how it currently works at thebullshitcollection.com . 您可以在thebullshitcollection.com上查看当前的工作方式 Thanks in advance for your help / suggestions. 在此先感谢您的帮助/建议。

EDIT : among other functions that I previously listed, this function has been added to my JS file: 编辑 :我以前列出的其他功能中,此功能已添加到我的JS文件中:

jQuery(document).ready(function ($) {
    $('#section5').click(function (e) {
        e.preventDefault();
        $('html, body').animate({
        scrollTop: $("#menu-main").position().top
        }, 1000);
    });
}

The target of your link is #section5 it should be #menu-main 链接的目标是#section5它应该是#menu-main
Hope this helps. 希望这可以帮助。

EDIT 编辑
If you want to scroll try this 如果您想滚动试试看

$('#section5').click(function (e) {
    e.preventDefault();
    $('html, body').animate({
        scrollTop: $("#menu-main").position().top
    }, 1000);
});

SYA :) SYA :)

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

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