SlideShare uma empresa Scribd logo
1 de 22
Baixar para ler offline
10 JavaScript Projects - Laurence
Svekis
requestAnimationFrame and
cancelAnimationFrame Code Sample
<!doctype html><html>
<head>
<title>Questions and Answers JavaScript</title>
</head>
<body>
<div class="top">
<div class="nested1">Nested 1</div>
<div class="nested2">Nested 2</div>
<div class="nested3">Nested 3</div>
</div>
<script>
let tog = true;
const div = document.createElement('div');
div.textContent = "hello";
div.style.color = "red";
div.style.position = "absolute";
div.style.left = '50px';
div.x = 50;
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
div.addEventListener('click', stopper);
const topEle = document.querySelector('.top');
topEle.append(div);
let myAni = requestAnimationFrame(mover);
function stopper() {
if (tog) {
cancelAnimationFrame(myAni);
tog = false;
}
else {
tog = true;
myAni = requestAnimationFrame(mover);
}
}
function mover() {
div.x = div.x + 1;
div.style.left = div.x + 'px';
myAni = requestAnimationFrame(mover);
}
</script>
</body>
</html>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
JavaScript Switch Statement
<!doctype html>
<html>
<head>
<title>Questions and Answers JavaScript</title>
</head>
<body>
<div class="top">
<div class="nested1">Nested 1</div>
<div class="nested2">Nested 2</div>
<div class="nested3">Nested 3</div>
</div>
<div class="message">What time is it</div>
<input type="text">
<button>Click</button>
<script>
const btn = document.querySelector('button');
const answer = document.querySelector('input');
const message = document.querySelector('.message');
btn.addEventListener('click', function () {
console.log(answer.value);
//let ans = Number(answer.value);
let ans = parseInt(answer.value);
//console.log(typeof(answer.value));
console.log(typeof (ans));
console.log(ans);
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
if (!Number(ans)) {
console.log('not a number');
}
else {
console.log('Okay');
message.textContent = checkTimeofDay(ans);
}
})
outputToday();
function outputToday() {
const today = new Date().getDay();
let dayName = 'Unknown';
let weekStatus = 'Unknown';
switch (today) {
case 0:
dayName = "Sunday";
break;
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
}
switch (dayName) {
case "Thursday":
case "Friday":
case "Saturday":
weekStatus = "end of Week";
break;
default:
weekStatus = "Start of Week";
}
console.log(today);
message.textContent = `Today is a ${dayName} its the
${weekStatus}`;
}
function checkTimeofDay(num) {
switch (num < 12) {
case true:
return 'Good Morning';
break;
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
case false:
return 'Good Afternoon';
break;
default:
return 'something went wrong'
}
}
</script>
</body>
</html>
Example of using Continue and Break in
For loop and While Loop
<!doctype html>
<html>
<head>
<title>Questions and Answers JavaScript</title>
</head>
<body>
<div class="top">
<div class="nested1">Nested 1</div>
<div class="nested2">Nested 2</div>
<div class="nested3">Nested 3</div>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
</div>
<div class="message">What time is it</div>
<input type="text">
<button>Click</button>
<script>
for (let i = 0; i < 10; i++) {
if (i === 3) {
continue;
}
if (i === 8) {
break;
}
console.log(i);
}
let x = 0;
while (x < 10) {
//if(x===3){continue;}
if (x === 8) {
break;
}
//console.log(x);
x++;
}
//console.log(x);
</script>
</body>
</html>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
Keyboard Event Listeners - Dynamically
Add Page Elements input and divs
<!doctype html><html>
<head>
<title>Questions and Answers JavaScript</title>
</head>
<body>
<script>
const output = document.createElement('div');
const message = document.createElement('div');
const btn = document.createElement('button');
document.body.append(output);
output.append(message);
output.append(btn);
btn.textContent = "Click to add input";
btn.style.backgroundColor = 'red';
btn.style.color = 'white';
btn.style.padding = '10px';
btn.addEventListener('click', maker)
function maker() {
const tempDiv = document.createElement('div');
const newInput = document.createElement('input');
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
output.append(tempDiv);
tempDiv.append(newInput);
newInput.value = 'test';
newInput.hiddenValue =
Math.random().toString(16).substr(-6);
newInput.style.backgroundColor = '#' +
newInput.hiddenValue;
newInput.focus();
newInput.addEventListener('keyup', log);
newInput.addEventListener('keypress', log);
newInput.addEventListener('keydown', function (e) {
console.log(e.keyCode);
if (e.keyCode == 13) {
message.innerHTML += `<div
style="background:#${newInput.hiddenValue}">${newInput.value}</d
iv>`;
}
});
}
function log(event) {
console.log(event);
}
</script>
</body>
</html>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
Create Page Elements add Dynamically
on the Page
<!doctype html><html>
<head>
<title>Questions and Answers JavaScript</title>
</head>
<body>
<script>
const btn = document.createElement('button');
const output = document.createElement('div');
const message = document.createElement('div');
btn.textContent = "Click Me!";
message.textContent = "Hello World";
document.body.append(output);
output.append(message);
output.append(btn);
btn.addEventListener('click', () => {
const today = new Date();
message.textContent = `${today.getHours()}
${today.getMinutes()} ${today.getSeconds()}`;
})
</script>
</body>
</html>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
Pure JavaScript Dice - Create Elements
and Build HTML for Dice
<!doctype html><html>
<head>
<title>Questions and Answers JavaScript</title>
</head>
<body>
<script>
const diceView = [[5], [1, 9], [1, 5, 9], [1, 3, 7, 9],
[1, 3, 5, 7, 9], [1, 3, 4, 6, 7, 9]];
const btn = document.createElement('button');
btn.textContent = "Roll Dice";
const playArea = document.createElement('div');
document.body.prepend(playArea);
playArea.append(btn);
const area1 = document.createElement('div');
const area2 = document.createElement('div');
const container = document.createElement('div');
playArea.append(container);
container.append(area1);
container.append(area2);
area1.textContent = "first Dice";
area2.textContent = "second Dice";
addBorders(area1);
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
addBorders(area2);
btn.addEventListener('click', () => {
rollValue();
console.log(area1.val);
console.log(area2.val);
})
function genDice(val) {
let html = '<div>';
let tempArr = diceView[val];
console.log(tempArr);
for (let x = 1; x < 10; x++) {
let tempVal = 'white';
if (tempArr.includes(x)) {
tempVal = 'black';
}
html += `<span
style="width:90px;display:inline-block;height:90px;border-radius
:20px;background-color:${tempVal};margin:2px;"></span>`;
}
html += '</div>';
return html;
}
function rollValue() {
area1.val = Math.floor(Math.random() * 6);
area2.val = Math.floor(Math.random() * 6);
area1.innerHTML = genDice(area1.val);
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
area2.innerHTML = genDice(area2.val);
}
function addBorders(el) {
el.style.border = '1px solid #ddd';
el.style.borderRadius = "10px";
el.style.padding = '10px';
el.style.fontSize = '1.5em';
el.style.width = '290px';
el.style.height = '290px';
el.style.margin = '10px';
el.style.backgroundColor = 'white';
//el.style.width = '40%';
el.style.float = 'left';
//el.style.height = el.offsetWidth+'px';
}
</script>
</body>
</html>
Create a JavaScript popup Modal
<!doctype html><!doctype html>
<html>
<head>
<title>Course</title>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
<style>
.modal {
position: fixed;
z-index: 5;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.3);
display: none;
}
.modal-body {
background-color: white;
margin: 20% auto;
padding: 20px;
border: 1px solid #333;
border-radius: 25px;
width: 70%;
min-height: 200px;
}
.close {
float: right;
color: red;
font-size: 2em;
font-weight: bold;
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
}
.close:hover {
color: black;
cursor: pointer;
}
</style>
</head>
<body>
<button class='modal1'>Open 1</button>
<button class='modal1'>Open 2</button>
<div class="modal" id="main">
<div class="modal-body"> <span class="close">&times;</span>
<div class="modal-text">Modal Text
<br> test </div>
</div>
</div>
<script>
const btns = document.querySelectorAll('.modal1');
const output = document.querySelector('.modal-text');
btns.forEach((btn) => {
btn.addEventListener('click', (e) => {
myModal.style.display = 'block';
console.log(e.target.textContent);
let val = e.target.textContent;
let html = "";
switch (val) {
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
case 'Open 1':
html = 'Number one is open <h1>ONE</h1>';
break;
case 'Open 2':
html = '<h1>TWO</h1>';
break;
default:
html = '<h1>ERROR</h1>';
}
output.innerHTML = html;
})
})
const closer = document.querySelector('.close');
const myModal = document.querySelector('#main');
closer.addEventListener('click', closeModal);
myModal.addEventListener('click', closeModal);
function closeModal() {
myModal.style.display = 'none';
}
</script>
</body>
</html>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
JavaScript Request Animation Frame
Simple Counter
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<h1>Hello World</h1>
<script>
const output = document.querySelector('h1');
output.textContent = 'Counter';
let reqVal = requestAnimationFrame(step);
let start;
function step(cnt) {
console.log(cnt);
if (start == undefined) {
start = cnt;
}
const val = Math.floor(cnt - start);
const str = String(val);
console.log(str[0]);
const mil = str.slice(1, 4);
console.log(mil);
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
console.log(val);
output.textContent = `${str[0]} : ${mil}`;
if (val < 5000) {
reqVal = requestAnimationFrame(step);
}
}
</script>
</body>
</html>
QuerySelector adding elements
dynamically to page use of NodeList
<!doctype html>
<html>
<head>
<title>Example querySelectorAll</title>
</head>
<body>
<ul></ul>
<input type="text" name="myInput" value="test">
<button>Click Me to add item</button>
<script>
const ul = document.querySelector('ul');
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
const li = document.querySelectorAll('li');
const myInput =
document.querySelector('input[name="myInput"]');
const btn = document.querySelector('button');
let x = 0;
let val = myInput.value;
btn.addEventListener('click', (e) => {
//console.log(e);
x++;
e.target.textContent = 'Clicked ' + x;
addListItem();
})
function addListItem() {
//console.log(myInput.value);
//console.log(val);
console.dir(ul);
console.log(ul.children.length);
console.log(ul.childElementCount);
const lis = document.querySelectorAll('li');
//console.log(lis.length);
if (myInput.value.length > 3 && lis.length < 5) {
const li = document.createElement('li');
li.textContent = myInput.value;
const val1 = ul.appendChild(li);
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
//console.log(val1);
}
}
</script>
</body>
</html>
Adding Event Listeners to All Matching
Elements on Page - Dynamically adding
<!doctype html>
<html>
<head>
<title>Example querySelectorAll Click</title>
<style>
.active {
color: blue;
}
</style>
</head>
<body>
<h1>Hello</h1>
<ul class="myList">
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
<script>
const ul = document.querySelector('ul.myList');
const lis = ul.querySelectorAll('li');
const btn = document.createElement('button');
let counter = lis.length;
btn.textContent = 'Click Me';
document.body.append(btn);
btn.addEventListener('click', (e) => {
counter++;
const li = document.createElement('li');
li.acter = 0;
li.textContent = `Value (${counter}) ${li.acter} - `;
li.addEventListener('click', updateItem);
ul.append(li);
})
lis.forEach((li) => {
console.log(li);
li.acter = 0;
li.addEventListener('click', updateItem);
})
function updateItem(e) {
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/
const ele = e.target;
console.dir(ele);
ele.acter++;
console.log(ele.acter);
let temp = ele.textContent;
ele.textContent = `${temp} ${ele.acter}`;
ele.classList.toggle('active');
console.log(ele.classList.contains('active'));
}
</script>
</body>
</html>
Code by Laurence Svekis - JavaScript Course https://basescripts.com/
Get the Full Modern Web Development Course at
https://www.udemy.com/course/modern-web-design/

