2

I want to create a similar button as in Lenta.ru, with a smooth retractable effect, can you help me with that?

albeck
  • 510
  • 1
  • 7
  • 16
BrBa
  • 51
  • 1
  • 1
  • 4
  • possible duplicate of [How do I scroll to the top of the page with jQuery?](http://stackoverflow.com/questions/1144805/how-do-i-scroll-to-the-top-of-the-page-with-jquery) – Sergio Jul 14 '13 at 21:06

2 Answers2

13

Check this DEMO http://jsfiddle.net/yeyene/J3zyq/3/

$(document).ready(function() {
    $(window).scroll(function() {
        if($(this).scrollTop() > 100){
            $('#goTop').stop().animate({
                top: '20px'    
                }, 500);
        }
        else{
            $('#goTop').stop().animate({
               top: '-100px'    
            }, 500);
        }
    });
    $('#goTop').click(function() {
        $('html, body').stop().animate({
           scrollTop: 0
        }, 500, function() {
           $('#goTop').stop().animate({
               top: '-100px'    
           }, 500);
        });
    });
});   
yeyene
  • 7,297
  • 1
  • 21
  • 29
0

Based on this How do I change my background on scroll using CSS?, and a few changes to fit your desire effect with a little CSS3 & jQ

HTML

<div id="up"></div>

CSS

#up {
    background: url(http://icdn.lenta.ru/assets/icons-s238578731b-c5df9ffc01b66d2cee1e3e3d6d74e49b.png) no-repeat;
    background-position: -2329px 0;
    display: inline-block;
    vertical-align: middle;
    position: fixed;
    right: 100px;
    top: -64px;
    width: 54px;
    height: 54px;
    cursor: pointer;
    opacity: 0;
    -webkit-transition: all 0.5s ease-in-out;
        -moz-transition: all 0.5s ease-in-out;
        -ms-transition: all 0.5s ease-in-out;
        -o-transition: all 0.5s ease-in-out;
        transition: all 0.5s ease-in-out;
}
#up.scrolled{
    opacity:1;
    top: 10px;
}

JS

jQuery(window).scroll(function(){
    var fromTopPx = 200; // distance to trigger
    var scrolledFromtop = jQuery(window).scrollTop();
    if(scrolledFromtop > fromTopPx){
        jQuery('#up').addClass('scrolled');
    }else{
        jQuery('#up').removeClass('scrolled');
    }
});
jQuery('#up').on('click',function(){
    jQuery("html, body").animate({ scrollTop: 0 }, 600);
    return false;
});

Of course you can change the effect and use jQuery animation instead of CSS3... but it will depends on your needs.

Live example: http://jsfiddle.net/E2aWr/

Community
  • 1
  • 1
gmo
  • 8,860
  • 3
  • 40
  • 51