简体   繁体   English

Javascript语法帮助仅在向上滚动时出现的返回顶部链接

[英]Javascript syntax help for a back to top link that appears only on scroll up

I have a page set up with a back to top link (in an entire bar) slides down at the top of the page, only on scroll up. 我有一个页面设置,其中一个回到顶部的链接(在整个栏中)在页面顶部向下滑动,仅向上滚动。

I'm a beginner with Javascript, so I have cobbled something together, that does work (it shows on scroll up, unless we're 500px from the top, hides on scroll down), and it uses some code I got from here to not check every pixel scrolled. 我是Javascript的初学者,所以我拼凑了一些东西,它确实有效(它显示在向上滚动,除非我们从顶部500px,隐藏向下滚动),它使用我从这里得到的一些代码不检查滚动的每个像素。

What I want to add is that even if you are still scrolling up once you get to the submenu, then the toplink should scroll off the page again - I don't need a back to the top once you are at the top. 我要添加的是,即使你进入子菜单时仍然向上滚动,然后顶部链接应该再次滚动页面 - 一旦你在顶部,我不需要回到顶部。

I've gotten this to work by adding a SECOND Javascript script, but I know that there must be a much better way to get something like this to work in the first call. 我已经通过添加一个SECOND Javascript脚本来实现这一点,但我知道必须有一个更好的方法来让这样的东西在第一次调用中工作。 Also this second call uses the window.scroll function which I'm pretty sure is the wrong way to do it. 此第二个调用使用window.scroll函数,我很确定这是错误的方法。

FIRST SCRIPT 第一个脚本

// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var subMenu = $('#subMenu').offset().top;

$(window).scroll(function(event){
    didScroll = true;
});

setInterval(function() {
    if (didScroll) {
        hasScrolled();
        didScroll = false;
    }
}, 250);

function hasScrolled() {
    var st = $(this).scrollTop();

       // Make sure they scroll more than delta
    if(Math.abs(lastScrollTop - st) <= delta)
        return;

    // If they scroll down, add class .nav-up.

    if (st > lastScrollTop && st ){
        // Scroll Down
         $('#backtotop').removeClass('nav-down').addClass('nav-up');
         $(".toplink").fadeOut("fast");


    } else {
        // Scroll Up
       if(st + $(window).height() < $(document).height() && st > 500) {
        $('#backtotop').removeClass('nav-up').addClass('nav-down');
        $(".toplink").fadeIn("slow");

        } 



    }


    lastScrollTop = st;

}

SECOND SCRIPT 第二篇抄写

    <script>

   $(window).scroll (function ()
   {var st = $(this).scrollTop();
    var subMenu = $('#subMenu').offset().top;


     if (st < subMenu) {
        $('#backtotop').removeClass('nav-down').addClass('nav-up');
        $(".toplink").fadeOut("fast");


     }

     });

    </script>

Fiddle with the HTML and CSS: https://jsfiddle.net/Lu0jz3nc/11/ 摆弄HTML和CSS: https//jsfiddle.net/Lu0jz3nc/11/

I've modified your jsfiddle a bit, albeit it's not the most elegant or efficient solution, it works. 我已经修改了你的jsfiddle,虽然它不是最优雅或最有效的解决方案,但它的工作原理。 https://jsfiddle.net/aedm9cut/ https://jsfiddle.net/aedm9cut/

Here is the JS: 这是JS:

var lastY = 0;
var subMenu = $('#subMenu').offset().top;

$(window).scroll(function(event){
    currentY = $(window).scrollTop();
    if(currentY > 500 && lastY > currentY){
           $('#backtotop').removeClass('nav-up').addClass('nav-down');
            $(".toplink").fadeIn("slow");
    }
    else{
        $('#backtotop').removeClass('nav-down').addClass('nav-up');
         $(".toplink").fadeOut("fast");
    }
    lastY = $(window).scrollTop();
});

This code does 2 things, check if you are past 500px from top and also checks the direction of your scroll. 此代码执行两项操作,检查您是否从顶部超过500px并检查滚动的方向。 if you are past 500px and are scrolling up the bar will show, otherwise it won't. 如果你超过500px并向上滚动,则显示该栏,否则不会。

This code can be improved so that the code in the if and else statements aren't run every time the user scrolls, but perhaps you can make a breakpoint that the code can detect the user has gone past and then run the code once. 可以改进此代码,以便每次用户滚动时都不会运行if和else语句中的代码,但也许您可以创建一个断点,代码可以检测到用户已经过去然后运行一次代码。 Right now, the code in the if/ or the else is run several times during a scroll event. 现在,在滚动事件期间,if /或else中的代码会多次运行。

Simple, call the hasScrolled in the onclick attribute. 很简单,在onclick属性中调用hasScrolled Now I've added a special argument forceup which when true will run the special code to make it hide. 现在我添加了一个特殊参数forceup ,当true运行时会运行特殊代码使其隐藏。 So when you click the To Top button, it will force it to go up. 因此,当您单击“ 顶部”按钮时,它将强制它上升。 The reason it is window.hasScrolled is to make it global. 它是window.hasScrolled的原因是使它成为全局的。

window.hasScrolled = function (forceup) {

    if (forceup) {
        $('#backtotop').removeClass('nav-down').addClass('nav-up');
        $(".toplink").fadeOut("fast");
    }

//Rest of code
}

Fiddle 小提琴

You should be able to do this all in the window scroll handler, with the help of one global variable. 在一个全局变量的帮助下,您应该能够在窗口滚动处理程序中完成所有操作。

var MIN_DELTA = 5;
var MIN_SCROLLTOP = 500;
var MENU_HEIGHT = $('#backtotop').height();

$('#backtotop').addClass('nav-up');

var lastScrollTop = 0;
$(window).scroll(function (event) {
    var scrollTop = $(window).scrollTop();
    if (scrollTop > MENU_HEIGHT) {
        if (scrollTop < lastScrollTop) { // Scrolling up
            if (scrollTop > MIN_SCROLLTOP) {
                if (scrollTop < (lastScrollTop - MIN_DELTA)) {
                    $('#backtotop').removeClass('nav-up').addClass('nav-down');
                    $(".toplink").fadeIn("slow");
                    lastScrollTop = scrollTop;
                }
            } else {
                lastScrollTop = scrollTop;
            }
        } else { // Scrolling down
            $('#backtotop').removeClass('nav-down').addClass('nav-up');
            $(".toplink").fadeOut("fast");
            lastScrollTop = scrollTop;
        }
    } else {
        $('#backtotop').removeClass('nav-down').addClass('nav-up');
        $(".toplink").fadeOut("fast");
        lastScrollTop = scrollTop;
    }
});

Notice that the lastScrollTop variable is not updated when the window has been scrolled down, past MIN_SCROLLTOP , but it has not changed more than MIN_DELTA . 请注意,当窗口向下滚动,超过MIN_SCROLLTOPlastScrollTop变量不会更新,但它的更改时间不超过MIN_DELTA

jsfiddle 的jsfiddle

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

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