Mais conteúdo relacionado

Mais procurados

Web Performance Tips
Web Performance TipsWeb Performance Tips
Web Performance Tips
Ravi Raj
 

Mais procurados (20)

JAVA SCRIPT
JAVA SCRIPTJAVA SCRIPT
JAVA SCRIPT
 
Java script
Java scriptJava script
Java script
 
Why and How to Use Virtual DOM
Why and How to Use Virtual DOMWhy and How to Use Virtual DOM
Why and How to Use Virtual DOM
 
Java script
Java scriptJava script
Java script
 
22 j query1
22 j query122 j query1
22 j query1
 
How to make Ajax work for you
How to make Ajax work for youHow to make Ajax work for you
How to make Ajax work for you
 
Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...
Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...
Develop High Performance Windows 8 Application with HTML5 and JavaScriptHigh ...
 
Web Projects: From Theory To Practice
Web Projects: From Theory To PracticeWeb Projects: From Theory To Practice
Web Projects: From Theory To Practice
 
Data presentation with dust js technologies backing linkedin
Data presentation with dust js   technologies backing linkedinData presentation with dust js   technologies backing linkedin
Data presentation with dust js technologies backing linkedin
 
Web Components
Web ComponentsWeb Components
Web Components
 
1 ppt-ajax with-j_query
1 ppt-ajax with-j_query1 ppt-ajax with-j_query
1 ppt-ajax with-j_query
 
