jquery.scrollup.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. if ($("#jHueScrollUpAnchor").length > 0) { // just one scroll up per page
  44. return;
  45. }
  46. var link = $("<a/>").attr("id", "jHueScrollUpAnchor").attr("href", "javascript:void(0)").html("<i class='fa fa-chevron-up'></i>").appendTo("body");
  47. if ($(_this.element).is("body")) {
  48. $(window).scroll(function () {
  49. $(($(window).scrollTop() > _this.options.threshold) ? link.fadeIn(200) : link.fadeOut(200));
  50. });
  51. link.click(function (event) {
  52. $("body, html").animate({scrollTop: $(_this.element).position().top}, 300);
  53. return false;
  54. });
  55. }
  56. else {
  57. $(_this.element).scroll(function () {
  58. $(($(_this.element).scrollTop() > _this.options.threshold) ? link.fadeIn(200) : link.fadeOut(200));
  59. });
  60. link.click(function (event) {
  61. $(_this.element).animate({scrollTop: 0}, 300);
  62. return false;
  63. });
  64. }
  65. };
  66. $.fn[pluginName] = function (options) {
  67. return this.each(function () {
  68. if (!$.data(this, 'plugin_' + pluginName)) {
  69. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  70. }
  71. else {
  72. $.data(this, 'plugin_' + pluginName).setOptions(options);
  73. }
  74. });
  75. }
  76. $[pluginName] = function (options) {
  77. new Plugin($("body"), options);
  78. };
  79. })(jQuery, window, document);