-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.html
128 lines (101 loc) · 4.5 KB
/
index.html
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
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Sticky Events — Ryan Walters</title>
<meta name="description" content="Events for position: sticky">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://bootswatch.com/4/flatly/bootstrap.min.css">
<link rel="stylesheet" href="demo.css">
<style>
:root {
--navbar-height: 70px;
}
.container {
margin-top: var(--navbar-height);
padding-top: 50px;
}
.container > div {
height: 500px;
}
.sticky-events, .dynamic-sticky {
color: white;
top: var(--navbar-height);
}
</style>
</head>
<body>
<div class="navbar navbar-dark bg-primary navbar-expand-lg fixed-top">
<a href="" class="navbar-brand">Sticky Events</a>
<form class="form-inline d-none d-sm-block">
<button id="toggle-button" type="button" class="btn btn-toggle active" data-toggle="button" aria-pressed="false" autocomplete="off">
<div class="handle"></div>
</button>
</form>
<ul class="navbar-nav ml-auto">
<li><a href="https://github.com/ryanwalters/sticky-events">View on Github</a></li>
</ul>
</div>
<div class="container" style="padding-bottom: 100px;">
<h1><a href="https://github.com/ryanwalters/sticky-events" target="_blank">Sticky Events</a></h1>
<p class="lead">Events for <code>position: sticky</code>.</p>
<div>
<div class="navbar bg-primary sticky-events">Sticky Heading 1</div>
</div>
<div>
<div class="navbar bg-primary sticky-events">Sticky Heading 2</div>
</div>
<div style="height: 200px;">
<div class="navbar bg-primary sticky-events">Sticky Heading 3</div>
</div>
<hr class="mt-5">
<h3 class="mt-3">Dynamically added stickies</h3>
<p class="lead">Easily add more sticky elements later with <code>addStickies</code>.</p>
<button id="add-sticky-button" class="btn btn-primary mb-5">Add Sticky</button>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=IntersectionObserver,IntersectionObserverEntry"></script>
<script type="module">
import StickyEvents from "./sticky-events.js";
const stickyEvents = new StickyEvents();
// Add listeners to all `.sticky-events` elements on the page
const { stickyElements } = stickyEvents;
stickyElements.forEach((sticky) => {
sticky.addEventListener(StickyEvents.CHANGE, (event) => {
sticky.classList.toggle('bg-dark', event.detail.isSticky);
});
sticky.addEventListener(StickyEvents.STUCK, ({ detail: { position }}) => {
console.log(sticky.textContent, 'stuck from', position);
});
sticky.addEventListener(StickyEvents.UNSTUCK, ({ detail: { position }}) => {
console.log(sticky.textContent, 'unstuck from', position);
});
});
// Dynamically add stickies
document.querySelector('#add-sticky-button').addEventListener('click', (e) => {
// Create your elements however you want
// Quick and dirty here, skip down to the next comment for the important bit
const wrapper = document.createElement('div');
const sticky = document.createElement('div');
sticky.classList.add('navbar', 'bg-primary', 'sticky-events');
sticky.innerText = 'Dynamically Added Heading';
wrapper.appendChild(sticky);
e.currentTarget.parentNode.appendChild(wrapper);
sticky.addEventListener(StickyEvents.CHANGE, ({ detail: { isSticky, position }}) => {
sticky.classList.toggle('bg-dark', isSticky);
console.log(sticky.textContent, isSticky ? 'stuck' : 'unstuck', 'from', position);
});
// Once you have your element, call `addSticky`
// If you have more than one sticky, you can use `addStickies` which expects an array-like argument (NodeList, Array)
stickyEvents.addSticky(sticky);
});
// You can also disable all of your stickies, reverting them to their original state
// If you don't want them to revert to their original state, pass "false" to disableEvents
document.querySelector('.btn').addEventListener('click', (e) => {
e.currentTarget.classList.contains('active') ? stickyEvents.disableEvents() : stickyEvents.enableEvents();
});
</script>
</body>
</html>