placeholder_wev8.js
5.97 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
~function(win, $) {
var px = 'px'
var doc = win.document
var body = doc.body
var docElem = doc.documentElement
$.viewSize = function() {
return {
width: win['innerWidth'] || docElem.clientWidth,
height: win['innerHeight'] || docElem.clientHeight
}
};
$.fn.center = function(option, callback) {
var settings = $.extend({}, option)
var position = settings.position || 'fixed'
var $win = $(win)
function fixIE6($el) {
$el[0].style.position = 'absolute'
$win.scroll(function() {
move($el)
})
}
function move($that) {
var that = $that[0]
var size = $.viewSize()
var x = (size.width)/2 - (that.clientWidth)/2
var y = (size.height)/2 - (that.clientHeight)/2
if ($.ie6) {
var scrollTop = docElem.scrollTop || document.body.scrollTop
y += scrollTop
}
$that.css({
top: y + px,
left: x + px
})
}
function init($that, option) {
$that.css({
position: position
}).show()
// ie6 don't support position 'fixed'
if (position === 'fixed' && $.ie6) {
fixIE6($that)
}
move($that)
}
return this.each(function() {
var $that = $(this)
init($that, option)
if (callback) callback($that)
})
};
$.uiParse = function(action) {
var arr = action.split('|').slice(1)
var len = arr.length
var res = [], exs
var boo = /^(true|false)$/
for (var i = 0; i < len; i++) {
var item = arr[i]
if (item == '&') {
item = undefined
} else if (exs = item.match(boo)) {
item = exs[0] == 'true' ? true : false
}
res[i] = item
}
return res
};
}(window, window.jQuery);
/**
* PlaceHolder组件
* $(input).focusPic({
* word: // @string 提示文本
* color: // @string 文本颜色
* evtType: // @string focus|keydown 触发placeholder的事件类型
* zIndex: // 模拟placeholder的zIndex
* diffPaddingLeft: 距离左侧的left,光标位置可调,默认取输入域的paddingLeft+3
* })
*
* NOTE:
* evtType默认是focus,即鼠标点击到输入域时默认文本消失,keydown则模拟HTML5 placeholder属性在Firefox/Chrome里的特征,光标定位到输入域后键盘输入时默认文本才消失。
* 此外,对于HTML5 placeholder属性,IE10+和Firefox/Chrome/Safari的表现形式也不一致,因此内部实现不采用原生placeholder属性
*/
~function() {
$.fn.placeholder = function(option, callback) {
var settings = $.extend({
word: '',
color: '#999',
evtType: 'focus',
zIndex: 20,
diffPaddingLeft: 3
}, option)
function bootstrap($that) {
// some alias
var word = settings.word
var color = settings.color
var evtType = settings.evtType
var zIndex = settings.zIndex
var diffPaddingLeft = settings.diffPaddingLeft
// default css
var width = $that.outerWidth()
var height = $that.outerHeight()
var fontSize = $that.css('font-size')
var fontFamily = $that.css('font-family')
var paddingLeft = $that.css('padding-left')
// process
var thatid = $that.attr("id");
paddingLeft = parseInt(paddingLeft, 10) + diffPaddingLeft
// redner
var $placeholder = jQuery("#placeholder"+thatid);
if($placeholder.size()==0) {
$placeholder = $('<span class="placeholder" id="placeholder'+thatid+'">')
}
$placeholder.css({
position: 'absolute',
zIndex: '20',
color: color,
width: (width - paddingLeft) + 'px',
height: height + 'px',
fontSize: fontSize,
paddingLeft: paddingLeft + 'px',
fontFamily: fontFamily
}).text(word).hide()
// 位置调整
move()
// textarea 不加line-heihgt属性
if ($that.is('input')) {
$placeholder.css({
lineHeight: height + 'px'
})
}
//$placeholder.appendTo(document.body)
$that.after($placeholder);
// 内容为空时才显示,比如刷新页面输入域已经填入了内容时
var val = $that.val()
if ( val == '' && $that.is(':visible') ) {
$placeholder.show()
} else {
jQuery("#placeholder"+thatid).remove();
}
function hideAndFocus() {
$placeholder.hide()
$that[0].focus()
}
function move() {
var offset = $that.offset()
var top = offset.top
var left = offset.left
$placeholder.css({
top: top,
left: left
})
}
function asFocus() {
$placeholder.click(function() {
hideAndFocus()
// 盖住后无法触发input的click事件,需要模拟点击下
setTimeout(function(){
$that.click()
}, 100)
})
// IE有些bug,原本不用加此句
$that.click(hideAndFocus)
$that.blur(function() {
var txt = $that.val()
if (txt == '') {
$placeholder.show()
}
})
}
function asKeydown() {
$placeholder.click(function() {
$that[0].focus()
})
}
if (evtType == 'focus') {
asFocus()
} else if (evtType == 'keydown') {
asKeydown()
} else {
}
$that.keyup(function() {
var txt = $that.val()
var isFocus=$that.is(":focus");
if (txt == '' && !isFocus) {
$placeholder.show()
} else {
$placeholder.hide()
}
})
// 窗口缩放时处理
$(window).resize(function() {
move()
})
// cache
$that.data('el', $placeholder)
$that.data('move', move)
}
return this.each(function() {
var $elem = $(this)
bootstrap($elem)
if ($.isFunction(callback)) callback($elem)
})
}
/*
* 自动初始化,配置参数按照使用频率先后排序,即最经常使用的在前,不经常使用的往后,使用默认参数替代
*
* 格式:data-ui="u-placeholder|word|evtType|color
* 示例:data-ui="u-placeholder|默认文字
*
*/
$(function() {
$('[data-ui^="u-placeholder"]').each(function() {
doPlaceholder(this)
})
})
}();