Front End Development: The Important Parts
Front End Development: The Important PartsFront End Development: The Important Parts
Front End Development: The Important Parts
 
Difference between java script and jquery
Difference between java script and jqueryDifference between java script and jquery
Difference between java script and jquery
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012
 
Introduction to web components
Introduction to web componentsIntroduction to web components
Introduction to web components
 
Mdst 3559-02-10-jquery
Mdst 3559-02-10-jqueryMdst 3559-02-10-jquery
Mdst 3559-02-10-jquery
 
Web Performance Tips
Web Performance TipsWeb Performance Tips
Web Performance Tips
 
JavaScript and jQuery Basics
JavaScript and jQuery BasicsJavaScript and jQuery Basics
JavaScript and jQuery Basics
 
JavaScript and BOM events
JavaScript and BOM eventsJavaScript and BOM events
JavaScript and BOM events
 
Better Selenium Tests with Geb - Selenium Conf 2014
Better Selenium Tests with Geb - Selenium Conf 2014Better Selenium Tests with Geb - Selenium Conf 2014
Better Selenium Tests with Geb - Selenium Conf 2014
 

Semelhante a 10 java script projects full source code

Ajax Performance Tuning and Best Practices
Ajax Performance Tuning and Best PracticesAjax Performance Tuning and Best Practices
Ajax Performance Tuning and Best Practices
Doris Chen
 
