Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

fadeOut element on scroll not working?

Hello,

Can someone please help me, I need this done today and I'm freaking out...

What should I change to hide this PROJECT MANAGEMENT title when sidebar appears?

I managed to hide it when sidebar appears and then scroll down, but I need it hidden as soon as sidebar is shown?

Thank you so much!

HERE ARE MY FILES

1 Answer

It seems like the javascript that you're using to animate the sidebar is sufficient to fadeOut and fadeIn the PROJECT MANAGEMENT title. The issue is you're event listening first and then jQuery is waiting for the scroll position to change to fadeOut the title. Just add,

function toggle( reveal ) {
    isAnimating = true;

    if( reveal ) {
            classie.add( container, 'modify' );
            $('.naslovservisa').fadeOut(200); // This
    }
    else {
            $('.naslovservisa').fadeIn(200); // This
        noscroll = true;
        disable_scroll();
        classie.remove( container, 'modify' );
         }

Edit: then you can remove the jQuery content in titlehide.js, since that's irrelevant with the above code.

Wher exactly should I add this code?

Just add it to the already existing code in the index.html file. Just add the code with the commented portions to the code that's already there. And then make sure you take out the code in the titlehide.js file so that's it's not conflicting with the above code.

I tried to do so, added new <script> tag with code you provided and deleted titlehide.js but now PROJECT MANAGEMENT just stays on the screen all the time. Did that code worked for you in my file?

Yes the above code worked for me. You shouldn't have needed to add any new tags, just edit the code that's already there. Right below the closing div to your container div your code should look like this,

</div><!-- /container -->
        <script src="js/classie.js"></script>
        <script>
            (function() {

                // detect if IE : from http://stackoverflow.com/a/16657946      
                var ie = (function(){
                    var undef,rv = -1; // Return value assumes failure.
                    var ua = window.navigator.userAgent;
                    var msie = ua.indexOf('MSIE ');
                    var trident = ua.indexOf('Trident/');

                    if (msie > 0) {
                        // IE 10 or older => return version number
                        rv = parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
                    } else if (trident > 0) {
                        // IE 11 (or newer) => return version number
                        var rvNum = ua.indexOf('rv:');
                        rv = parseInt(ua.substring(rvNum + 3, ua.indexOf('.', rvNum)), 10);
                    }

                    return ((rv > -1) ? rv : undef);
                }());


                // disable/enable scroll (mousewheel and keys) from http://stackoverflow.com/a/4770179                  
                // left: 37, up: 38, right: 39, down: 40,
                // spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
                var keys = [32, 37, 38, 39, 40], wheelIter = 0;

                function preventDefault(e) {
                    e = e || window.event;
                    if (e.preventDefault)
                    e.preventDefault();
                    e.returnValue = false;  
                }

                function keydown(e) {
                    for (var i = keys.length; i--;) {
                        if (e.keyCode === keys[i]) {
                            preventDefault(e);
                            return;
                        }
                    }
                }

                function touchmove(e) {
                    preventDefault(e);
                }

                function wheel(e) {
                    // for IE 
                    //if( ie ) {
                        //preventDefault(e);
                    //}
                }

                function disable_scroll() {
                    window.onmousewheel = document.onmousewheel = wheel;
                    document.onkeydown = keydown;
                    document.body.ontouchmove = touchmove;
                }

                function enable_scroll() {
                    window.onmousewheel = document.onmousewheel = document.onkeydown = document.body.ontouchmove = null;  
                }

                var docElem = window.document.documentElement,
                    scrollVal,
                    isRevealed, 
                    noscroll, 
                    isAnimating,
                    container = document.getElementById( 'container' ),
                    trigger = container.querySelector( 'button.trigger' );

                function scrollY() {
                    return window.pageYOffset || docElem.scrollTop;
                }

                function scrollPage() {
                    scrollVal = scrollY();

                    if( noscroll && !ie ) {
                        if( scrollVal < 0 ) return false;
                        // keep it that way
                        window.scrollTo( 0, 0 );
                    }

                    if( classie.has( container, 'notrans' ) ) {
                        classie.remove( container, 'notrans' );
                        return false;
                    }

                    if( isAnimating ) {
                        return false;
                    }

                    if( scrollVal <= 0 && isRevealed ) {
                        toggle(0);
                    }
                    else if( scrollVal > 0 && !isRevealed ){
                        toggle(1);
                    }
                }
                               // This is where you need to edit. 
                function toggle( reveal ) {
                    isAnimating = true;

                    if( reveal ) {
 // This is the jQuery fadeOut with the duration you've indicated
 // This will allow the title to fadeOut at the same time the sidebar is sliding in.
                        $('.naslovservisa').fadeOut(200); 
                        classie.add( container, 'modify' );
                    }
                    else {
// This will allow the title to fade back in when you reach the top.
                        $('.naslovservisa').fadeIn(200);
                        noscroll = true;
                        disable_scroll();
                        classie.remove( container, 'modify' );
                    }

                    // simulating the end of the transition:
                    setTimeout( function() {
                        isRevealed = !isRevealed;
                        isAnimating = false;
                        if( reveal ) {
                            noscroll = false;
                            enable_scroll();
                        }
                    }, 600 );
                }

                // refreshing the page...
                var pageScroll = scrollY();
                noscroll = pageScroll === 0;

                disable_scroll();

                if( pageScroll ) {
                    isRevealed = true;
                    classie.add( container, 'notrans' );
                    classie.add( container, 'modify' );
                }

                window.addEventListener( 'scroll', scrollPage );
                trigger.addEventListener( 'click', function() { toggle( 'reveal' ); } );
            })();
        </script>
    </body>
</html>

Does that help?

YES!!! But I have no idea what I was doing wrong, thank you so much! You just saved me a lot of trouble...

No sweat, glad to help.