-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy.js
More file actions
153 lines (113 loc) · 4.44 KB
/
my.js
File metadata and controls
153 lines (113 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
async function fetchImages(url){
try{
const response = await fetch(url);
const data = await response.json();
return data.message
}catch(error) {
console.error("Error fetching URL",error);
return [];
}
}
async function populateImages(containerId,arrayUrls) {
const container = document.getElementById(containerId);
const imageArray = await fetchImages(arrayUrls);
imageArray.forEach(imageUrl=>{
const imageDiv = document.createElement("div");
//const imageDiv = document.querySelector(".flex-container");
imageDiv.classList.add('imageContainer');
const imageElement = document.createElement("img");
imageElement.classList.add('image-item');
imageElement.src=imageUrl;
imageDiv.appendChild(imageElement);
container.appendChild(imageDiv);
})}
populateImages("image-container",'https://dog.ceo/api/breeds/image/random/6');
// // button
// //Load more
// const btn = document.querySelector(".button");
// btn.addEventListener("click",function(){
// populateImages("image-container","https://dog.ceo/api/breeds/image/random/3")
// })
//Scroll to top
const scroll = document.querySelector(".scrollTop");
scroll.addEventListener("click",function(){
window.scrollTo({top:0,behavior:"smooth"})
})
// Function to check if the user has scrolled to the bottom of the page
function isAtBottom() {
console.log("atBottom");
return (window.innerHeight + window.scrollY) >= document.body.offsetHeight;
}
// Function to handle scroll event
// function handleScroll() {
// console.log("entered")
// if (isAtBottom()) {
// console.log("populated");
// populateImages("image-container","https://dog.ceo/api/breeds/image/random/3");
// }
// }
// Add scroll event listener
window.addEventListener('scroll', handleScroll);
// Show or hide loading icon based on scroll position
function handleScroll() {
var loadingIcon = document.getElementById('loadingIcon');
if (isAtBottom()) {
populateImages("image-container","https://dog.ceo/api/breeds/image/random/3");
loadingIcon.style.display = 'block';
// Simulated loading process (setTimeout is used here, replace with actual loading process)
setTimeout(function() {
loadingIcon.style.display = 'none';
// Trigger the function to load more content here
}, 2000); // Simulated loading time of 2 seconds
} else {
loadingIcon.style.display = 'none';
}
}
// Login card
const openCardBtn = document.getElementById('openCardBtn');
const closeCardBtn = document.querySelector('.close-card-btn');
const cardContainer = document.querySelector('.card-container');
console.log(openCardBtn);
console.log(closeCardBtn);
console.log(cardContainer);
openCardBtn.addEventListener('click', function() {
cardContainer.style.display = 'flex';
});
closeCardBtn.addEventListener('click', function() {
cardContainer.style.display = 'none';
});
window.addEventListener('click', (e) => {
if (e.target === cardContainer) {
cardContainer.style.display = 'none';
}
});
// LOGIN Details
const form = document.getElementById('loginForm');
const messageDiv = document.getElementById('message');
form.addEventListener('submit', (e) => {
e.preventDefault();
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
// Send the login request to the server
sendLoginRequest(username, password);
});
function sendLoginRequest(username, password) {
// Create an XMLHttpRequest object
const xhr = new XMLHttpRequest();
// Open a POST request to the server-side script
xhr.open('POST', 'login.php', true);
// Set the request header for sending JSON data
xhr.setRequestHeader('Content-Type', 'application/json');
// Define the callback function for handling the server response
xhr.onload = function() {
if (xhr.status === 200) {
const response = xhr.responseText;
messageDiv.textContent = response;
} else {
messageDiv.textContent = 'An error occurred. Please try again.';
}
};
// Send the request with the username and password as JSON
const data = JSON.stringify({ username, password });
xhr.send(data);
}