Skip to content

Commit 6005480

Browse files
authored
Merge pull request #17 from PygmySlowLoris/ripple-relative-to-target
Ripple relative to target
2 parents 3ecfa20 + dfc5f06 commit 6005480

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ This directive it's meant to be used in any element in which you like to achieve
1616
npm install vue-ripple-directive --save
1717
```
1818

19+
>### Important Notice
20+
>The directive will work better if the element where you attach it is **relative positioned**.
21+
>Although the directive will try to set `position: relative` if the element does not have this property.
22+
>This is because the ripple since v2.0.* is `position: absolute`, to avoid trailing issues when elements with the directive in the UI move.
23+
1924
## Options
2025

2126
Optional parameter to pass to the directive.

dist/build.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/build.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-ripple-directive",
3-
"version": "1.1.1",
3+
"version": "2.0.1-beta",
44
"description": "Vue Material Ripple Effect Directive",
55
"main": "src/ripple.js",
66
"repository": {
@@ -26,9 +26,9 @@
2626
],
2727
"license": "MIT",
2828
"bugs": {
29-
"url": "https://github.com/PygmySlowLoris/vue-fab/issues"
29+
"url": "https://github.com/PygmySlowLoris/vue-ripple-directive/issues"
3030
},
31-
"homepage": "https://github.com/PygmySlowLoris/vue-ripple-effect#readme",
31+
"homepage": "https://github.com/PygmySlowLoris/vue-ripple-directive#readme",
3232
"scripts": {
3333
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
3434
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules",

src/ripple.js

+40-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ var Ripple = {
1818

1919
function rippler(event, el) {
2020
var target = el;
21+
// Get border to avoid offsetting on ripple container position
22+
var targetBorder = parseInt((getComputedStyle(target).borderWidth).replace('px', ''));
2123

2224
// Get necessary variables
2325
var rect = target.getBoundingClientRect(),
@@ -30,11 +32,14 @@ var Ripple = {
3032
maxX = Math.max(dx, width - dx),
3133
maxY = Math.max(dy, height - dy),
3234
style = window.getComputedStyle(target),
33-
radius = Math.sqrt((maxX * maxX) + (maxY * maxY));
35+
radius = Math.sqrt((maxX * maxX) + (maxY * maxY)),
36+
border = (targetBorder > 0 ) ? targetBorder : 0;
3437

3538
// Create the ripple and its container
3639
var ripple = document.createElement("div"),
3740
rippleContainer = document.createElement("div");
41+
rippleContainer.className = 'ripple-container';
42+
ripple.className = 'ripple';
3843

3944
//Styles for ripple
4045
ripple.style.marginTop= '0px';
@@ -50,21 +55,29 @@ var Ripple = {
5055

5156
//Styles for rippleContainer
5257
rippleContainer.style.position= 'absolute';
53-
rippleContainer.style.left = '0';
54-
rippleContainer.style.top = '0';
58+
rippleContainer.style.left = 0 - border + 'px';
59+
rippleContainer.style.top = 0 - border + 'px';
5560
rippleContainer.style.height = '0';
5661
rippleContainer.style.width = '0';
5762
rippleContainer.style.pointerEvents = 'none';
5863
rippleContainer.style.overflow = 'hidden';
5964

65+
// Store target position to change it after
66+
var storedTargetPosition = ((target.style.position).length > 0) ? target.style.position : getComputedStyle(target).position;
67+
// Change target position to relative to guarantee ripples correct positioning
68+
if (storedTargetPosition !== 'relative') {
69+
target.style.position = 'relative';
70+
}
71+
6072
rippleContainer.appendChild(ripple);
61-
document.body.appendChild(rippleContainer);
73+
target.appendChild(rippleContainer);
6274

6375
ripple.style.marginLeft = dx + "px";
6476
ripple.style.marginTop = dy + "px";
6577

66-
rippleContainer.style.left = left + (((window.pageXOffset || document.scrollLeft) - (document.clientLeft || 0)) || 0) + "px";
67-
rippleContainer.style.top = top + (((window.pageYOffset || document.scrollTop) - (document.clientTop || 0)) || 0) + "px";
78+
// No need to set positioning because ripple should be child of target and to it's relative position.
79+
// rippleContainer.style.left = left + (((window.pageXOffset || document.scrollLeft) - (document.clientLeft || 0)) || 0) + "px";
80+
// rippleContainer.style.top = top + (((window.pageYOffset || document.scrollTop) - (document.clientTop || 0)) || 0) + "px";
6881
rippleContainer.style.width = width + "px";
6982
rippleContainer.style.height = height + "px";
7083
rippleContainer.style.borderTopLeftRadius = style.borderTopLeftRadius;
@@ -92,6 +105,27 @@ var Ripple = {
92105
}, 850);
93106

94107
el.removeEventListener('mouseup', clearRipple, false);
108+
109+
// After removing event set position to target to it's original one
110+
// Timeout it's needed to avoid jerky effect of ripple jumping out parent target
111+
setTimeout(function () {
112+
113+
var clearPosition = true;
114+
for(var i = 0; i < target.childNodes.length; i++) {
115+
if(target.childNodes[i].className === 'ripple-container') {
116+
clearPosition = false;
117+
}
118+
}
119+
120+
if(clearPosition) {
121+
if(storedTargetPosition !== 'static') {
122+
target.style.position = storedTargetPosition;
123+
} else {
124+
target.style.position = '';
125+
}
126+
}
127+
128+
}, props.transition + 250)
95129
}
96130

97131
if(event.type === 'mousedown') {

0 commit comments

Comments
 (0)