문제

<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