User:Zuhui/SI26/Storybook/Code to work: Difference between revisions
No edit summary |
|||
Line 231: | Line 231: | ||
=javascript part 2= | =javascript part 2= | ||
=resource and reference= | |||
===Javascript=== | |||
* [https://developer.mozilla.org/en-US/docs/Web/API/Element/classList Classlist-add,remove,replace,toggle] | |||
* [https://www.w3schools.com/js/js_random.asp Math.random()] | |||
* [https://www.w3schools.com/jsref/jsref_pop.asp pop()] | |||
* [https://www.w3schools.com/jsref/jsref_push.asp push()] | |||
<br> | |||
<br> | |||
===Targeting=== | |||
* [https://www.w3schools.com/tags/att_data-.asp html data-* attribute] | |||
* [https://www.w3schools.com/cssref/css_selectors.php css selectors] | |||
* [https://www.w3schools.com/cssref/css_ref_pseudo_classes.php css pseudo classes] | |||
* [https://www.w3schools.com/cssref/css_ref_pseudo_elements.php css pseudo elements] | |||
<br> | |||
<br> |
Revision as of 13:06, 13 February 2025
drawing html/css structure
the main prompter / the words inside
html
<p id="sentence">
<span class="do">do</span>
<span class="tigers">tigers</span>
<span class="melt">melt</span>
<span class="like">like</span>
<span class="butter">butter</span>
<span class="in">in</span>
<span class="the">the</span>
<span class="forest">forest</span>
<span class="?">?</span>
</p>
css
#sentence {
position: fixed;
margin: 0;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
z-index: 999999999;
text-align: center;
font-size: 29px;
font-weight: bold;
font-family: Arial, sans-serif;
font-weight: bold;
color:rgb(0, 0, 0);
white-space: nowrap;
}
stage
html
<div id="stage">
//all the children divs that goes onto the stage//
</div>
css
#stage {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
margin: 0;
z-index: -99999;
}
what goes onto the stage
html
using both data-* selector and class to target better in css and javascript
<div data-space="forest" class="forest"> //or, data-object="forest" for the occasion when forest becomes an object//
<div data-object="celestial" class="celestial"></div>
<div data-object="tigers" class="tigers"></div>
<div data-object="butter" class="butter"></div>
<div data-effect="melt" class="melt"></div>
</div>
css data-*selector
[data-space="forest"] {
top: 0;
left: 0;
width: 100vw;
height: 100vh;
margin: 0;
z-index: -99998;
background-color: green;
}
[data-object="forest"] {
width: 300px;
height: 300px;
margin: auto;
/*margin-top: 50%;*/
background-color: green;
}
[data-object="celestial"]{
}
[data-object="tiger"]{
}
[data-object="butter"]{
}
[data-effect="melt"]{
}
hide
css .hide[data-*] for javascript to target
.hide[data-space]{
background-color: transparent;
background-image: none;
}
.hide[data-object]{
display:none;
}
.hide[data-effect]{
}
javascript part 1
first part of javascript should allow the words in the sentence to remove and restore themselves in correspondence to the shifting browser width.
- 1. I need two repositories(variables) to store the words, one repository that is shown on the screen, and the other repository to store the removed words from the screen
- ↳ store all
span
element in a variable (I'm usingspan
underp
tag only for the words).
- ↳ store all
const words = document.querySelectorAll("span");
- ↳ apparently, "The
querySelectorAll()
method returns a static NodeList." it may look like an array but it's not an array, can't use array methods if I keep it like this.
- ↳ apparently, "The
Although NodeList is not an Array, it is possible to iterate over it with
forEach()
. It can also be converted to a real Array usingArray.from().
const words = Array.from(document.querySelectorAll("span"));
- 2. make another variable to store the (randomly)removed words.
- ↳ so that I can bring them back(randomly) whenever the screen size increases.
let removedWords = [];
- 3. make javascript aware of the browser screen size changes, so the words respond accodringly.
- ↳ the words should disappear when the screen size decreases and reappear when it increases.
- ↳ look up
window.innerWidth
, I'm only going to decrease and increase the width.- ↳ need a variable to save the innerWidth property
- ↳ look up
"resize"
function inwindow.addEventListner()
- ↳ need both prevWidth and currWidth variables for javascript to calculate screen size difference(widthChange).
- ↳ also look up
Math.-
methods. need to calculate the width difference.- ↳ to math method to function, I need the innerWidth(prevWidth, currWidth) output into text.
- ↳ look up
- ↳ the words should disappear when the screen size decreases and reappear when it increases.
window.innerWidth
(checkout Demo code on the page, super nice example with "resize"eventlistener)
"innerWidth returns the interior width of the window in pixels (that is, the width of the window's layout viewport)."
element.clientWidth, (interesting)
window.addEventListener()
(look for more syntax examples especially the ones using true/fase or if/else)
"resize" example
mdn onresize (much simpler)
mdn textContent
w3c textContent
Math.*
Math.absolute
("The math. abs() method accepts a single integer data type parameter
and returns the number's absolute value, or positive value, without using the negative sign.")
three ways to resize event
window.onresize = (peekaBoo) => {
};
/////////////////////or//////////////////////
window.addEventListener("resize", () => {
})
/////////////////////or//////////////////////
window.addEventListener("resize", peekaBoo);
function peekaBoo () {
}
- 4. make two different functions, one for removing random words as the screen size decreases, and the other for bringing back random words as the screen size increases. it has to work with resize window.eventListener.
- ↳ when removing words, cap the minimal amount of word to at least 1, so the screen doesn't remove all the words.
javascript part 2
resource and reference
Javascript
Targeting