mui.transparent.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. (function($, window) {
  2. var CLASS_ACTIVE = $.className('active');
  3. var rgbaRegex = /^rgba\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d*(?:\.\d+)?)\)$/;
  4. var getColor = function(colorStr) {
  5. var matches = colorStr.match(rgbaRegex);
  6. if (matches && matches.length === 5) {
  7. return [
  8. matches[1],
  9. matches[2],
  10. matches[3],
  11. matches[4]
  12. ];
  13. }
  14. return [];
  15. };
  16. var Transparent = function(element, options) {
  17. this.element = element;
  18. this.options = $.extend({
  19. top: 0, //距离顶部高度(到达该高度即触发)
  20. offset: 150, //滚动透明距离
  21. duration: 16, //过渡时间
  22. scrollby: window//监听滚动距离容器
  23. }, options || {});
  24. this.scrollByElem = this.options.scrollby || window;
  25. if (!this.scrollByElem) {
  26. throw new Error("监听滚动的元素不存在");
  27. }
  28. this.isNativeScroll = false;
  29. if (this.scrollByElem === window) {
  30. this.isNativeScroll = true;
  31. } else if (!~this.scrollByElem.className.indexOf($.className('scroll-wrapper'))) {
  32. this.isNativeScroll = true;
  33. }
  34. this._style = this.element.style;
  35. this._bgColor = this._style.backgroundColor;
  36. var color = getColor(mui.getStyles(this.element, 'backgroundColor'));
  37. if (color.length) {
  38. this._R = color[0];
  39. this._G = color[1];
  40. this._B = color[2];
  41. this._A = parseFloat(color[3]);
  42. this.lastOpacity = this._A;
  43. this._bufferFn = $.buffer(this.handleScroll, this.options.duration, this);
  44. this.initEvent();
  45. } else {
  46. throw new Error("元素背景颜色必须为RGBA");
  47. }
  48. };
  49. Transparent.prototype.initEvent = function() {
  50. this.scrollByElem.addEventListener('scroll', this._bufferFn);
  51. if (this.isNativeScroll) { //原生scroll
  52. this.scrollByElem.addEventListener($.EVENT_MOVE, this._bufferFn);
  53. }
  54. }
  55. Transparent.prototype.handleScroll = function(e) {
  56. var y = window.scrollY;
  57. if (!this.isNativeScroll && e && e.detail) {
  58. y = -e.detail.y;
  59. }
  60. var opacity = (y - this.options.top) / this.options.offset + this._A;
  61. opacity = Math.min(Math.max(this._A, opacity), 1);
  62. this._style.backgroundColor = 'rgba(' + this._R + ',' + this._G + ',' + this._B + ',' + opacity + ')';
  63. if (opacity > this._A) {
  64. this.element.classList.add(CLASS_ACTIVE);
  65. } else {
  66. this.element.classList.remove(CLASS_ACTIVE);
  67. }
  68. if (this.lastOpacity !== opacity) {
  69. $.trigger(this.element, 'alpha', {
  70. alpha: opacity
  71. });
  72. this.lastOpacity = opacity;
  73. }
  74. };
  75. Transparent.prototype.destory = function() {
  76. this.scrollByElem.removeEventListener('scroll', this._bufferFn);
  77. this.scrollByElem.removeEventListener($.EVENT_MOVE, this._bufferFn);
  78. this.element.style.backgroundColor = this._bgColor;
  79. this.element.mui_plugin_transparent = null;
  80. };
  81. $.fn.transparent = function(options) {
  82. options = options || {};
  83. var transparentApis = [];
  84. this.each(function() {
  85. var transparentApi = this.mui_plugin_transparent;
  86. if (!transparentApi) {
  87. var top = this.getAttribute('data-top');
  88. var offset = this.getAttribute('data-offset');
  89. var duration = this.getAttribute('data-duration');
  90. var scrollby = this.getAttribute('data-scrollby');
  91. if (top !== null && typeof options.top === 'undefined') {
  92. options.top = top;
  93. }
  94. if (offset !== null && typeof options.offset === 'undefined') {
  95. options.offset = offset;
  96. }
  97. if (duration !== null && typeof options.duration === 'undefined') {
  98. options.duration = duration;
  99. }
  100. if (scrollby !== null && typeof options.scrollby === 'undefined') {
  101. options.scrollby = document.querySelector(scrollby) || window;
  102. }
  103. transparentApi = this.mui_plugin_transparent = new Transparent(this, options);
  104. }
  105. transparentApis.push(transparentApi);
  106. });
  107. return transparentApis.length === 1 ? transparentApis[0] : transparentApis;
  108. };
  109. $.ready(function() {
  110. $($.classSelector('.bar-transparent')).transparent();
  111. });
  112. })(mui, window);