-
Notifications
You must be signed in to change notification settings - Fork 0
/
example4_randomuser.html
123 lines (109 loc) · 3.3 KB
/
example4_randomuser.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random User Fetcher</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f9f9f9;
text-align: center;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.user-card {
background: #ffffff;
border-radius: 12px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
margin: 15px;
padding: 20px;
display: inline-block;
width: 250px;
text-align: center;
transition: transform 0.3s, box-shadow 0.3s;
}
.user-card:hover {
transform: translateY(-10px);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
.user-card img {
border-radius: 50%;
width: 120px;
height: 120px;
object-fit: cover;
margin-bottom: 15px;
}
.user-card h3 {
margin: 10px 0;
font-size: 20px;
color: #333;
}
.user-card p {
margin: 5px 0;
color: #666;
font-size: 14px;
}
.refresh-btn {
padding: 12px 25px;
font-size: 18px;
color: #ffffff;
background-color: #007bff;
border: none;
border-radius: 8px;
cursor: pointer;
margin-top: 20px;
transition: background-color 0.3s;
}
.refresh-btn:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h1>Random User Fetcher</h1>
<button class="refresh-btn" id="refreshBtn">Refresh</button>
<div id="userList"></div>
</div>
<script>
const userList = document.getElementById('userList');
const refreshBtn = document.getElementById('refreshBtn');
// Function to fetch users from API
function fetchUsers() {
fetch('https://randomuser.me/api/?results=5')
.then(response => response.json())
.then(data => {
displayUsers(data.results);
})
.catch(error => {
console.error('Error fetching users:', error);
});
}
// Function to display users on the page
function displayUsers(users) {
userList.innerHTML = '';
users.forEach(user => {
const userCard = document.createElement('div');
userCard.className = 'user-card';
userCard.innerHTML = `
<img src="${user.picture.large}" alt="${user.name.first} ${user.name.last}">
<h3>${user.name.first} ${user.name.last}</h3>
<p>${user.email}</p>
`;
userList.appendChild(userCard);
});
}
// Fetch users on page load
fetchUsers();
// Refresh button click event
refreshBtn.addEventListener('click', () => {
fetchUsers();
});
</script>
</body>
</html>