Skip to main content
Hi Everone, welcome to my blog in this blog post i will share the code of JavaScript practice project (click it to to watch the video) "Image Search App" for beginners along with explaination. You can copy the code by clicking the button just above the code. Read the explaiantion for better understanding. so let's get started with index.html file:

index.html



Here is the explaination:

The body section contains the visible content of the web page. It includes the following elements:
h1: Represents the main heading of the page. It contains the text "Image Search App" and a span element with the text "@front.end24".
form: Defines a form for user input. It contains an input element of type "text" with the id "search-input" and a placeholder text "Search for images...". It also has a button element with the id "search-button" for submitting the form.
div class="search-results": Represents a container for the search results. This is where the search results will be displayed.
button id="show-more-button"Show more button: Represents a button with the id "show-more-button" that can be used to load more search results.
script src="index.js"script: Includes an external JavaScript file (index.js) for adding interactivity to the page.
The commented-out div elements with the class "search-result" represent individual search results. Each result consists of an img element displaying an image and an a element containing a link to the image source. These are examples which is used to style the display of images on research and have been commented out in the html file because we will get images dynamically using JavaScript.

style.css:



Here is the explaination:

The body selector sets the styles for the entire body of the page. It sets the background color to a pinkish shade (#e69ad9), sets the font family to cursive, and specifies a line height of 1.6. It also removes any margin and sets the text color to white.
The h1 selector styles the main heading of the page. It sets the font size to 36 pixels, makes it bold, centers the text, and adds top and bottom margins. The margin-top is set to 40 pixels and margin-bottom to 60 pixels.
The h1 span selector styles the span element within the heading. It sets the font size to 25 pixels.
The form selector styles the form element. It uses flexbox to center the form both horizontally and vertically within its container. It also adds a bottom margin of 60 pixels.
The #search-input selector styles the search input field. It sets the width to 60% with a maximum width of 400 pixels, adds padding, removes the border, and adds a box shadow effect. The font size is set to 18 pixels, and the text color is set to #333 (a dark gray).
The #search-button selector styles the search button. It sets the padding, background color, text color, border, font size, box shadow, and cursor. It also adds a border radius and a transition effect for the background color on hover.
The .search-results selector styles the container for the search results. It uses flexbox with the flex-wrap property set to wrap to create a responsive layout that wraps the search result items. The maximum width is set to 1200 pixels, and left and right margins are set to auto for centering. It also adds padding.
The .search-result selector styles each individual search result item. It sets the bottom margin, width to 30%, border radius, and box shadow. It also applies an overflow property to handle any content that exceeds the container's dimensions.
The .search-result:hover img selector styles the image inside a search result item on hover. It applies a scale transformation to make the image slightly larger, creating a zoom effect.
The .search-result img selector styles the images within the search result items. It sets the width to 100%, a fixed height of 200 pixels, and uses object-fit: cover to ensure the image fills the available space while preserving its aspect ratio. It also adds a transition effect for smooth scaling.
The .search-result a selector styles the links within the search result items. It adds padding, sets the display to block, and sets the color and text decoration. It also applies a transition effect for the background color on hover.
The .search-result:hover a selector styles the links within a search result item on hover. It adds a background color with slight transparency.
The #show-more-button selector styles the "Show more" button. It sets the background color, border, text color, padding, display, margin, text alignment, border radius, and cursor. It also applies a transition effect for the background color on hover. The button is initially set to display: none to hide it.
The #show-more-button:hover selector styles the "Show more" button on hover. It changes the background color to a darker shade of the original color.
The @media queries introduce responsive styles based on different

index.js:



Here is the explaination:

The code starts by defining a constant variable accessKey that stores an API access key. This key is used to authenticate requests to the Unsplash API.
The code then selects various elements from the HTML document using document.querySelector and document.getElementById. These elements include the form, the search input field, the search results container, and the "Show more" button.
Two variables, inputData and page, are declared and initialized. inputData will store the user's search input, and page will keep track of the current page of search results.
The searchImages function is defined as an asynchronous function. It is responsible for performing the image search and displaying the results.
Inside the searchImages function, the user's input is obtained from the search input field (searchInput.value). Then, a URL is constructed using the Unsplash API endpoint, the current page, the user's input, and the API access key.
The fetch function is used to send a GET request to the constructed URL and retrieve the response. The response is then parsed as JSON using the response.json() method.
If page is equal to 1, indicating a fresh search, the searchResults container's content is cleared using searchResults.innerHTML = "".
The search results are extracted from the JSON response (data.results), and a map function is used to iterate over each result.
For each search result, a new div element with the class "search-result" is created (document.createElement("div")). Inside this wrapper, an img element and an a element are created using document.createElement.
The src attribute of the img element is set to the result.urls.small value from the API response, which represents the URL of the image. The alt attribute is set to the result.alt_description, which provides an alternative text for the image.
The href attribute of the a element is set to result.links.html, which is the URL to the Unsplash page of the image. The target attribute is set to "_blank" to open the link in a new tab. The textContent of the a element is set to result.alt_description, which will display a description of the image.
The img and a elements are appended as children to the imageWrapper element, and the imageWrapper is appended as a child to the searchResults container.
The page variable is incremented to prepare for the next page of search results.
If page is greater than 1, indicating there are more pages to load, the "Show more" button's display style is set to "block" to make it visible.
An event listener is added to the form element, listening for the "submit" event. When the form is submitted, the default form submission is prevented (event.preventDefault()), and the page variable is reset to 1. Then, the searchImages function is called to perform the search.
Another event listener is added to the "Show more" button. When the button is clicked, the searchImages function is called to load more search results.

FINAL WORDS:
To sum up, this code provides a basic implementation of an image search app using HTML, CSS, and JavaScript. It demonstrates how to fetch data from an API, handle user input, and dynamically update the UI based on the search results.

Thank you
Don't forget to leave your feedback in the comment section. Follow front.end24 on instagram for more project ideas

Comments

Popular posts from this blog

Project name: Profile statistics

Hi,My name is Maimoona and Welcome to my blog. In this post I will share code of the project profile statistics along with explaination. So let's get started: 1) index.html The first line, , is a document type declaration that tells the browser that this is an HTML5 document. The html element is the root element of the HTML document, and it contains two child elements: head and body. The head element contains meta information about the document, such as the character encoding, viewport settings, and title of the page. It also links to an external stylesheet called style.css, which is used to define the appearance of the webpage. The body element contains the content of the webpage, which is divided into two main sections: a profile container and a profile statistics container. The profile container contains an image, a text paragraph, and a username heading, which provide information about the owner of the Instagram page. The profile statistics container co...

Anmie Pics Generator

Hi Everone, welcome to my blog in this blog post i will share the code of JavaScript practice project (click it to view video) "Anmie Pics Generator" for beginners along with explaination. You can copy the code by clicking the button just above right corner of the code. Read the explaiantion for better understanding. I will also mention where you can do changes of your choice so let's get started with index.html file: INDEX.HTML: Anime Pics Generator Random Anime Pics Generator front.end24 Generate Anime Name Click to Copy Here is the explaination: 1. The code starts with the '!DOCTYPE html' declaration, which specifies that this is an HTML document. 2. The 'html' element represents the root of an HTML document. 3. The 'head' element contains meta-information about the HTML document, such as the character set, viewport s...

snack zilla : a responsive design with a slider

Hi Everone, welcome to my blog and and to new blog post in this blog post i will share the code of JavaScript practice project "Snack Zilla a responsive design with a slider" for beginners along with explaination. You can copy the code by clicking the button just above the code. Read the explaiantion for better understanding. let's get started with index.html file: index.html Snack Zilla Snack Zilla Home About Services Contact Us Welcome to Snack Zilla! your ultimate destination for mouthwatering snacks that will satisfy all your cravings! We are thrilled to have you here and can't wait to take you on a delicious journey.. View Menu Today's offer Get 50% discount on following items: View List ...