<script>      
    $(window).scroll(function(){
        var fromTopPx = 717; // distance to trigger
        var scrolledFromtop = $(window).scrollTop();
        if(scrolledFromtop > fromTopPx){
            $("#farbe").fadeTo("slow", 0 );
        }else{
            $("#farbe").fadeTo("slow", 1 );

        }
    });

</script>

Hi, I have a fixed div that should not be displayed by default. If you go down 717px from the top, it should fade in. If you go back to the top, it should fade out.

What am I doing wrong?

有帮助吗?

解决方案

Use this function, it's a lot cleaner.

$(window).bind("scroll", function() {
    if ($(this).scrollTop() > 717) {
        $("#farbe").fadeIn();
    } else {
        $("#farbe").stop().fadeOut();
    }
});

Dont forget to add the CSS: #farbe{display: none;} so it's hidden by default.

DEMO

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top