Skip to content

Commit 083685c

Browse files
author
Evan You
committed
improve mobile experience
1 parent 1db750e commit 083685c

File tree

10 files changed

+145
-92
lines changed

10 files changed

+145
-92
lines changed

themes/vue/layout/layout.ejs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,29 @@
1515
<script src="/js/vue.min.js"></script>
1616
</head>
1717
<body>
18+
<div id="mobile-bar">
19+
<a class="menu-button"></a>
20+
<a class="logo" href="/"></a>
21+
</div>
22+
1823
<% if (page.index) { %>
1924
<%- partial('partials/index') %>
2025
<% } else { %>
2126
<%- partial('partials/header') %>
2227
<% } %>
28+
2329
<div id="main">
24-
<% if (page.blog_index) { %>
25-
<%- partial('partials/blog') %>
26-
<% } else { %>
27-
<%- body %>
28-
<% } %>
30+
<% if (page.blog_index) { %>
31+
<%- partial('partials/blog') %>
32+
<% } else { %>
33+
<%- body %>
34+
<% } %>
2935
</div>
30-
<script src="/js/smooth-scroll.min.js"></script>
31-
<script src="/js/common.js"></script>
36+
37+
<% if (!page.index) { %>
38+
<script src="/js/smooth-scroll.min.js"></script>
39+
<script src="/js/common.js"></script>
40+
<% } %>
41+
3242
</body>
3343
</html>

