Skip to content

Commit d2633e7

Browse files
committed
add elastic header example
1 parent 940555f commit d2633e7

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

examples/elastic-header/index.html

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
6+
<title></title>
7+
<script src="../../dist/vue.js"></script>
8+
<script src="http://dynamicsjs.com/lib/dynamics.js"></script>
9+
<link rel="stylesheet" href="style.css">
10+
<!-- template for the component -->
11+
<template id="header-view-template" style="display:none">
12+
<div class="draggable-header-view"
13+
@mousedown="startDrag" @touchstart="startDrag"
14+
@mousemove="onDrag" @touchmove="onDrag"
15+
@mouseup="stopDrag" @touchend="stopDrag" @mouseleave="stopDrag">
16+
<svg class="bg" width="320" height="560">
17+
<path :d="headerPath" fill="#3F51B5"></path>
18+
</svg>
19+
<div class="header">
20+
<slot name="header"></slot>
21+
</div>
22+
<div class="content" :style="contentPosition">
23+
<slot name="content"></slot>
24+
</div>
25+
</div>
26+
</template>
27+
</head>
28+
<body @touchmove.prevent>
29+
30+
<draggable-header-view>
31+
<template slot="header">
32+
<h1>Elastic Draggable SVG Header</h1>
33+
<p>with <a href="http://vuejs.org">Vue.js</a> + <a href="http://dynamicsjs.com">dynamics.js</a></p>
34+
</template>
35+
<template slot="content">
36+
<p>Note this is just an effect demo - there are of course many additional details if you want to use this in production, e.g. handling responsive sizes, reload threshold and content scrolling. Those are out of scope for this quick little hack. However, the idea is that you can hide them as internal details of a Vue.js component and expose a simple Web-Component-like interface.</p>
37+
</template>
38+
</draggable-header-view>
39+
40+
<script>
41+
Vue.component('draggable-header-view', {
42+
template: '#header-view-template',
43+
data: function () {
44+
return {
45+
dragging: false,
46+
// quadratic bezier control point
47+
c: { x: 160, y: 160 },
48+
// record drag start point
49+
start: { x: 0, y: 0 }
50+
}
51+
},
52+
computed: {
53+
headerPath: function () {
54+
return 'M0,0 L320,0 320,160' +
55+
'Q' + this.c.x + ',' + this.c.y +
56+
' 0,160'
57+
},
58+
contentPosition: function () {
59+
var dy = this.c.y - 160
60+
var dampen = dy > 0 ? 2 : 4
61+
return {
62+
transform: 'translate3d(0,' + dy / dampen + 'px,0)'
63+
}
64+
}
65+
},
66+
methods: {
67+
startDrag: function (e) {
68+
e = e.changedTouches ? e.changedTouches[0] : e
69+
this.dragging = true
70+
this.start.x = e.pageX
71+
this.start.y = e.pageY
72+
},
73+
onDrag: function (e) {
74+
e = e.changedTouches ? e.changedTouches[0] : e
75+
if (this.dragging) {
76+
this.c.x = 160 + (e.pageX - this.start.x)
77+
// dampen vertical drag by a factor
78+
var dy = e.pageY - this.start.y
79+
var dampen = dy > 0 ? 1.5 : 4
80+
this.c.y = 160 + dy / dampen
81+
}
82+
},
83+
stopDrag: function () {
84+
if (this.dragging) {
85+
this.dragging = false
86+
dynamics.animate(this.c, {
87+
x: 160,
88+
y: 160
89+
}, {
90+
type: dynamics.spring,
91+
duration: 700,
92+
friction: 280
93+
})
94+
}
95+
}
96+
}
97+
})
98+
99+
new Vue({ el: 'body' })
100+
</script>
101+
</body>
102+
</html>

examples/elastic-header/style.css

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
h1 {
2+
font-weight: 300;
3+
font-size: 1.8em;
4+
margin-top: 0;
5+
}
6+
a {
7+
color: #fff;
8+
}
9+
.draggable-header-view {
10+
background-color: #fff;
11+
box-shadow: 0 4px 16px rgba(0,0,0,.15);
12+
width: 320px;
13+
height: 560px;
14+
overflow: hidden;
15+
margin: 30px auto;
16+
position: relative;
17+
font-family: 'Roboto', Helvetica, Arial, sans-serif;
18+
color: #fff;
19+
font-size: 14px;
20+
font-weight: 300;
21+
-webkit-user-select: none;
22+
-moz-user-select: none;
23+
-ms-user-select: none;
24+
user-select: none;
25+
}
26+
.draggable-header-view .bg {
27+
position: absolute;
28+
top: 0;
29+
left: 0;
30+
z-index: 0;
31+
}
32+
.draggable-header-view .header, .draggable-header-view .content {
33+
position: relative;
34+
z-index: 1;
35+
padding: 30px;
36+
box-sizing: border-box;
37+
}
38+
.draggable-header-view .header {
39+
height: 160px;
40+
}
41+
.draggable-header-view .content {
42+
color: #333;
43+
line-height: 1.5em;
44+
}

0 commit comments

Comments
 (0)