back-to-top.js 879 B

12345678910111213141516171819202122232425262728
  1. jQuery(document).ready(function($){
  2. // browser window scroll (in pixels) after which the "back to top" link is shown
  3. var offset = 300,
  4. //browser window scroll (in pixels) after which the "back to top" link opacity is reduced
  5. offset_opacity = 1200,
  6. //duration of the top scrolling animation (in ms)
  7. scroll_top_duration = 700,
  8. //grab the "back to top" link
  9. $back_to_top = $('.cd-top');
  10. //hide or show the "back to top" link
  11. $(window).scroll(function(){
  12. ( $(this).scrollTop() > offset ) ? $back_to_top.addClass('cd-is-visible') : $back_to_top.removeClass('cd-is-visible cd-fade-out');
  13. if( $(this).scrollTop() > offset_opacity ) {
  14. $back_to_top.addClass('cd-fade-out');
  15. }
  16. });
  17. //smooth scroll to top
  18. $back_to_top.on('click', function(event){
  19. event.preventDefault();
  20. $('body,html').animate({
  21. scrollTop: 0 ,
  22. }, scroll_top_duration
  23. );
  24. });
  25. });