-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproject.js
More file actions
95 lines (84 loc) · 4.18 KB
/
Copy pathproject.js
File metadata and controls
95 lines (84 loc) · 4.18 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
91
92
93
94
95
// One trick to organizing code is to put related functions inside of an object,
// so they are under the same "namespace". This helps maek readable code that is
// easier to maintain in the long term.
// TODO: replace use of `document.getElementByXXX` with `d3.select` so it is more readable
/* globals scrollama */
const Project = {};
Project.scrolling = {
// these hold references to helpers and rendered page elements (filled in by `initialize`)
scroller: undefined, // an instance of scrollama
steps: undefined, // an array of all the step elements
// a list of the backdrop images, ordered so they match the `step` elements on the page
backdrops: [
{ 'src': 'https://cdn.glitch.global/60a947a3-a0d4-473b-a51a-ef7120e2f598/webcoding.jpeg?v=1673897542123',
'credit': 'https://zapier.com/blog/learn-html-css/',
'type': 'image',
},
{ 'src': 'https://cdn.glitch.global/60a947a3-a0d4-473b-a51a-ef7120e2f598/puppies.jpg?v=1673897599766',
'credit': 'https://www.wisdompanel.com/en-us/blog/sibling-genetics-in-dogs'
},
{ 'src': 'https://cdn.glitch.global/60a947a3-a0d4-473b-a51a-ef7120e2f598/kitten-vs-puppy.jpeg?v=1673897648888',
'credit': 'https://www.marketwatch.com/story/owning-a-cat-vs-owning-a-dog-which-pet-makes-better-financial-sense-11649445375',
},
],
// set up the webpage to scroll
initialize: () => {
// grab the elements on the page that are related to the scrolling
const scrollWrapper = document.getElementById("scrolly");
Project.scrolling.figure = scrollWrapper.getElementsByTagName("figure")[0];
const article = scrollWrapper.getElementsByTagName('article')[0];
Project.scrolling.steps = Array.from(article.getElementsByClassName("step")); // convert from HTMLCollection to Array for ease of use later
// intialize the scrollama helper
Project.scrolling.scroller = scrollama();
Project.scrolling.scroller
.setup({
step: "#scrolly article .step",
offset: 0.9,
debug: false
})
.onStepEnter(Project.scrolling.handleStepEnter)
.onStepExit(Project.scrolling.handleStepExit);
// setup the default view to be the right size and include first step
Project.scrolling.handleResize();
Project.scrolling.setBackdropImage(0); // remember: 0 means the first item in an array
},
// call this to switch the background image
setBackdropImage: (index) => {
// grab the info for thi step
// if this step's type is image
//. then swap the image
// if this step's type if video
//. hide the image
//. set the video src
//. show the video
//. play the video
const image = Project.scrolling.figure.getElementsByTagName("img")[0];
image.src = Project.scrolling.backdrops[index].src;
//image.classList.add = 'fade-in';
// TODO: make this caption text a link
document.getElementsByTagName("figcaption")[0].innerHTML = Project.scrolling.backdrops[index].credit;
},
// called by scrollama when the step is being entered
handleStepEnter: (stepInfo) => { // stepInfo = { element, directihandle, index }
// console.log(`Switched to step ${stepInfo.index}`);
// TODO: add an `is-active` class on the step that we switched to (and remove from all others)
// and switch the background image to match the step content
Project.scrolling.setBackdropImage(stepInfo.index);
},
// called by scrollama when moving out of a step
handleStepExit: (stepInfo) => {
// we don't make any transitions when a step scrolls out of view
},
// called to get content to be the right size to fit the device
handleResize: () => {
const stepH = Math.floor(window.innerHeight * 1); // update step heights
Project.scrolling.steps.forEach(step => step.style.height = stepH + "px")
const figureWidth = window.innerWidth;
const figureHeight = window.innerHeight;
Project.scrolling.figure.style.width = figureWidth + "px";
Project.scrolling.figure.style.height = figureHeight + "px";
Project.scrolling.figure.style.top = "0px";
Project.scrolling.figure.getElementsByClassName("wrapper")[0].style.height = figureHeight + "px";
Project.scrolling.scroller.resize(); // tell scrollama to update new element dimensions
},
};