This repository has been archived by the owner on Jan 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathjquery.responsiveText.coffee
77 lines (63 loc) · 1.92 KB
/
jquery.responsiveText.coffee
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
###
*
* 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.
*
###
delayedAdjust = []
responsiveTextIndex = 0
class ResponsiveText
constructor: (el) ->
@index = responsiveTextIndex++
@el = el
@compression = $(@el).data('compression') || 10
@minFontSize = $(@el).data('min') || Number.NEGATIVE_INFINITY
@maxFontSize = $(@el).data('max') || Number.POSITIVE_INFINITY
@scrollable = $(@el).data('scrollable') || false
@scrollSpeed = $(@el).data('scrollspeed') || 650
@scrollReset = $(@el).data('scrollreset') || 200
@init()
init: ->
$(@el).wrapInner('<span class="responsiveText-wrapper" />')
@adjustOnLoad()
@adjustOnResize()
@scrollOnHover() if @scrollable
resizeText: ->
$(@el).css "font-size", Math.floor( Math.max( Math.min( ($(@el).width() / @compression), @maxFontSize ), @minFontSize ) )
adjustOnLoad: ->
$(window).on 'load', =>
@resizeText()
adjustOnResize: ->
$(window).on 'resize', =>
clearTimeout(delayedAdjust[@index])
delayedAdjust[@index] = setTimeout(=>
@resizeText()
, 20)
scrollOnHover: ->
$(@el).css
'overflow': 'hidden'
'text-overflow': 'ellipsis'
'white-space': 'nowrap'
$(@el).hover =>
@difference = @el.scrollWidth - $(@el).width()
@scrollSpeed = @difference if @difference > @scrollSpeed
if @difference > 0
$(@el).css('cursor', 'e-resize')
$(@el).stop().animate
"text-indent": -@difference
, @scrollSpeed
, =>
$(@el).css('cursor', 'text')
, =>
$(@el).stop().animate
"text-indent": 0
, @scrollReset
(($) ->
responsiveTextElements = []
$.fn.responsiveText = (options) ->
@each ->
responsiveTextElements.push( new ResponsiveText(@) )
) jQuery