jquery.scrollup.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. */
  26. (function ($, window, document, undefined) {
  27. var pluginName = "jHueScrollUp",
  28. defaults = {
  29. threshold: 100 // it displays it after 100 px of scroll
  30. };
  31. function Plugin(element, options) {
  32. this.element = element;
  33. this.options = $.extend({}, defaults, options);
  34. this._defaults = defaults;
  35. this._name = pluginName;
  36. this.init();
  37. }
  38. Plugin.prototype.setOptions = function (options) {
  39. this.options = $.extend({}, defaults, options);
  40. };
  41. Plugin.prototype.init = function () {
  42. var _this = this;
  43. var link = $("<a/>").attr("id", "jHueScrollUpAnchor").attr("href", "javascript:void(0)").html("<i class='fa fa-chevron-up'></i>").appendTo("body");
  44. if ($("#jHueScrollUpAnchor").length > 0) { // just one scroll up per page
  45. link = $("#jHueScrollUpAnchor");
  46. $(document).off("click", "#jHueScrollUpAnchor");
  47. }
  48. $(_this.element).attr("jHueScrollified", "true");
  49. if ($(_this.element).is("body")) {
  50. setScrollBehavior($(window), $("body, html"));
  51. }
  52. else {
  53. setScrollBehavior($(_this.element), $(_this.element));
  54. }
  55. function setScrollBehavior(scrolled, scrollable) {
  56. scrolled.scroll(function () {
  57. if (scrolled.scrollTop() > _this.options.threshold) {
  58. if (link.is(":hidden")) {
  59. link.fadeIn(200);
  60. }
  61. if ($(_this.element).data("lastScrollTop") == null || $(_this.element).data("lastScrollTop") < scrolled.scrollTop()) {
  62. $("#jHueScrollUpAnchor").data("caller", scrollable);
  63. }
  64. $(_this.element).data("lastScrollTop", scrolled.scrollTop());
  65. }
  66. else {
  67. checkForAllScrolls();
  68. }
  69. });
  70. }
  71. function checkForAllScrolls() {
  72. var _allOk = true;
  73. $(document).find("[jHueScrollified='true']").each(function (cnt, item) {
  74. if ($(item).is("body")) {
  75. if ($(window).scrollTop() > _this.options.threshold) {
  76. _allOk = false;
  77. $("#jHueScrollUpAnchor").data("caller", $("body, html"));
  78. }
  79. }
  80. else {
  81. if ($(item).scrollTop() > _this.options.threshold) {
  82. _allOk = false;
  83. $("#jHueScrollUpAnchor").data("caller", $(item));
  84. }
  85. }
  86. });
  87. if (_allOk) {
  88. link.fadeOut(200);
  89. $("#jHueScrollUpAnchor").data("caller", null);
  90. }
  91. }
  92. $(document).on("click", "#jHueScrollUpAnchor", function (event) {
  93. if ($("#jHueScrollUpAnchor").data("caller") != null) {
  94. $("#jHueScrollUpAnchor").data("caller").animate({scrollTop: 0}, 300, function () {
  95. if ($(document).find("[jHueScrollified='true']").not($("#jHueScrollUpAnchor").data("caller")).is("body") && $(window).scrollTop() > _this.options.threshold) {
  96. $("#jHueScrollUpAnchor").data("caller", $("body, html"));
  97. }
  98. else {
  99. checkForAllScrolls();
  100. }
  101. });
  102. }
  103. return false;
  104. });
  105. };
  106. $.fn[pluginName] = function (options) {
  107. return this.each(function () {
  108. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  109. });
  110. }
  111. $[pluginName] = function (options) {
  112. new Plugin($("body"), options);
  113. };
  114. })(jQuery, window, document);