From 2beaf5307e326652a6a38570a36a4a2f51606bc7 Mon Sep 17 00:00:00 2001 From: jessiwowww <142979406+jessiwowww@users.noreply.github.com> Date: Wed, 31 Jan 2024 14:36:34 +0100 Subject: [PATCH] jessiwowww second project --- Magnify-1.6s-216px.svg | 14 ++++ favicon-32x32.png | Bin 0 -> 1038 bytes index.html | 42 +++++----- script.js | 186 ++++++++++++++++++++++++----------------- style.css | 116 ++++++++++++------------- 5 files changed, 202 insertions(+), 156 deletions(-) create mode 100644 Magnify-1.6s-216px.svg create mode 100644 favicon-32x32.png diff --git a/Magnify-1.6s-216px.svg b/Magnify-1.6s-216px.svg new file mode 100644 index 0000000..9adaac5 --- /dev/null +++ b/Magnify-1.6s-216px.svg @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/favicon-32x32.png b/favicon-32x32.png new file mode 100644 index 0000000000000000000000000000000000000000..45083649a07fb1c5d0d5e455a7cc7efc6114ebe0 GIT binary patch literal 1038 zcmV+p1o8WcP)Px&$Vo&&R9HvNms?DfM-<0@!?GwF+FiLtg@-K{ZRD~Pt!>trn7%Zr4=SLEeIT)0 zHMoXoU?n~j6(WeXF(L7Zw($i>DlLJSCi`IHON~L;7QD2nu`wk}YvrC41Qw>5`M9i9 zDwH)}zhsjyJ7@mqH)qaw=8RyOkCglEAU*)z1Njz6CkTfzT@HhI4B`jiJCNTBacyKd z(&A-CQtsOXmI@FDf!LSX&yJ6SY=-GNAzFX=w>e7!NVz{2L^a4#z`9Jwi_NA$)Pwv) zh->37(s&_&l!cifT0vy}N5_k%$@?Jp3GwyA#d`k$kg^~jEPfy{GS25!`eFKgA-)NO z>pllytUT)0D{wyNvma*M;gOgJF!o&DiCoy1il5pJ@%CI!gaI7*6l7V1!@h)^_)Lhe z%GIC-Fg~^Dd#lLym2yu(C|iA^vj9b3fcR*oDyy@XUkcIU5k`gM_Z1YG)%jhRt?I%C zA?1OCAe$nkx-uQ{p#o?C`SHpoM#@@zuC|~v7#!c4l46wkk&!9vb}It|lh|w)qN1R? zdjzM`Mso66g28c=?bOta{@HBN^!1H1Gb2e!u_45Q#8f+{Qf_wrz`N%a+&4&v#H<+(A>*yEHWXs{d*M0ZTztMByxw%;<72pR;b&@U zvIkTILbq=ZSROt4i{9Qb4N+U0Lv{57y}Bbu(pbNKt&YyIV*$2rk0&R`PHStBP-upu zN7HF<@5AGHgPNM3aJv&}Y3b$6nJnt-A9DF}o?c8a>kFwD7gStKQ` z;nb-ftX6@~_a>E<_sGbIqqH=Wn>YWYxw%(ED%gpMX)axIDMM7@GN zd%mZwZ3n^N7?qXxDJV##qhk=4%T8foDu)kuQeB;`0ZK{&G&a7Y0hOb!u3^f`HsSHy zp|&jz*yZ09D?K`+};|X`~j&kBerYeDY3p#$^!}%% z#gUq7Bx?A0_`7m6IvT>^us&xKJ}2A;P)TU|XJ~pMGPx8Rr=f|5(f{H&HXuxFNDKgz zTQUQ}#HMuafHk!(Uw|<2VzK~X;?*p?-!biSy140jRoC;s0pr)pga;Du!T - - - - - Infinite Scroll - - - - - -

Unsplash API - Infinite Scroll

- -
- Loading -
- -
- - - + + + + + + Infinite Scroll + + + + + +

Unsplash API - Infinite Scroll

+ +
+ Loading +
+ +
+ + + \ No newline at end of file diff --git a/script.js b/script.js index 4155a51..3626ec4 100644 --- a/script.js +++ b/script.js @@ -1,78 +1,108 @@ -const imageContainer = document.getElementById('image-container'); -const loader = document.getElementById('loader'); - -let ready = false; -let imagesLoaded = 0; -let totalImages = 0; -let photosArray = []; - -// Unsplash API -const count = 30; -// Normally, don't store API Keys like this, but an exception made here because it is free, and the data is publicly available! -const apiKey = 'jFgS8tteGD425f4oZfygQVaVnD6gt6GucN2yyz3xFek'; -const apiUrl = `https://api.unsplash.com/photos/random?client_id=${apiKey}&count=${count}`; - -// Check if all images were loaded -function imageLoaded() { - imagesLoaded++; - if (imagesLoaded === totalImages) { - ready = true; - loader.hidden = true; - } -} - -// Helper Function to Set Attributes on DOM Elements -function setAttributes(element, attributes) { - for (const key in attributes) { - element.setAttribute(key, attributes[key]); - } -} - -// Create Elements For Links & Photos, Add to DOM -function displayPhotos() { - imagesLoaded = 0; - totalImages = photosArray.length; - // Run function for each object in photosArray - photosArray.forEach((photo) => { - // Create to link to full photo - const item = document.createElement('a'); - setAttributes(item, { - href: photo.links.html, - target: '_blank', - }); - // Create for photo - const img = document.createElement('img'); - setAttributes(img, { - src: photo.urls.regular, - alt: photo.alt_description, - title: photo.alt_description, - }); - // Event Listener, check when each is finished loading - img.addEventListener('load', imageLoaded); - // Put inside , then put both inside imageContainer Element - item.appendChild(img); - imageContainer.appendChild(item); - }); -} - -// Get photos from Unsplash API -async function getPhotos() { - try { - const response = await fetch(apiUrl); - photosArray = await response.json(); - displayPhotos(); - } catch (error) { - // Catch Error Here - } -} - -// Check to see if scrolling near bottom of page, Load More Photos -window.addEventListener('scroll', () => { - if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 1000 && ready) { - ready = false; - getPhotos(); - } -}); - -// On Load -getPhotos(); + + +const imageContainer = document.getElementById('image-container'); +const loader = document.getElementById('loader'); + + +let ready = false; +let imagesLoaded = 0; +let totalImages = 0; +let photosArray = []; +let initialLoad = true; + +// Unsplash API +let count = 5; +const apiKey = 'dUNvFfQX9xJ0hhUhCkKEb7dLggVeenKqpsnfu6FIqWQ'; +let apiUrl = `https://api.unsplash.com/photos/random/?client_id=${apiKey}&count=${count}`; + +function updateAPIURLWithNewCount (picCount) { + apiUrl = `https://api.unsplash.com/photos/random?client_id=${apiKey}&count=${picCount}`; + } + +// Check of all images were loaded +function imageLoaded() { + imagesLoaded++; + // console.log(imagesLoaded); + if (imagesLoaded === totalImages){ + ready = true; + loader.hidden=true; + //console.log('ready =', ready); + // initialLoad = false; + //count = 30; + ApiUrl = `https://api.unsplash.com/photos/random/?client_id=${apiKey}&count=${count}` + } +} + + +// Helper Function to set Attributes on DOM Elements +function setAttributes(element, attributes) { + for (const key in attributes) { + element.setAttribute(key, attributes[key]); + } +} + + +/* Create Elements for Link and Photos*/ +function displayPhotos(){ + imagesLoaded = 0; + totalImages = photosArray.length + console.log('total images', totalImages); + + // Run function for each object + photosArray.forEach((photo) => { + // Create to link to Unsplash + const item = document.createElement('a'); + // item.setAttribute('href', photo.links.html); + //item.setAttribute('target','_blank'); + setAttributes(item, { + href: photo.links.html, + target: '_blank', + }); + // Create for photo + const img = document.createElement('img'); + //img.setAttribute('src', photo.urls.regular); + //img.setAttribute('alt', photo.alt_description); + //img.setAttribute('title', photo.alt_description); + setAttributes(img, { + src: photo.urls.regular, + alt: photo.alt_description, + title: photo.alt_description, + }); + + // Event Listener, check when each is finished loading + img.addEventListener('load', imageLoaded); + + // Put inside , then put both inside imageContainer Element + item.appendChild(img); + imageContainer.appendChild(item); + }); +} + +// Get photos from Unsplash API +async function getPhotos() { + try { + const response = await fetch(apiUrl); + photosArray = await response.json(); + displayPhotos(); + if (isInitialLoad) { + updateAPIURLWithNewCount(30) + isInitialLoad = false + } + } catch (error) { + // Catch Error Here + } +} + +// Check to see if scrolling near bottom of page, Load More Photos + +window.addEventListener('scroll', ()=>{ + if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 1000 && ready){ + ready = false; + getPhotos(); + console.log('load more'); + } + +}); + +// On Load +getPhotos(); diff --git a/style.css b/style.css index d36db76..783f7ea 100644 --- a/style.css +++ b/style.css @@ -1,57 +1,59 @@ -@import url("https://fonts.googleapis.com/css?family=Bebas+Neue&display=swap"); - -html { - box-sizing: border-box; -} - -body { - margin: 0; - font-family: Bebas Neue, sans-serif; - background: whitesmoke; -} - -h1 { - text-align: center; - margin-top: 25px; - margin-bottom: 15px; - font-size: 40px; - letter-spacing: 5px; -} - -/* Loader */ -.loader { - position: fixed; - top: 0; - bottom: 0; - left: 0; - right: 0; - background: rgba(255, 255, 255, 0.8); -} - -.loader img { - position: fixed; - top: 50%; - left: 50%; - transform: translate(-50%, -50%); -} - -/* Image Container */ -.image-container { - margin: 10px 30%; -} - -.image-container img { - width: 100%; - margin-top: 5px; -} - -/* Media Query: Large Smartphone Vertical */ -@media screen and (max-width: 600px) { - h1 { - font-size: 20px; - } - - .image-container { - margin: 10px; - } -} + @import url('https://fonts.googleapis.com/css2?family=Urbanist&display=swap'); + + + +html { + box-sizing: border-box; +} + +body { + margin: 0; + font-family: 'Urbanist', sans-serif; + background-color: whitesmoke; +} + +h1 { + text-align: center; + margin-top: 25px; + margin-bottom: 15px; + font-size: 40px; + letter-spacing: 2px; +} + +/* Loader */ + +.loader { + position: fixed; + top: 0; + bottom: 0; + left: 0; + right: 0; + background-color: rgba(255, 255, 255, 0.8); +} + +.loader img { + position: fixed; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); +} + +/* Image Container */ +.image-container { + margin: 10px 30%; +} + +.image-container img { + width: 100%; + margin-top: 5px; +} + +/* Media Query for Large SmartPhone */ +@media screen and (max-width: 600px){ + h1{ + font-size: 20px; + } + .image-container { + margin: 10px; + } +} \ No newline at end of file