Scalable vector ember
Scalable vector emberScalable vector ember
Scalable vector ember
Matthew Beale
 

Semelhante a 10 java script projects full source code (20)

Java script Learn Easy
Java script Learn Easy Java script Learn Easy
Java script Learn Easy
 
Ajax Performance Tuning and Best Practices
Ajax Performance Tuning and Best PracticesAjax Performance Tuning and Best Practices
Ajax Performance Tuning and Best Practices
 
Meta Programming with JavaScript
Meta Programming with JavaScriptMeta Programming with JavaScript
Meta Programming with JavaScript
 
20190118_NetadashiMeetup#8_React2019
20190118_NetadashiMeetup#8_React201920190118_NetadashiMeetup#8_React2019
20190118_NetadashiMeetup#8_React2019
 
HTML5
HTML5HTML5
HTML5
 
JavaScript front end performance optimizations
JavaScript front end performance optimizationsJavaScript front end performance optimizations
JavaScript front end performance optimizations
 
CSS framework By Palash
CSS framework By PalashCSS framework By Palash
CSS framework By Palash
 
Webpack packing it all
Webpack packing it allWebpack packing it all
Webpack packing it all
 
Packing it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to nowPacking it all: JavaScript module bundling from 2000 to now
Packing it all: JavaScript module bundling from 2000 to now
 
Coldfusion with Keith Diehl
Coldfusion with Keith DiehlColdfusion with Keith Diehl
Coldfusion with Keith Diehl
 
Please dont touch-3.6-jsday
Please dont touch-3.6-jsdayPlease dont touch-3.6-jsday
Please dont touch-3.6-jsday
 
Scalable vector ember
Scalable vector emberScalable vector ember
Scalable vector ember
 
