-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterialDesign.js
More file actions
59 lines (53 loc) · 2.08 KB
/
Copy pathmaterialDesign.js
File metadata and controls
59 lines (53 loc) · 2.08 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
/**
*
* @authors 石亚南 (1348571886@qq.com)
* @date 2016-06-14 14:48:42
* @version $Id$
*/
(function() {
'use strict';
document.addEventListener("DOMContentLoaded", DOMContentLoaded, false);
function DOMContentLoaded() {
for (var i = 0, btn; btn = document.querySelectorAll('[materialDesign]')[i++];) {
btn.addEventListener('mousedown', materialDesignBg);
}
}
function materialDesignBg(e) {
e.preventDefault();
e.stopPropagation();
var tag = this;
var div = tag.querySelector(".animate-hand");
if (!div) {
div = document.createElement("div");
}
div.className = 'animate-hand';
tag.insertBefore(div, tag.firstElementChild);
var scale = Math.round(tag.offsetWidth / 100) || 1;
// 优化移动端获取不到精确位置问题
var left = e.type==='click' ? e.layerX : (e.touches[0].clientX - tag.offsetLeft);
var top = e.type==='click' ? e.layerY : (e.touches[0].clientY - tag.offsetTop);
//优化移动端固定元素的位置获取
if (e.type !== 'click') {
deepCheckFixed(tag);
}
function deepCheckFixed(tag) {
var parent = tag.parentElement;
if (parent.tagName !== 'BODY') {
if (getComputedStyle(parent, null).getPropertyValue('position') === 'fixed') {
left = e.touches[0].clientX - tag.offsetLeft - parent.offsetLeft;
top = e.touches[0].clientY - parent.offsetTop;
} else {
deepCheckFixed(parent);
}
}
}
div.style.left = left + "px";
div.style.top = top + "px";
scale = scale > 6 ? 6 : scale;
div.className = "animate-hand animate ripple_" + (e.changedTouches ? scale + 1 : scale);
if (tag.nodeName != 'A' && tag.getAttribute("href")) {
location.href = tag.getAttribute("href");
}
return false;
}
})();