themes/vue/layout/partials/blog.ejs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
<div class="sidebar">
2+
<ul class="main-menu">
3+
<%- partial('partials/main_menu') %>
4+
</ul>
5+
</div>
16
<div class="content blog">
27
<h1 style="text-align: center; margin-bottom:.5em">The Vue.js Blog</h1>
38
<% site.posts.sort('date', -1).each(function (post) { %>

themes/vue/layout/partials/header.ejs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
<span>Vue.js</span>
55
</a>
66
<ul id="nav">
7-
<li><a href="/guide/" class="nav-link<%- page.path.match(/guide/) ? ' current' : '' %>">Guide</a></li>
8-
<li><a href="/api/" class="nav-link<%- page.path.match(/api/) ? ' current' : '' %>">API Reference</a></li>
9-
<br class="break" />
10-
<li><a href="/examples/" class="nav-link<%- page.path.match(/examples/) ? ' current' : '' %>">Examples</a></li>
11-
<li><a href="/blog/" class="nav-link<%- (page.path.match(/blog/) || is_post()) ? ' current' : '' %>">Blog</a></li>
12-
<li><a href="https://github.com/yyx990803/vue" target="_blank" class="nav-link">GitHub</a></li>
7+
<%- partial('partials/main_menu') %>
138
</ul>
149
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<li><a href="/guide/" class="nav-link<%- page.path.match(/guide/) ? ' current' : '' %>">Guide</a></li>
2+
<li><a href="/api/" class="nav-link<%- page.path.match(/api/) ? ' current' : '' %>">API Reference</a></li>
3+
<li><a href="/examples/" class="nav-link<%- page.path.match(/examples/) ? ' current' : '' %>">Examples</a></li>
4+
<li><a href="/blog/" class="nav-link<%- (page.path.match(/blog/) || is_post()) ? ' current' : '' %>">Blog</a></li>
5+
<li><a href="https://github.com/yyx990803/vue" target="_blank" class="nav-link">GitHub</a></li>
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<div class="sidebar">
2-
<div class="toggle">···</div>
2+
<ul class="main-menu">
3+
<%- partial('partials/main_menu') %>
4+
</ul>
35
<div class="list">
46
<h2><%- type.charAt(0).toUpperCase() + type.slice(1) %></h2>
57
<ul class="menu-root">
6-
<% site.pages.find({type: type}).sort('order').each(function (p) { %>
7-
<li><a href="/<%- p.path %>" class="sidebar-link<%- page.title === p.title ? ' current' : '' %>"><%- p.title %></a></li>
8-
<% }) %>
9-
<li style="margin:10px 0 3px">
10-
<script data-gittip-username="yyx990803"
11-
data-gittip-widget="button"
12-
src="//gttp.co/v1.js"></script>
13-
</li>
8+
<% site.pages.find({type: type}).sort('order').each(function (p) { %>
9+
<li><a href="/<%- p.path %>" class="sidebar-link<%- page.title === p.title ? ' current' : '' %>"><%- p.title %></a></li>
10+
<% }) %>
11+
<li style="margin:10px 0 3px">
12+
<script data-gittip-username="yyx990803"
13+
data-gittip-widget="button"
14+
src="//gttp.co/v1.js"></script>
15+
</li>
1416
</ul>
1517
</div>
1618
</div>

themes/vue/layout/post.ejs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<div class="sidebar blog">
2-
<div class="toggle">···</div>
2+
<ul class="main-menu">
3+
<%- partial('partials/main_menu') %>
4+
</ul>
35
<div class="list">
46
<h2>Recent Posts</h2>
57
<ul>
68
<% site.posts.sort('date', -1).limit(10).each(function (post) { %>
79
<li>
8-
<a href="/<%- post.path %>" class="sidebar-link"><%- post.title %></a>
10+
<a href="/<%- post.path %>" class="sidebar-link<%- page.title === post.title ? ' current' : '' %>"><%- post.title %></a>
911
</li>
1012
<% }) %>
1113
<li style="margin:10px 0 3px">

themes/vue/source/css/_common.styl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ a.button
9999
position relative
100100
z-index 1
101101
padding 0 60px 30px
102+
overflow-x hidden
102103

103104
#ad
104105
width 130px

themes/vue/source/css/page.styl

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ $header-height = 40px
88
background-color #fff
99
box-shadow 0 0 4px rgba(0,0,0,.25)
1010
padding 25px 60px
11+
position relative
12+
z-index 0
1113

1214
#nav
1315
list-style-type none
@@ -23,6 +25,7 @@ $header-height = 40px
2325
li
2426
display inline-block
2527
margin 0 .6em
28+
2629
.nav-link
2730
padding-bottom 3px
2831
&:hover, &.current
@@ -40,8 +43,37 @@ $header-height = 40px
4043
width $header-height
4144
height $header-height
4245

46+
#mobile-bar
47+
position fixed
48+
top 0
49+
left 0
50+
width 100%
51+
height 40px
52+
background-color #fff
53+
z-index 9
54+
display none
55+
box-shadow 0 0 4px rgba(0,0,0,.25)
56+
.menu-button
57+
position absolute
58+
width 24px
59+
height 24px
60+
top 8px
61+
left 12px
62+
background url(../images/menu.png) center center no-repeat
63+
background-size 24px
64+
.logo
65+
position absolute
66+
width 30px
67+
height 30px
68+
background url(../images/logo.png) center center no-repeat
69+
top 5px
70+
left 50%
71+
margin-left -15px
72+
background-size 30px
73+
4374
.sidebar
4475
position absolute
76+
z-index 10
4577
top 0
4678
left 60px
4779
bottom 0
@@ -50,22 +82,16 @@ $header-height = 40px
5082
width 260px
5183
margin-right 20px
5284
-webkit-overflow-scrolling touch
53-
.toggle
54-
font-size 1.5em
55-
cursor pointer
56-
display none
57-
color $light
5885
h2
5986
margin-top .2em
6087
ul
6188
list-style-type none
6289
margin 0
63-
padding-left 1em
6490
line-height 1.8em
65-
.menu-root
6691
padding-left 0
6792
.menu-sub
6893
font-size .85em
94+
padding-left 1em
6995
.sidebar-link
7096
color $light
7197
&.current
@@ -77,6 +103,9 @@ $header-height = 40px
77103
&.active
78104
font-weight bold
79105
color $green
106+
.main-menu
107+
margin-bottom 20px
108+
display none
80109

81110
.content
82111
padding 2.2em 0
@@ -162,7 +191,7 @@ $header-height = 40px
162191

163192
@media screen and (max-width: 1200px)
164193
#ad
165-
z-index 9
194+
z-index 8
166195
position relative
167196
width auto
168197
height 100px
@@ -186,41 +215,33 @@ $header-height = 40px
186215
-webkit-text-size-adjust none
187216
font-size 14px
188217
#header
189-
text-align center
190-
padding 18px 0
191-
#nav
192-
position static
193-
margin-top .4em
194-
height 55px
195-
line-height 2.2em
196-
.break
197-
display inline-block
218+
display none
219+
#logo
220+
display none
198221
.nav-link
199222
padding-bottom 1px
200223
&:hover, &.current
201224
border-bottom 2px solid $green
202-
#logo
225+
#mobile-bar
203226
display block
204-
img
205-
margin-right 0
206-
span
207-
display none
208227
#main
209-
padding 0 1.4em
228+
padding 2em 1.4em 0
210229
.highlight pre
211230
padding 1.2em 1em
212231
.sidebar
213232
position fixed
214-
background-color #fff
215-
z-index 10
233+
background-color #f9f9f9
216234
height 100%
217235
top 0
218236
left 0
219-
padding 20px 30px
237+
padding 60px 30px 20px
220238
box-shadow 0 0 10px rgba(0,0,0,.2)
221239
box-sizing border-box
240+
transition transform .4s cubic-bezier(0.4, 0, 0, 1), -webkit-transform .4s cubic-bezier(0.4, 0, 0, 1)
222241
transform translate(-110%,0)
223242
-webkit-transform translate(-110%,0)
243+
.main-menu
244+
display block
224245
&.open
225246
transform translate(0,0)
226247
-webkit-transform translate(0,0)

themes/vue/source/images/menu.png

193 Bytes
Loading

themes/vue/source/js/common.js

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,81 @@
66
var body = document.body
77
var header = document.getElementById('header')
88
var menu = document.querySelector('.sidebar')
9-
var menuToggle = document.querySelector('.sidebar .toggle')
109
var content = document.querySelector('.content')
10+
var mobileBar = document.getElementById('mobile-bar')
1111

12-
if (!menu) return
13-
14-
menuToggle.addEventListener('click', function () {
12+
var menuButton = mobileBar.querySelector('.menu-button')
13+
menuButton.addEventListener('click', function () {
1514
menu.classList.toggle('open')
1615
})
1716

17+
body.addEventListener('click', function (e) {
18+
if (e.target !== menuButton && !menu.contains(e.target)) {
19+
menu.classList.remove('open')
20+
}
21+
})
22+
1823
// build sidebar
19-
var allLinks = []
2024
var currentPageAnchor = menu.querySelector('.sidebar-link.current')
21-
var sectionContainer = document.createElement('ul')
22-
sectionContainer.className = 'menu-sub'
23-
currentPageAnchor.parentNode.appendChild(sectionContainer)
24-
var h2s = content.querySelectorAll('h2')
25-
if (h2s.length) {
26-
each.call(h2s, function (h) {
27-
sectionContainer.appendChild(makeLink(h))
28-
var h3s = collectH3s(h)
29-
allLinks.push(h)
30-
allLinks.push.apply(allLinks, h3s)
31-
if (h3s.length) {
32-
sectionContainer.appendChild(makeSubLinks(h3s))
25+
if (currentPageAnchor) {
26+
var allLinks = []
27+
var sectionContainer = document.createElement('ul')
28+
sectionContainer.className = 'menu-sub'
29+
currentPageAnchor.parentNode.appendChild(sectionContainer)
30+
var h2s = content.querySelectorAll('h2')
31+
if (h2s.length) {
32+
each.call(h2s, function (h) {
33+
sectionContainer.appendChild(makeLink(h))
34+
var h3s = collectH3s(h)
35+
allLinks.push(h)
36+
allLinks.push.apply(allLinks, h3s)
37+
if (h3s.length) {
38+
sectionContainer.appendChild(makeSubLinks(h3s))
39+
}
40+
})
41+
} else {
42+
h2s = content.querySelectorAll('h3')
43+
each.call(h2s, function (h) {
44+
sectionContainer.appendChild(makeLink(h))
45+
allLinks.push(h)
46+
})
47+
}
48+
49+
var animating = false
50+
sectionContainer.addEventListener('click', function (e) {
51+
e.preventDefault()
52+
if (e.target.classList.contains('section-link')) {
53+
menu.classList.remove('open')
54+
setActive(e.target)
55+
animating = true
56+
setTimeout(function () {
57+
animating = false
58+
}, 400)
3359
}
34-
})
35-
} else {
36-
h2s = content.querySelectorAll('h3')
37-
each.call(h2s, function (h) {
38-
sectionContainer.appendChild(makeLink(h))
39-
allLinks.push(h)
60+
}, true)
61+
62+
// init smooth scroll
63+
smoothScroll.init({
64+
speed: 400,
65+
offset: window.innerWidth > 720
66+
? 40
67+
: 58
4068
})
4169
}
4270

43-
// init smooth scroll
44-
smoothScroll.init({
45-
speed: 400
46-
})
47-
48-
var animating = false
49-
sectionContainer.addEventListener('click', function (e) {
50-
e.preventDefault()
51-
if (e.target.classList.contains('section-link')) {
52-
setActive(e.target)
53-
animating = true
54-
setTimeout(function () {
55-
animating = false
56-
}, 400)
57-
}
58-
}, true)
59-
6071
// listen for scroll event to do positioning & highlights
6172
window.addEventListener('scroll', updateSidebar)
6273
window.addEventListener('resize', updateSidebar)
6374

6475
function updateSidebar () {
6576
var top = doc && doc.scrollTop || body.scrollTop
66-
if (top > header.offsetHeight) {
77+
var headerHeight = header.offsetHeight
78+
if (top > headerHeight) {
6779
main.className = 'fix-sidebar'
6880
} else {
6981
main.className = ''
7082
}
71-
if (animating) return
83+
if (animating || !allLinks) return
7284
var last
7385
for (var i = 0; i < allLinks.length; i++) {
7486
var link = allLinks[i]

0 commit comments

Comments
 (0)