Building iPhone Web Apps using "classic" Domino
Building iPhone Web Apps using "classic" DominoBuilding iPhone Web Apps using "classic" Domino
Building iPhone Web Apps using "classic" Domino
 
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development
 
What you need to know bout html5
What you need to know bout html5What you need to know bout html5
What you need to know bout html5
 
jQuery
jQueryjQuery
jQuery
 
Wordless, stop writing WordPress themes like it's 1998
Wordless, stop writing WordPress themes like it's 1998Wordless, stop writing WordPress themes like it's 1998
Wordless, stop writing WordPress themes like it's 1998
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
Node.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash CourseNode.js & Twitter Bootstrap Crash Course
Node.js & Twitter Bootstrap Crash Course
 

Mais de Laurence Svekis ✔

JavaScript Lessons 2023 V2
JavaScript Lessons 2023 V2JavaScript Lessons 2023 V2
JavaScript Lessons 2023 V2
Laurence Svekis ✔
 
Top 10 Linkedin Tips Guide 2023
Top 10 Linkedin Tips Guide 2023Top 10 Linkedin Tips Guide 2023
Top 10 Linkedin Tips Guide 2023
Laurence Svekis ✔
 
JavaScript Advanced - Useful methods to power up your code
JavaScript Advanced - Useful methods to power up your codeJavaScript Advanced - Useful methods to power up your code
JavaScript Advanced - Useful methods to power up your code
Laurence Svekis ✔
 
JavaScript Core fundamentals - Learn JavaScript Here
JavaScript Core fundamentals - Learn JavaScript HereJavaScript Core fundamentals - Learn JavaScript Here
JavaScript Core fundamentals - Learn JavaScript Here
Laurence Svekis ✔
 

Mais de Laurence Svekis ✔ (19)

Quiz JavaScript Objects Learn more about JavaScript
Quiz JavaScript Objects Learn more about JavaScriptQuiz JavaScript Objects Learn more about JavaScript
Quiz JavaScript Objects Learn more about JavaScript
 
JavaScript Lessons 2023 V2
JavaScript Lessons 2023 V2JavaScript Lessons 2023 V2
JavaScript Lessons 2023 V2
 
JavaScript Lessons 2023
JavaScript Lessons 2023JavaScript Lessons 2023
JavaScript Lessons 2023
 
Top 10 Linkedin Tips Guide 2023
Top 10 Linkedin Tips Guide 2023Top 10 Linkedin Tips Guide 2023
Top 10 Linkedin Tips Guide 2023
 
JavaScript Interview Questions 2023
JavaScript Interview Questions 2023JavaScript Interview Questions 2023
JavaScript Interview Questions 2023
 
Code examples javascript ebook
Code examples javascript ebookCode examples javascript ebook
Code examples javascript ebook
 
Brackets code editor guide
Brackets code editor guideBrackets code editor guide
Brackets code editor guide
 
Web hosting get start online
Web hosting get start onlineWeb hosting get start online
Web hosting get start online
 
JavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScriptJavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScript
 
Web hosting Free Hosting
Web hosting Free HostingWeb hosting Free Hosting
Web hosting Free Hosting
 
Web development resources brackets
Web development resources bracketsWeb development resources brackets
Web development resources brackets
 
Google Apps Script for Beginners- Amazing Things with Code
Google Apps Script for Beginners- Amazing Things with CodeGoogle Apps Script for Beginners- Amazing Things with Code
Google Apps Script for Beginners- Amazing Things with Code
 
Introduction to Node js for beginners + game project
Introduction to Node js for beginners + game projectIntroduction to Node js for beginners + game project
Introduction to Node js for beginners + game project
 
JavaScript Advanced - Useful methods to power up your code
JavaScript Advanced - Useful methods to power up your codeJavaScript Advanced - Useful methods to power up your code
JavaScript Advanced - Useful methods to power up your code
 
JavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScriptJavaScript Objects and OOP Programming with JavaScript
JavaScript Objects and OOP Programming with JavaScript
 
JavaScript Core fundamentals - Learn JavaScript Here
JavaScript Core fundamentals - Learn JavaScript HereJavaScript Core fundamentals - Learn JavaScript Here
JavaScript Core fundamentals - Learn JavaScript Here
 
