Page.js
2.05 KB
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
import React, { Component } from 'react';
import Header from './Header';
import Footer from './Footer';
import About from './About';
import Skills from './Skills';
import Vitae from './Vitae';
import Links from './Links';
import './Page.css';
class Page extends Component {
constructor(props) {
super(props);
this.handleScroll = this.handleScroll.bind(this);
this.state = {
active: 0,
sections: [],
sections_data: [
{ title: 'About me', },
{ title: 'Skills', },
{ title: 'Vitae', },
{ title: 'Links', },
],
}
}
componentDidMount() {
const sections = document.getElementsByClassName('section');
this.setState({
sections: sections,
});
window.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll);
}
handleScroll(event) {
let active = null;
for (var i=0; i<this.state.sections.length; i++) {
if (document.documentElement.scrollTop >=
this.state.sections[i].offsetTop) {
active = i;
}
else {
break;
}
}
this.setState({
active: active,
});
}
handleShow(section) {
this.refs[section].scrollIntoView({
block: 'start',
behavior: 'smooth',
});
}
render() {
return (
<div id="page">
<Header
sections={this.state.sections_data}
active={this.state.active}
onClick={(section) => this.handleShow.bind(this, section)}
/>
<div id="section1" ref="1" className="section">
<About />
</div>
<div id="section2" ref="2" className="section">
<Skills
onClick={(section) => this.handleShow.bind(this, section)}
/>
</div>
<div id="section3" ref="3" className="section">
<Vitae />
</div>
<div id="section4" ref="4" className="section">
<Links />
</div>
<Footer />
</div>
);
}
}
export default Page;
// vim: set ts=2 sw=2 et: