forked from ghepting/jquery-responsive-text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.responsiveText.js
104 lines (90 loc) · 3.04 KB
/
jquery.responsiveText.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Generated by CoffeeScript 1.6.3
/*
*
* jQuery ResponsiveText by Gary Hepting - https://github.com/ghepting/jquery-responsive-text
*
* Open source under the MIT License.
*
* Copyright © 2013 Gary Hepting. All rights reserved.
*
*/
(function() {
var ResponsiveText, delayedAdjust, responsiveTextIndex;
delayedAdjust = [];
responsiveTextIndex = 0;
ResponsiveText = (function() {
function ResponsiveText(el) {
this.index = responsiveTextIndex++;
this.el = el;
this.compression = $(this.el).data('compression') || 10;
this.minFontSize = $(this.el).data('min') || Number.NEGATIVE_INFINITY;
this.maxFontSize = $(this.el).data('max') || Number.POSITIVE_INFINITY;
this.scrollable = $(this.el).data('scrollable') || false;
this.scrollSpeed = $(this.el).data('scrollspeed') || 650;
this.scrollReset = $(this.el).data('scrollreset') || 200;
this.init();
}
ResponsiveText.prototype.init = function() {
$(this.el).wrapInner('<span class="responsiveText-wrapper" />');
this.adjustOnLoad();
this.adjustOnResize();
if (this.scrollable) {
return this.scrollOnHover();
}
};
ResponsiveText.prototype.resizeText = function() {
return $(this.el).css("font-size", Math.floor(Math.max(Math.min($(this.el).width() / this.compression, this.maxFontSize), this.minFontSize)));
};
ResponsiveText.prototype.adjustOnLoad = function() {
var _this = this;
return $(window).on('load', function() {
return _this.resizeText();
});
};
ResponsiveText.prototype.adjustOnResize = function() {
var _this = this;
return $(window).on('resize', function() {
clearTimeout(delayedAdjust[_this.index]);
return delayedAdjust[_this.index] = setTimeout(function() {
return _this.resizeText();
}, 20);
});
};
ResponsiveText.prototype.scrollOnHover = function() {
var _this = this;
$(this.el).css({
'overflow': 'hidden',
'text-overflow': 'ellipsis',
'white-space': 'nowrap'
});
return $(this.el).hover(function() {
_this.difference = _this.el.scrollWidth - $(_this.el).width();
if (_this.difference > _this.scrollSpeed) {
_this.scrollSpeed = _this.difference;
}
if (_this.difference > 0) {
$(_this.el).css('cursor', 'e-resize');
return $(_this.el).stop().animate({
"text-indent": -_this.difference
}, _this.scrollSpeed, function() {
return $(_this.el).css('cursor', 'text');
});
}
}, function() {
return $(_this.el).stop().animate({
"text-indent": 0
}, _this.scrollReset);
});
};
return ResponsiveText;
})();
(function($) {
var responsiveTextElements;
responsiveTextElements = [];
return $.fn.responsiveText = function(options) {
return this.each(function() {
return responsiveTextElements.push(new ResponsiveText(this));
});
};
})(jQuery);
}).call(this);