Changed around line 1
- let currentPage = 1;
- const perPage = 15;
+ const API_KEY = 'YOUR_UNSPLASH_API_KEY'; // Replace with actual API key
+ const wallpaperGrid = document.getElementById('wallpaperGrid');
+ const loading = document.getElementById('loading');
+ let page = 1;
- async function fetchWallpapers(query = '', page = 1) {
- const url = `https://api.pexels.com/v1/search?query=${query}&per_page=${perPage}&page=${page}`;
-
+ async function fetchWallpapers(query = '') {
+ loading.style.display = 'block';
+ const url = query
+ ? `https://api.unsplash.com/search/photos?page=${page}&query=${query}&per_page=20`
+ : `https://api.unsplash.com/photos?page=${page}&per_page=20`;
+
- Authorization: 'YOUR_PEXELS_API_KEY'
+ 'Authorization': `Client-ID ${API_KEY}`
+
- displayWallpapers(data.photos);
+ const images = query ? data.results : data;
+
+ images.forEach(image => {
+ const card = createWallpaperCard(image);
+ wallpaperGrid.appendChild(card);
+ });
+ } finally {
+ loading.style.display = 'none';
- function displayWallpapers(photos) {
- const container = document.getElementById('wallpapersContainer');
+ function createWallpaperCard(image) {
+ const card = document.createElement('div');
+ card.className = 'wallpaper-card';
- if (currentPage === 1) {
- container.innerHTML = '';
- }
-
- photos.forEach(photo => {
- const wallpaper = document.createElement('div');
- wallpaper.className = 'wallpaper';
- wallpaper.innerHTML = `
-
- `;
- container.appendChild(wallpaper);
+ const img = document.createElement('img');
+ img.src = image.urls.regular;
+ img.alt = image.alt_description || 'Wallpaper';
+ img.loading = 'lazy';
+
+ card.appendChild(img);
+
+ card.addEventListener('click', () => {
+ window.open(image.urls.full, '_blank');
+
+ return card;
- document.getElementById('searchBtn').addEventListener('click', () => {
- const query = document.getElementById('searchInput').value;
- currentPage = 1;
+ // Infinite scroll
+ window.addEventListener('scroll', () => {
+ if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 1000) {
+ page++;
+ fetchWallpapers();
+ }
+ });
+
+ // Search functionality
+ const searchBtn = document.getElementById('searchBtn');
+ const searchInput = document.getElementById('search');
+
+ searchBtn.addEventListener('click', () => {
+ const query = searchInput.value.trim();
+ wallpaperGrid.innerHTML = '';
+ page = 1;
- document.getElementById('loadMore').addEventListener('click', () => {
- const query = document.getElementById('searchInput').value;
- currentPage++;
- fetchWallpapers(query, currentPage);
+ searchInput.addEventListener('keypress', (e) => {
+ if (e.key === 'Enter') {
+ searchBtn.click();
+ }
+ });
+
+ // Category filters
+ document.querySelectorAll('.categories button').forEach(button => {
+ button.addEventListener('click', () => {
+ document.querySelector('.categories .active').classList.remove('active');
+ button.classList.add('active');
+ wallpaperGrid.innerHTML = '';
+ page = 1;
+ fetchWallpapers(button.dataset.category === 'all' ? '' : button.dataset.category);
+ });
- fetchWallpapers();
+ fetchWallpapers();