-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.akTextOverlay.js
166 lines (142 loc) · 4.41 KB
/
jquery.akTextOverlay.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*!
* jQuery.akTextOverlay
* Version: 0.1
*
* Copyright(c) 2014 akey <[email protected]>
* MIT Licensed
*/
(function($)
{
'use strict';
$.fn.akTextOverlay = function(options) {
/**
* Option - defaults
*/
// MEMO: HEX値を#付きで渡せるようにするかどうか検討中
options = $.extend({
backgroundColor: '000000',
opacity: 0.7,
color: 'ffffff',
direction: 'toT',
tag: 'div',
className: '',
transition: true,
duration: 300,
easing: 'ease'
}, options);
/**
* Core
*/
return this.each(function(){
var $target = $(this),
targetW = $target.width(),
targetH = $target.height(),
style;
createTextLayer($target, options);
$target.css({ position: 'relative', overflow: 'hidden', width: targetW, height: targetH });
var $text = $target.find(options.tag);
initializeTextStyle($text, options, targetW, targetH);
// MEMO: animate のときに easing を渡せるようにするか迷ってる(IE9以下のために手厚くしたくない)
var animateFunc = options.transition ? 'css' : 'animate';
var animatDuration = options.transition ? '' : options.duration;
$target.on({
mouseenter: function() {
style = moveTextPosition(options);
$text[animateFunc](style, animatDuration);
},
mouseleave: function() {
style = initTextPosition(options, targetW, targetH);
$text[animateFunc](style, animatDuration);
}
});
});
/**
* Method - private
*/
// 画像の上に重なるDOMを作る
function createTextLayer($dom, options) {
// MEMO: attr() でも値の取得は可能だけど data() にしといた
// data* 属性を上書きしたいときは attr() だと都合が悪いらしい(https://w3g.jp/blog/studies/jquery-data-attr-cache)
var text = $dom.find('img').data('text');
var htmlSrc = '<' + options.tag + '>';
if (options.className) htmlSrc = '<' + options.tag + ' class="' + options.className + '">';
htmlSrc += text;
htmlSrc += '</' + options.tag + '>';
$dom.find('img').after(htmlSrc);
}
// $text のスタイルの初期化をする
function initializeTextStyle($dom, options, width, height) {
var backgroundColor = hexToRGBA(options.backgroundColor, options.opacity);
var style = {
width: '100%',
height: '100%',
position: 'absolute',
top: '0',
left: '0',
backgroundColor: 'rgba(' + backgroundColor + ')',
color: '#' + options.color,
boxSizing: 'border-box'
};
$.extend(style, initTextPosition(options, width, height));
if (options.transition) {
var transition = {
transitionProperty: 'top, left',
transitionDuration: options.duration / 1000 + 's',
transitionTimingFunction: options.easing,
};
$.extend(style, transition);
}
$dom.css(style);
}
// HEX値(16進数)をRGB値(10進数)に変更して、カンマ区切りで RGBA を返す
function hexToRGBA(hex, opacity) {
var i, rgb, rgba;
rgb = [hex.substr(0, 2), hex.substr(2, 2), hex.substr(4, 2)];
rgba = [];
for (i = 0; i < rgb.length; i++) {
rgba.push(parseInt(rgb[i], 16));
}
rgba.push(opacity);
rgba = rgba.join(',');
return rgba;
}
// テキストレイヤーの初期位置を設定
function initTextPosition(options, width, height) {
var style = {};
switch(options.direction) {
case 'toT':
style.top = height + 'px';
break;
case 'toB':
style.top = '-' + height + 'px';
break;
case 'toR':
style.left = width + 'px';
break;
case 'toL':
style.left = '-' + width + 'px';
break;
default:
style.top = height + 'px';
}
return style;
}
// テキストレイヤーの移動後の位置
function moveTextPosition(options) {
var style = {};
switch(options.direction) {
case 'toT':
case 'toB':
style.top = 0;
break;
case 'toR':
case 'toL':
style.left = 0;
break;
default:
style.top = 0;
}
return style;
}
};
})(jQuery);