Web Development Introduction to jQuery
Web Development Introduction to jQueryWeb Development Introduction to jQuery
Web Development Introduction to jQuery
 
WordPress for Entrepreneurs Management of your own website
WordPress for Entrepreneurs Management of your own websiteWordPress for Entrepreneurs Management of your own website
WordPress for Entrepreneurs Management of your own website
 
Getting to Know Bootstrap for Rapid Web Development
Getting to Know Bootstrap for Rapid Web DevelopmentGetting to Know Bootstrap for Rapid Web Development
Getting to Know Bootstrap for Rapid Web Development
 

Último

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Último (20)

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 

10 java script projects full source code

  • 1. 10 JavaScript Projects - Laurence Svekis requestAnimationFrame and cancelAnimationFrame Code Sample <!doctype html><html> <head> <title>Questions and Answers JavaScript</title> </head> <body> <div class="top"> <div class="nested1">Nested 1</div> <div class="nested2">Nested 2</div> <div class="nested3">Nested 3</div> </div> <script> let tog = true; const div = document.createElement('div'); div.textContent = "hello"; div.style.color = "red"; div.style.position = "absolute"; div.style.left = '50px'; div.x = 50; Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 2. div.addEventListener('click', stopper); const topEle = document.querySelector('.top'); topEle.append(div); let myAni = requestAnimationFrame(mover); function stopper() { if (tog) { cancelAnimationFrame(myAni); tog = false; } else { tog = true; myAni = requestAnimationFrame(mover); } } function mover() { div.x = div.x + 1; div.style.left = div.x + 'px'; myAni = requestAnimationFrame(mover); } </script> </body> </html> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 3. JavaScript Switch Statement <!doctype html> <html> <head> <title>Questions and Answers JavaScript</title> </head> <body> <div class="top"> <div class="nested1">Nested 1</div> <div class="nested2">Nested 2</div> <div class="nested3">Nested 3</div> </div> <div class="message">What time is it</div> <input type="text"> <button>Click</button> <script> const btn = document.querySelector('button'); const answer = document.querySelector('input'); const message = document.querySelector('.message'); btn.addEventListener('click', function () { console.log(answer.value); //let ans = Number(answer.value); let ans = parseInt(answer.value); //console.log(typeof(answer.value)); console.log(typeof (ans)); console.log(ans); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 4. if (!Number(ans)) { console.log('not a number'); } else { console.log('Okay'); message.textContent = checkTimeofDay(ans); } }) outputToday(); function outputToday() { const today = new Date().getDay(); let dayName = 'Unknown'; let weekStatus = 'Unknown'; switch (today) { case 0: dayName = "Sunday"; break; case 1: dayName = "Monday"; break; case 2: dayName = "Tuesday"; break; case 3: dayName = "Wednesday"; break; case 4: Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 5. dayName = "Thursday"; break; case 5: dayName = "Friday"; break; case 6: dayName = "Saturday"; break; } switch (dayName) { case "Thursday": case "Friday": case "Saturday": weekStatus = "end of Week"; break; default: weekStatus = "Start of Week"; } console.log(today); message.textContent = `Today is a ${dayName} its the ${weekStatus}`; } function checkTimeofDay(num) { switch (num < 12) { case true: return 'Good Morning'; break; Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 6. case false: return 'Good Afternoon'; break; default: return 'something went wrong' } } </script> </body> </html> Example of using Continue and Break in For loop and While Loop <!doctype html> <html> <head> <title>Questions and Answers JavaScript</title> </head> <body> <div class="top"> <div class="nested1">Nested 1</div> <div class="nested2">Nested 2</div> <div class="nested3">Nested 3</div> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 7. </div> <div class="message">What time is it</div> <input type="text"> <button>Click</button> <script> for (let i = 0; i < 10; i++) { if (i === 3) { continue; } if (i === 8) { break; } console.log(i); } let x = 0; while (x < 10) { //if(x===3){continue;} if (x === 8) { break; } //console.log(x); x++; } //console.log(x); </script> </body> </html> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 8. Keyboard Event Listeners - Dynamically Add Page Elements input and divs <!doctype html><html> <head> <title>Questions and Answers JavaScript</title> </head> <body> <script> const output = document.createElement('div'); const message = document.createElement('div'); const btn = document.createElement('button'); document.body.append(output); output.append(message); output.append(btn); btn.textContent = "Click to add input"; btn.style.backgroundColor = 'red'; btn.style.color = 'white'; btn.style.padding = '10px'; btn.addEventListener('click', maker) function maker() { const tempDiv = document.createElement('div'); const newInput = document.createElement('input'); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 9. output.append(tempDiv); tempDiv.append(newInput); newInput.value = 'test'; newInput.hiddenValue = Math.random().toString(16).substr(-6); newInput.style.backgroundColor = '#' + newInput.hiddenValue; newInput.focus(); newInput.addEventListener('keyup', log); newInput.addEventListener('keypress', log); newInput.addEventListener('keydown', function (e) { console.log(e.keyCode); if (e.keyCode == 13) { message.innerHTML += `<div style="background:#${newInput.hiddenValue}">${newInput.value}</d iv>`; } }); } function log(event) { console.log(event); } </script> </body> </html> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 10. Create Page Elements add Dynamically on the Page <!doctype html><html> <head> <title>Questions and Answers JavaScript</title> </head> <body> <script> const btn = document.createElement('button'); const output = document.createElement('div'); const message = document.createElement('div'); btn.textContent = "Click Me!"; message.textContent = "Hello World"; document.body.append(output); output.append(message); output.append(btn); btn.addEventListener('click', () => { const today = new Date(); message.textContent = `${today.getHours()} ${today.getMinutes()} ${today.getSeconds()}`; }) </script> </body> </html> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 11. Pure JavaScript Dice - Create Elements and Build HTML for Dice <!doctype html><html> <head> <title>Questions and Answers JavaScript</title> </head> <body> <script> const diceView = [[5], [1, 9], [1, 5, 9], [1, 3, 7, 9], [1, 3, 5, 7, 9], [1, 3, 4, 6, 7, 9]]; const btn = document.createElement('button'); btn.textContent = "Roll Dice"; const playArea = document.createElement('div'); document.body.prepend(playArea); playArea.append(btn); const area1 = document.createElement('div'); const area2 = document.createElement('div'); const container = document.createElement('div'); playArea.append(container); container.append(area1); container.append(area2); area1.textContent = "first Dice"; area2.textContent = "second Dice"; addBorders(area1); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 12. addBorders(area2); btn.addEventListener('click', () => { rollValue(); console.log(area1.val); console.log(area2.val); }) function genDice(val) { let html = '<div>'; let tempArr = diceView[val]; console.log(tempArr); for (let x = 1; x < 10; x++) { let tempVal = 'white'; if (tempArr.includes(x)) { tempVal = 'black'; } html += `<span style="width:90px;display:inline-block;height:90px;border-radius :20px;background-color:${tempVal};margin:2px;"></span>`; } html += '</div>'; return html; } function rollValue() { area1.val = Math.floor(Math.random() * 6); area2.val = Math.floor(Math.random() * 6); area1.innerHTML = genDice(area1.val); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 13. area2.innerHTML = genDice(area2.val); } function addBorders(el) { el.style.border = '1px solid #ddd'; el.style.borderRadius = "10px"; el.style.padding = '10px'; el.style.fontSize = '1.5em'; el.style.width = '290px'; el.style.height = '290px'; el.style.margin = '10px'; el.style.backgroundColor = 'white'; //el.style.width = '40%'; el.style.float = 'left'; //el.style.height = el.offsetWidth+'px'; } </script> </body> </html> Create a JavaScript popup Modal <!doctype html><!doctype html> <html> <head> <title>Course</title> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 14. <style> .modal { position: fixed; z-index: 5; left: 0; top: 0; width: 100%; height: 100%; background-color: rgb(0, 0, 0); background-color: rgba(0, 0, 0, 0.3); display: none; } .modal-body { background-color: white; margin: 20% auto; padding: 20px; border: 1px solid #333; border-radius: 25px; width: 70%; min-height: 200px; } .close { float: right; color: red; font-size: 2em; font-weight: bold; Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 15. } .close:hover { color: black; cursor: pointer; } </style> </head> <body> <button class='modal1'>Open 1</button> <button class='modal1'>Open 2</button> <div class="modal" id="main"> <div class="modal-body"> <span class="close">&times;</span> <div class="modal-text">Modal Text <br> test </div> </div> </div> <script> const btns = document.querySelectorAll('.modal1'); const output = document.querySelector('.modal-text'); btns.forEach((btn) => { btn.addEventListener('click', (e) => { myModal.style.display = 'block'; console.log(e.target.textContent); let val = e.target.textContent; let html = ""; switch (val) { Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 16. case 'Open 1': html = 'Number one is open <h1>ONE</h1>'; break; case 'Open 2': html = '<h1>TWO</h1>'; break; default: html = '<h1>ERROR</h1>'; } output.innerHTML = html; }) }) const closer = document.querySelector('.close'); const myModal = document.querySelector('#main'); closer.addEventListener('click', closeModal); myModal.addEventListener('click', closeModal); function closeModal() { myModal.style.display = 'none'; } </script> </body> </html> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 17. JavaScript Request Animation Frame Simple Counter <!DOCTYPE html> <html> <head> <title>test</title> </head> <body> <h1>Hello World</h1> <script> const output = document.querySelector('h1'); output.textContent = 'Counter'; let reqVal = requestAnimationFrame(step); let start; function step(cnt) { console.log(cnt); if (start == undefined) { start = cnt; } const val = Math.floor(cnt - start); const str = String(val); console.log(str[0]); const mil = str.slice(1, 4); console.log(mil); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 18. console.log(val); output.textContent = `${str[0]} : ${mil}`; if (val < 5000) { reqVal = requestAnimationFrame(step); } } </script> </body> </html> QuerySelector adding elements dynamically to page use of NodeList <!doctype html> <html> <head> <title>Example querySelectorAll</title> </head> <body> <ul></ul> <input type="text" name="myInput" value="test"> <button>Click Me to add item</button> <script> const ul = document.querySelector('ul'); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 19. const li = document.querySelectorAll('li'); const myInput = document.querySelector('input[name="myInput"]'); const btn = document.querySelector('button'); let x = 0; let val = myInput.value; btn.addEventListener('click', (e) => { //console.log(e); x++; e.target.textContent = 'Clicked ' + x; addListItem(); }) function addListItem() { //console.log(myInput.value); //console.log(val); console.dir(ul); console.log(ul.children.length); console.log(ul.childElementCount); const lis = document.querySelectorAll('li'); //console.log(lis.length); if (myInput.value.length > 3 && lis.length < 5) { const li = document.createElement('li'); li.textContent = myInput.value; const val1 = ul.appendChild(li); Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 20. //console.log(val1); } } </script> </body> </html> Adding Event Listeners to All Matching Elements on Page - Dynamically adding <!doctype html> <html> <head> <title>Example querySelectorAll Click</title> <style> .active { color: blue; } </style> </head> <body> <h1>Hello</h1> <ul class="myList"> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 21. <li>One</li> <li>Two</li> <li>Three</li> </ul> <script> const ul = document.querySelector('ul.myList'); const lis = ul.querySelectorAll('li'); const btn = document.createElement('button'); let counter = lis.length; btn.textContent = 'Click Me'; document.body.append(btn); btn.addEventListener('click', (e) => { counter++; const li = document.createElement('li'); li.acter = 0; li.textContent = `Value (${counter}) ${li.acter} - `; li.addEventListener('click', updateItem); ul.append(li); }) lis.forEach((li) => { console.log(li); li.acter = 0; li.addEventListener('click', updateItem); }) function updateItem(e) { Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/
  • 22. const ele = e.target; console.dir(ele); ele.acter++; console.log(ele.acter); let temp = ele.textContent; ele.textContent = `${temp} ${ele.acter}`; ele.classList.toggle('active'); console.log(ele.classList.contains('active')); } </script> </body> </html> Code by Laurence Svekis - JavaScript Course https://basescripts.com/ Get the Full Modern Web Development Course at https://www.udemy.com/course/modern-web-design/