简体   繁体   English

根据滚动淡入或淡出

[英]Fade in or fade out based on scroll

$(document).scroll(function () {
    var y = $(this).scrollTop();

    if (y > 397) {
        $('#aboutNav.fixed').fadeIn(500);   
    } else {
        $('#aboutNav.fixed').hide();
    }
});

As you can tell, this shows a fixed navigation. 如您所知,这显示了固定的导航。 The CSS is fine, the positioning is great. CSS很好,定位很棒。 However I want the navigation to become visible above 397px which it does fine. 但是我希望导航在397px以上变得可见,这确实很好。

However I want the navigation to fade in when I start scrolling: 但是,我希望导航在开始滚动时逐渐消失:

.fadeIn(500);

When the user starts stops to look at content or whatever, I want the element to fade out 当用户开始停下来查看内容时,我希望元素淡出

.delay(3000).fadeOut(350);

I believe this is something that can be done by doing an if statement within the first if statement. 我相信这可以通过在第一个if语句中执行if语句来完成。 However a script to check if the user is scrolling and the working script above seem to collide. 但是,用于检查用户是否正在滚动并且上面的工作脚本似乎冲突的脚本。

Ideas, anyone? 想法,有人吗?

If I understand you correctly. 如果我正确理解您的意思。 You want the nav to fade in if its above 397px and only when its scrolling... So this function will do that. 您希望导航栏在其397px以上时淡入,并且仅在其滚动时淡出...因此,此功能可以做到这一点。 If I misunderstood your question please clarify in the comments 如果我误解了您的问题,请在评论中进行说明

$(window).scroll(function() {
    clearTimeout($.data(this, 'scrollTimer'));//Lets the timer know were scrolling

    //Hide/Show nav based on location
    var y = $(this).scrollTop();
    if (y > 397) {
        $('#aboutNav.fixed').fadeIn(500);   
    } else {
        $('#aboutNav.fixed').hide();
    }

    //TimeOut function that is only hit if we havent scrolled for 3 Seconds
    //In here is where you fade out the nav
    $.data(this, 'scrollTimer', setTimeout(function() {    
        $('#aboutNav.fixed').fadeOut();
        console.log("Haven't scrolled in 3s!");
    }, 3000));
});

JAN 23 UPDATE based on your comment 1月23日根据您的评论更新

You can add this to you $(document).ready() function 您可以将其添加到$(document).ready()函数中

$("#elementID").hover(function(){ 
    //show here (mouse over)
    $("#elementToShow").show();
},function(){
    //Start timeout here, (mouse out)
    setTimeout(function() {
        $("#elementToHide").hide();
    }, 3000);
}

Expanding on what Kierchon's answer a bit: 进一步扩展基尔川的答案:

Since there's no real way to tell when the user is done scrolling (ie no event for 'finished scrolling') you'll have to use a event-delaying method called debouncing. 由于没有真正的方法可以告诉用户何时完成滚动(即没有“完成滚动”的事件),因此您必须使用一种称为debouncing的事件延迟方法。

Debouncing is basically setting a timeout to run some code (a function) in the future, and if the event calling the debounced function get called again, you clear and reset the timeout, doing this repeatedly until the event finally stops being called. 防抖基本上是设置超时,以便将来运行某些代码(一个函数),并且如果再次调用调用了防抖动函数的事件,则可以清除并重置超时,重复执行此操作,直到最终停止调用该事件为止。 This method is to prevent events that fire repeatedly (such as scroll and resize) to only execute things after the final event fires and your delayed (debounced) code finally executes. 此方法是为了防止重复触发的事件(例如,滚动和调整大小)仅在最终事件触发并且延迟(去抖动)代码最终执行之后才执行。

Here is a nice article on use of debouncing methods in JS . 这是一篇有关在JS中使用反跳方法的不错的文章。

As long as I understand what you need (which I think I do) - Here's a JSBin with some working code 只要我了解您的需求(我认为我需要这样做)- 这是一个包含一些工作代码的JSBin

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

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