JavaScript + CSS M3L2
JavaScript + CSS M3L2
LESSON 2
Methodological
Скачать Guidelines
методичку
MODULE 3. LESSON 2
New topic:
Timers and Functions
Timer
We found out that it's possible to delay code execution for some
time. What data might the browser need to do that?
Timer
We found out that it's possible to delay code execution for some
time. What data might the browser need to do that?
Like this:
Like this:
1. alert().
2. console.log().
3. querySelector().
After all, JavaScript won't start looking for an object on the page
and editing it if we don't call the function we need, right?
function querySelector(selector){
// go over the tree
// select an element
// return the element
}
let number1 = 5
let number2 = 10
let result = number1 + number2
console.log(result)
Functions
Connecting the function declaration and its body:
function summa() {
let number1 = 5
let number2 = 10
let result = number1 + number2
console.log(result)
}
Functions
Connecting the function declaration and its body:
function summa() {
Done :)
let number1 = 5
let number2 = 10
let result = number1 + number2
console.log(result)
}
Functions
Now if you call the function by its name:
summa()
the result of adding two numbers will be output to the console.
But there is a flaw: the function code displays the sum of two
exact numbers: 5 and 10. This means that when the function is
called, the number 15 will always be displayed in the console.
document.querySelector(<selector>)
function remind() {
alert('20% discount on one-way ticket!')
}
Task
Adding setTimeout():
function remind() {
alert('20% discount on one-way ticket!')
}
setTimeout(remind, 2*60*1000)
Task
Detail
function remind() {
alert('20% discount on one-way ticket!')
}
setTimeout( remind , 2*60*1000)
We did it!
Task
The text of the notification may be different each time. How can
this be programmed?
Task
The text of the notification may be different each time. How can
this be programmed?
You can write a function so that it accepts a message as an
argument:
function remind(message) {
alert(message)
}
setTimeout(remind, 2*60*1000)
Task
The text of the notification may be different each time. How can
this be programmed?
You can write a function so that it accepts a message as an
argument:
function remind(message) {
alert(message)
}
setTimeout(function(){remind('Message') }, 2*60*1000)
Anonymous functions
In such cases, anonymous functions are used. They don't
need to come up with a name because the whole construct is
passed to setTimeout:
We pass the function
that will be called
at the right moment.
function remind(message) {
alert(message)
}
setTimeout( function(){remind('Message') }, 2*60*1000)
Anonymous functions
In such cases, anonymous functions are used. They don't
need to come up with a name because the whole structure is
passed to setTimeout:
And then the remind function
will be called from it
with the argument passed.
function remind(message) {
alert(message)
}
setTimeout( function(){remind('Message') }, 2*60*1000)
Bonus
Interesting facts about the setTimeout() command. What do you
think, which of the two functions will be executed first?
function first() {
console.log('The first function')
}
function second() {
console.log('The second function')
}
setTimeout(first, 50000)
setTimeout(second, 100)
Bonus
The second function will be executed first.
10:2
1
MODULE 3. LESSON 1
New topic.
Event handling
Discussion
Let's look at this page again:
Discussion
Let's formulate what's happening:
1. There is a button on the page.
2. The user clicks on the button.
3. Page styles change.
Let's go!!
Event handling
Let's start with creating a function. What properties should we
change?
Event handling
Let's start with creating a function:
function switchTheme() {
let header = document.querySelector( 'header')
header.style.background = 'url(dark4.jpg)'
header.style.backgroundSize = 'cover'
}
Event handling
Now let's get to the button:
function switchTheme() {
let header = document.querySelector( 'header')
header.style.background = 'url(dark4.jpg)'
header.style.backgroundSize = 'cover'
}
let btn = document.querySelector('.btn-change-img')
Event handling
The last step is to program the response:
function switchTheme() {
let header = document.querySelector( 'header')
header.style.background = 'url(dark4.jpg)'
header.style.backgroundSize = 'cover'
}
let btn = document.querySelector('.btn-change-img')
btn.addEventListener('click', switchTheme)
Event handling
Considering the last line:
Referring to
the button
Referring to
the button
<div class="submit-btn-row">
<div class="submit-btn submit-btn-calc">
Price
</div>
<div class="submit-btn submit-btn-send">
Send
</div>
</div>
mouseover and mouseout
The sequence of actions will be the same as for handling the click
event: we write a function, get a button, and add event handling.
The only difference is the event to respond to:
function make_transparent() {
send_btn.style.backgroundColor = 'transparent'
}
letsend_btn = document.querySelector('.submit-btn-send')
send_btn.addEventListener('mouseover', make_transparent)
mouseover and mouseout
function make_transparent() {
send_btn.style.backgroundColor = 'transparent'
}
letsend_btn = document.querySelector('.submit-btn-send')
send_btn.addEventListener( 'mouseover' , make_transparent)
function make_colorful() {
send_btn.style.backgroundColor = '#C2AB99'
}
letsend_btn = document.querySelector('.submit-btn-send')
send_btn.addEventListener( 'mouseout' , make_colorful)
function make_colorful() {
send_btn.style.backgroundColor = '#C2AB99'
}
send_btn.addEventListener('mouseover', make_transparent)
send_btn.addEventListener('mouseout', make_colorful)
mouseover and mouseout
And a wonderful result!
Event handling
The last one left!
● click ― clicking on an element
● mouseover ― moving the mouse to a block
● mouseout ― moving the mouse outside a block
● mousemove ― mouse movement
mousemove
The mousemove event is triggered if the user moves the cursor
within a page or block. The only difference is that this event is
almost always triggered because the cursor is almost always in
motion.
This event will be triggered regularly, with a frequency
determined by the browser.
mousemove