-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcards.js
141 lines (126 loc) · 4.86 KB
/
cards.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
import Soo from "https://unpkg.com/[email protected]/build/soo.esm.js";
class Card extends Soo {
css() {
return `.card {
overflow:hidden;
width:100%;
height:100%;
border-radius: 15px;
box-shadow: 5px 5px 20px -5px rgba(0,0,0,0.6);
perspective: 1200px;
position: relative;
transform-style: preserve-3d;
transform: translatez(35px);
transition: transform 200ms ease-out;
background: url(${this.getAttribute("background")}) center / cover no-repeat;
background-position: calc(var(--mouseX) * 0.35px) calc(var(--mouseY) * -0.35px);
}
.card img {
transform: translateX(calc(var(--mouseX) * -1px)) translateY(calc(var(--mouseY) * 1px));
}
.title {
align-items: center;
bottom: 0;
display: flex;
flex-direction: column;
height: 70px;
justify-content: center;
position: absolute;
width: 100%;
color:white;
font-size:20px;
z-index: 2;
text-shadow: calc(var(--mouseX) * -0.2px) calc(var(--mouseY) * -0.2px) 0.5px rgba(0,0,0,0.6);x);
}
@media screen and (max-width: 1024px) {
.card {
height:200px;
}
}
`
}
render() {
return HTML`<section class="card">
<img src="${this.getAttribute("img") || ""}"></img>
<h4 class="title">${this.getAttribute("title") || "title"}</h4>
</section>`
}
}
class Cards extends Soo {
installed() {
const range = 40;
const calcValue = (a, b) => (a / b * range - range / 2).toFixed(1);
document.addEventListener('mousemove', ({ x, y }) => {
window.requestAnimationFrame(() => {
const yValue = calcValue(y, window.innerHeight);
const xValue = calcValue(x, window.innerWidth);
this.style.setProperty('--mouseX', `${xValue}`);
this.style.setProperty('--mouseY', `${yValue}`);
})
});
window.addEventListener("deviceorientation", (event) => {
window.requestAnimationFrame(() => {
const yValue = calcValue(event.beta * 5, window.innerHeight);
const xValue = calcValue(event.gamma * 5, window.innerWidth);
this.style.setProperty('--mouseX', `${xValue}`);
this.style.setProperty('--mouseY', `${yValue}`);
})
});
}
css() {
return `
:root {
--mouseX: 0px;
--mouseY: 0px;
}
:host {
background: #fff;
border-radius: 15px;
box-shadow: 0px 10px 20px 20px rgba(0,0,0,0.17);
display: grid;
padding: 30px 35px;
perspective: 1800px;
text-align: left;
transform-origin: 50% 50%;
transform-style: preserve-3d;
transform: rotateX(calc(var(--mouseY) * 1deg)) rotateY(calc(var(--mouseX) * 1deg));
min-width: 595px;
min-height: auto;
width:95%;
justify-self:center;
}
.cards {
display:grid;
width:100%;
height:100%;
grid-template-columns: repeat(3, 15vw);
grid-auto-rows:max-content;
grid-column-gap:30px;
grid-row-gap:30px;
}
img {
width:100px;
}
@media screen and (max-width: 1024px) {
:host {
min-width:80vw;
padding:0 10px;
justify-content:center;
margin:50px 0;
}
.cards {
grid-template-columns: repeat(auto-fill, minmax(60vw, 0.9fr));
min-width:100%;
height:100%;
}
}
`
}
render() {
return HTML`<div class="cards">
<slot></slot>
</div>`
}
}
customElements.define("paralax-cards", Cards);
customElements.define("card-element", Card);