jquery.scrollup.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. /*
  17. * jHue scroll to top plugin
  18. * Can be used globally with
  19. * $.jHueScrollUp()
  20. * or with a target for the scroll up
  21. * $(element).jHueScrollUp()
  22. *
  23. * options:
  24. * - threshold: (default 100) value in pixels, scroll amount before the link appears
  25. * - secondClickScrollToTop: (default false) if specified, the anchor stays for longer and you can re-click on it to scroll to this element
  26. */
  27. (function ($, window, document, undefined) {
  28. var pluginName = "jHueScrollUp",
  29. defaults = {
  30. threshold: 100, // it displays it after 100 px of scroll
  31. secondClickScrollToTop: false
  32. };
  33. function Plugin(element, options) {
  34. this.element = element;
  35. this.options = $.extend({}, defaults, options);
  36. this._defaults = defaults;
  37. this._name = pluginName;
  38. this.init();
  39. }
  40. Plugin.prototype.setOptions = function (options) {
  41. this.options = $.extend({}, defaults, options);
  42. };
  43. Plugin.prototype.init = function () {
  44. var _this = this;
  45. var link = $("<a/>").attr("id", "jHueScrollUpAnchor").attr("href", "javascript:void(0)").html("<i class='fa fa-chevron-up'></i>").appendTo("body");
  46. if ($("#jHueScrollUpAnchor").length > 0) { // just one scroll up per page
  47. link = $("#jHueScrollUpAnchor");
  48. link.off("click");
  49. }
  50. if ($(_this.element).is("body")) {
  51. $(window).scroll(function () {
  52. $(($(window).scrollTop() > _this.options.threshold) ? link.fadeIn(200) : link.fadeOut(200));
  53. });
  54. link.click(function (event) {
  55. $("body, html").animate({scrollTop: $(_this.element).position().top}, 300);
  56. return false;
  57. });
  58. }
  59. else {
  60. $(_this.element).scroll(function () {
  61. var _fadeOutMs = 200;
  62. if (_this.options.secondClickScrollToTop) {
  63. _fadeOutMs = 1000;
  64. }
  65. $(($(_this.element).scrollTop() > _this.options.threshold) ? link.fadeIn(200) : link.fadeOut(_fadeOutMs));
  66. });
  67. link.on("click", function (event) {
  68. event.preventDefault();
  69. event.stopImmediatePropagation();
  70. if (_this.options.secondClickScrollToTop) {
  71. if ($(_this.element).data("lastClick") == null || (new Date()).getTime() - $(_this.element).data("lastClick") > 1500) {
  72. $(_this.element).animate({scrollTop: 0}, 300);
  73. }
  74. else {
  75. $("body, html").animate({scrollTop: 0}, 300);
  76. link.fadeOut(200);
  77. }
  78. $(_this.element).data("lastClick", (new Date()).getTime());
  79. }
  80. else {
  81. $(_this.element).animate({scrollTop: 0}, 300);
  82. }
  83. return false;
  84. });
  85. }
  86. };
  87. $.fn[pluginName] = function (options) {
  88. return this.each(function () {
  89. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  90. });
  91. }
  92. $[pluginName] = function (options) {
  93. new Plugin($("body"), options);
  94. };
  95. })(jQuery, window, document);