Creating a random photos section using HTML and JavaScript involves dynamically fetching and displaying random images each time the page loads or a button is clicked. For this example, we’ll use the Unsplash API to fetch random images. You’ll need an Unsplash API access key, which you can get by signing up at Unsplash Developer.
Explanation
- HTML Structure:
- The HTML structure includes a container with a title, a div with the ID
photo-grid
for displaying the photos, and a button for loading more photos.
- The HTML structure includes a container with a title, a div with the ID
- CSS Styling:
- The
.photo-grid
class uses CSS Grid to create a responsive grid layout for the photos. - The
img
elements within the grid are styled with rounded corners and a box shadow. - The
button
is styled for a modern look with hover effects.
- The
- JavaScript Functionality:
- The
accessKey
variable stores your Unsplash API access key. - When the DOM content is loaded, the
loadRandomPhotos
function is called to fetch and display random photos. - The
loadPhotosButton
is set up with an event listener to load more photos when clicked. - The
loadRandomPhotos
function fetches 10 random photos from the Unsplash API and appends them to thephoto-grid
div.
- The
Replace 'YOUR_UNSPLASH_ACCESS_KEY'
with your actual Unsplash API access key. This script fetches random photos from Unsplash and displays them in a grid layout, allowing users to load more photos by clicking the button.
Reviews
There are no reviews yet. Be the first one to write one.