Skip to content

Commit 01aaf1a

Browse files
committed
Fix resize issue with blur
1 parent b433f3c commit 01aaf1a

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

animated-blur/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
animation.update();
6666
m.set(img, animation);
6767
});
68+
69+
window.onresize = _ => {
70+
m.forEach(anim => {
71+
anim.resize();
72+
});
73+
};
6874
}
6975
init();
7076
})();

animated-blur/scripts/animated_blur.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class AnimatedBlur {
2424
this.initialized = false;
2525
element.classList.add('animated-blur');
2626
}
27+
2728
static get BLUR_MODE() {
2829
return {
2930
BLUR: 1,
@@ -87,6 +88,25 @@ class AnimatedBlur {
8788
document.getElementsByTagName('head')[0].appendChild(s);
8889
}
8990
}
91+
92+
calculateMargin() {
93+
this.marginTop =
94+
parseInt(window.getComputedStyle(this.element).marginTop);
95+
this.marginLeft =
96+
parseInt(window.getComputedStyle(this.element).marginLeft);
97+
if (this.marginTop == 0) {
98+
var header = 'p, pre, h1, h2, h3, h4, h5, h6';
99+
var descendants = this.element.querySelectorAll(header);
100+
for (var i = 0; i < descendants.length; ++i) {
101+
if (window.getComputedStyle(descendants[i]).marginTop != '0px') {
102+
this.marginTop =
103+
parseInt(window.getComputedStyle(descendants[i]).marginTop);
104+
break;
105+
}
106+
}
107+
}
108+
}
109+
90110
// Create template for shadow dom. It includes the element to be animated
91111
// and its style.
92112
createTemplate() {
@@ -126,24 +146,8 @@ class AnimatedBlur {
126146
var height = this.element.clientHeight;
127147
var container = document.createElement('div');
128148
container.id = this.name + '-clonedElement';
129-
// The following margin* logic should be optimized.
130-
var marginTop =
131-
parseInt(window.getComputedStyle(this.element).marginTop);
132-
var marginLeft =
133-
parseInt(window.getComputedStyle(this.element).marginLeft);
134-
if (marginTop == 0) {
135-
var header = 'p, pre, h1, h2, h3, h4, h5, h6';
136-
var descendants = this.element.querySelectorAll(header);
137-
for (var i = 0; i < descendants.length; ++i) {
138-
if (window.getComputedStyle(descendants[i]).marginTop != '0px') {
139-
marginTop =
140-
parseInt(window.getComputedStyle(descendants[i]).marginTop);
141-
break;
142-
}
143-
}
144-
}
145-
container.style.top = this.element.offsetTop - marginTop + 'px';
146-
container.style.left = this.element.offsetLeft - marginLeft + 'px';
149+
container.style.top = this.element.offsetTop - this.marginTop + 'px';
150+
container.style.left = this.element.offsetLeft - this.marginLeft + 'px';
147151
container.style.width = width + 'px';
148152
container.style.height = height + 'px';
149153
container.classList.add('composited');
@@ -186,6 +190,7 @@ class AnimatedBlur {
186190
if (this.initialized)
187191
return;
188192
document.body.classList.add('bodyStyle');
193+
this.calculateMargin();
189194
this.setupKeyFrames();
190195
this.createTemplate();
191196
this.cloneElements();
@@ -229,8 +234,13 @@ class AnimatedBlur {
229234
}
230235

231236
resize() {
232-
var elements = document.body.querySelectorAll('.clonedElement');
237+
var blur = 'div[id^="' + this.name + '"][id$="clonedElement"]';
238+
var elements = document.body.querySelectorAll(blur);
233239
for (var i = 0; i < elements.length; ++i) {
240+
elements[i].style.top = this.element.offsetTop
241+
- this.marginTop + 'px';
242+
elements[i].style.left = this.element.offsetLeft
243+
- this.marginLeft + 'px';
234244
elements[i].style.width = this.element.clientWidth + 'px';
235245
elements[i].style.height = this.element.clientHeight + 'px';
236246
}

0 commit comments

Comments
 (0)