/js/jquery.scrollUp.js

https://gitlab.com/fabiorf/fabio-site · JavaScript · 133 lines · 84 code · 19 blank · 30 comment · 11 complexity · 35ed4afeb307cf4fb80ea3a2d2a04710 MD5 · raw file

  1. /*
  2. scrollup v2.2.0
  3. Author: Mark Goodyear - http://markgoodyear.com
  4. Git: https://github.com/markgoodyear/scrollup
  5. Copyright 2014 Mark Goodyear.
  6. Licensed under the MIT license
  7. http://www.opensource.org/licenses/mit-license.php
  8. Twitter: @markgdyr
  9. */
  10. (function($, window, document) {
  11. // Main function
  12. $.fn.scrollUp = function (options) {
  13. // Ensure that only one scrollUp exists
  14. if (!$.data(document.body, 'scrollUp')) {
  15. $.data(document.body, 'scrollUp', true);
  16. $.fn.scrollUp.init(options);
  17. }
  18. };
  19. // Init
  20. $.fn.scrollUp.init = function(options) {
  21. // Apply any options to the settings, override the defaults
  22. var o = $.fn.scrollUp.settings = $.extend({}, $.fn.scrollUp.defaults, options),
  23. // Set scrollTitle
  24. scrollTitle = (o.scrollTitle) ? o.scrollTitle : o.scrollText,
  25. // Create element
  26. $self;
  27. if (o.scrollTrigger) {
  28. $self = $(o.scrollTrigger);
  29. } else {
  30. $self = $('<a/>', {
  31. id: o.scrollName,
  32. href: '#top',
  33. title: scrollTitle
  34. });
  35. }
  36. $self.appendTo('body');
  37. // If not using an image display text
  38. if (!(o.scrollImg || o.scrollTrigger)) {
  39. $self.html(o.scrollText);
  40. }
  41. // Minimum CSS to make the magic happen
  42. $self.css({
  43. display: 'none',
  44. position: 'fixed',
  45. zIndex: o.zIndex
  46. });
  47. // Active point overlay
  48. if (o.activeOverlay) {
  49. $('<div/>', { id: o.scrollName + '-active' }).css({ position: 'absolute', 'top': o.scrollDistance + 'px', width: '100%', borderTop: '1px dotted' + o.activeOverlay, zIndex: o.zIndex }).appendTo('body');
  50. }
  51. // Scroll function
  52. scrollEvent = $(window).scroll(function() {
  53. // If from top or bottom
  54. if (o.scrollFrom === 'top') {
  55. scrollDis = o.scrollDistance;
  56. } else {
  57. scrollDis = $(document).height() - $(window).height() - o.scrollDistance;
  58. }
  59. // Switch animation type
  60. switch (o.animation) {
  61. case 'fade':
  62. $( ($(window).scrollTop() > scrollDis) ? $self.fadeIn(o.animationInSpeed) : $self.fadeOut(o.animationOutSpeed) );
  63. break;
  64. case 'slide':
  65. $( ($(window).scrollTop() > scrollDis) ? $self.slideDown(o.animationInSpeed) : $self.slideUp(o.animationOutSpeed) );
  66. break;
  67. default:
  68. $( ($(window).scrollTop() > scrollDis) ? $self.show(0) : $self.hide(0) );
  69. }
  70. });
  71. // To the top
  72. $self.click(function(e) {
  73. e.preventDefault();
  74. $('html, body').animate({
  75. scrollTop:0
  76. }, o.scrollSpeed, o.easingType);
  77. });
  78. };
  79. // Defaults
  80. $.fn.scrollUp.defaults = {
  81. scrollName: 'scrollUp', // Element ID
  82. scrollDistance: 300, // Distance from top/bottom before showing element (px)
  83. scrollFrom: 'top', // 'top' or 'bottom'
  84. scrollSpeed: 300, // Speed back to top (ms)
  85. easingType: 'linear', // Scroll to top easing (see http://easings.net/)
  86. animation: 'fade', // Fade, slide, none
  87. animationInSpeed: 200, // Animation in speed (ms)
  88. animationOutSpeed: 200, // Animation out speed (ms)
  89. scrollTrigger: false, // Set a custom triggering element. Can be an HTML string or jQuery object
  90. scrollText: 'Scroll to top', // Text for element, can contain HTML
  91. scrollTitle: false, // Set a custom <a> title if required. Defaults to scrollText
  92. scrollImg: false, // Set true to use image
  93. activeOverlay: false, // Set CSS color to display scrollUp active point, e.g '#00FFFF'
  94. zIndex: 2147483647 // Z-Index for the overlay
  95. };
  96. // Destroy scrollUp plugin and clean all modifications to the DOM
  97. $.fn.scrollUp.destroy = function (scrollEvent){
  98. $.removeData( document.body, 'scrollUp' );
  99. $( '#' + $.fn.scrollUp.settings.scrollName ).remove();
  100. $( '#' + $.fn.scrollUp.settings.scrollName + '-active' ).remove();
  101. // If 1.7 or above use the new .off()
  102. if ($.fn.jquery.split('.')[1] >= 7) {
  103. $(window).off( 'scroll', scrollEvent );
  104. // Else use the old .unbind()
  105. } else {
  106. $(window).unbind( 'scroll', scrollEvent );
  107. }
  108. };
  109. $.scrollUp = $.fn.scrollUp;
  110. })(jQuery, window, document);