Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript fade

Options
  • 05-03-2014 3:06pm
    #1
    Registered Users Posts: 1,298 ✭✭✭


    $(document).ready(function() {
    $(window).scroll(function() {
    if ($(this).scrollTop() < 350) {
    $('.fixed_wrap').css("background-color", "rgba(0, 131, 170, 0.3)");
    } else {
    $('.fixed_wrap').css("background-color", "rgba(0, 131, 170, 1)");
    }
    });
    });


    Have this piece of code in order to change the opacity of a background. Only thing is that it catches very fast was wondering if there is anyway to make it fade slower on the change?


Comments

  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    You could use fadeTo.


  • Registered Users Posts: 1,298 ✭✭✭off.the.walls


    You could use fadeTo.


    Where would i place it in the code? thats the part im a bit confused about. would I use it instead of the else?


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    You could write it a few ways. Maybe like this:
    $(document).ready(function() {
        // If fixed_wrap is never removed/added to DOM, cache the results. 
        var fixedWrap = $('.fixed_wrap');
        
        $(window).scroll(function() {
            var opacity = $(this).scrollTop() < 350 ? 0.3 : 1;
            fixedWrap.fadeTo('fast', opacity);
        });
    });
    


  • Registered Users Posts: 1,298 ✭✭✭off.the.walls


    You could write it a few ways. Maybe like this:
    $(document).ready(function() {
        // If fixed_wrap is never removed/added to DOM, cache the results. 
        var fixedWrap = $('.fixed_wrap');
        
        $(window).scroll(function() {
            var opacity = $(this).scrollTop() < 350 ? 0.3 : 1;
            fixedWrap.fadeTo('fast', opacity);
        });
    });
    

    Problem with that is that it fades everything inside the div. I'm only looking too fade the background.


  • Registered Users Posts: 10,622 ✭✭✭✭28064212


    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Advertisement
  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    If you have jQuery UI/jQuery Color, you can just change you call to css to animate in your existing code.


  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    I would use CSS transitions for this only if possible.
    //CSS
    .fixed_wrap.fade { background-color: rgba(0, 131, 170, .3) ; }
    .fixed_wrap { background-color: rgba(0, 131, 170, 1); transition:background-color linear .2s; }
    
    //Script
    $(window).scroll(function() {
        var fade = $(this).scrollTop() < 350;        
        $('.fixed_wrap').toggleClass('fade',fade);  
    });
    

    You could remove the dependency on jQuery as well with this method using the classList property on DOM elements.


  • Registered Users Posts: 1,298 ✭✭✭off.the.walls


    $(window).scroll(function() {

    if($(window).scrollTop() > 500){

    $('.fixed_wrap').css('background','rgba(0, 131, 170, 1)')

    }

    else if ($(window).scrollTop() < 500){
    $('.fixed_wrap').css().stop().animate({"rgba":"(0,131,170,0.3)"},1000)
    }


    });



    thats the code I went with in the end. the css doesn't seem to be working for me.


  • Registered Users Posts: 1,298 ✭✭✭off.the.walls


    So really wasn't happy with the way this was working.

    What I did was set the css3 using -webkit transitions and stuff to switch between an background rgba

    Then I used a toggle function too add a class to the div in order to change its height slightly and the background opacity to full.

    Thought i'd post up the solution in case anyone else wanted to do this.


Advertisement