利用html、css、js制作一个可以由登录界面跳转而来的图片展示页面,在该页面要能够上传和下载图片,写出完整代码
时间: 2024-03-07 17:50:41 浏览: 125
好的,下面是一个基于 HTML、CSS 和 JS 实现的图片展示页面,包括登录界面、上传和下载图片的功能。
HTML 代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片展示页面</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- 登录界面 -->
<div class="login-container">
<h1>欢迎来到图片展示页面</h1>
<form>
<div>
<label>用户名</label>
<input type="text" id="username-input">
</div>
<div>
<label>密码</label>
<input type="password" id="password-input">
</div>
<button type="button" id="login-button">登录</button>
</form>
</div>
<!-- 图片展示区域 -->
<div class="gallery-container">
<div class="upload-container">
<h2>上传图片</h2>
<form>
<input type="file" id="image-upload">
<button type="button" id="upload-button">上传</button>
</form>
</div>
<div class="gallery"></div>
</div>
<!-- 下载模态框 -->
<div class="modal">
<div class="modal-content">
<span class="close">×</span>
<img id="modal-image">
<div>
<a href="#" id="download-link">下载</a>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS 代码:
```css
body {
margin: 0;
padding: 0;
font-family: 'Helvetica Neue', sans-serif;
}
.login-container,
.gallery-container {
display: none;
width: 100%;
max-width: 800px;
margin: auto;
padding: 20px;
}
.login-container {
text-align: center;
}
.login-container h1 {
margin-top: 0;
}
.login-container form {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 20px;
}
.login-container form div {
display: flex;
flex-direction: column;
margin-bottom: 10px;
width: 100%;
}
.login-container form label {
font-weight: bold;
margin-bottom: 5px;
}
.login-container form input {
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
width: 100%;
}
.login-container form button {
margin-top: 20px;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: #fff;
cursor: pointer;
}
.gallery-container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
}
.upload-container {
width: 100%;
margin-bottom: 20px;
}
.upload-container h2 {
margin-top: 0;
}
.upload-container form {
display: flex;
align-items: center;
justify-content: center;
margin-top: 10px;
}
.upload-container input[type="file"] {
display: none;
}
.upload-container label {
display: block;
padding: 10px;
border: 1px solid #007bff;
border-radius: 5px;
background-color: #007bff;
color: #fff;
cursor: pointer;
}
.upload-container button {
margin-left: 10px;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: #fff;
cursor: pointer;
}
.gallery {
width: 100%;
}
.gallery img {
width: 100%;
max-width: 300px;
margin-bottom: 20px;
cursor: pointer;
}
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.8);
}
.modal-content {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin: auto;
margin-top: 100px;
width: 80%;
max-width: 800px;
height: 80%;
max-height: 800px;
background-color: #fff;
padding: 20px;
border-radius: 10px;
position: relative;
}
.modal-content .close {
position: absolute;
top: 0;
right: 0;
font-size: 30px;
font-weight: bold;
cursor: pointer;
color: #fff;
background-color: #007bff;
border-radius: 50%;
width: 30px;
height: 30px;
display: flex;
justify-content: center;
align-items: center;
}
.modal-content img {
width: 100%;
max-height: 80%;
object-fit: contain;
}
.modal-content div {
margin-top: 20px;
text-align: center;
}
.modal-content a {
padding: 10px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: #fff;
cursor: pointer;
}
.modal-content a:hover {
text-decoration: none;
}
```
JS 代码:
```js
// 登录界面
const loginContainer = document.querySelector('.login-container');
const usernameInput = document.querySelector('#username-input');
const passwordInput = document.querySelector('#password-input');
const loginButton = document.querySelector('#login-button');
// 图片展示区域
const galleryContainer = document.querySelector('.gallery-container');
const uploadContainer = document.querySelector('.upload-container');
const imageUpload = document.querySelector('#image-upload');
const uploadButton = document.querySelector('#upload-button');
const gallery = document.querySelector('.gallery');
// 下载模态框
const modal = document.querySelector('.modal');
const modalImage = document.querySelector('#modal-image');
const downloadLink = document.querySelector('#download-link');
const modalClose = document.querySelector('.close');
// 登录验证
loginButton.addEventListener('click', () => {
const username = usernameInput.value.trim();
const password = passwordInput.value.trim();
if (username === 'admin' && password === '123456') {
loginContainer.style.display = 'none';
galleryContainer.style.display = 'block';
updateGallery();
} else {
alert('用户名或密码错误!');
}
});
// 更新图片展示
function updateGallery() {
gallery.innerHTML = '';
fetch('http://localhost:3000/images')
.then(res => res.json())
.then(images => {
images.forEach(image => {
const img = document.createElement('img');
img.src = image.path;
img.alt = image.name;
img.addEventListener('click', () => {
modalImage.src = image.path;
downloadLink.href = image.path;
modal.style.display = 'block';
});
gallery.appendChild(img);
});
})
.catch(err => {
console.error(err);
alert('获取图片列表失败!');
});
}
// 上传图片
uploadButton.addEventListener('click', () => {
const file = imageUpload.files[0];
const formData = new FormData();
formData.append('image', file);
fetch('http://localhost:3000/images', {
method: 'POST',
body: formData
})
.then(res => res.json())
.then(data => {
alert('上传成功!');
updateGallery();
})
.catch(err => {
console.error(err);
alert('上传失败!');
});
});
// 关闭模态框
modalClose.addEventListener('click', () => {
modal.style.display = 'none';
});
// 点击模态框外部区域关闭模态框
window.addEventListener('click', event => {
if (event.target === modal) {
modal.style.display = 'none';
}
});
```
注意:这段代码是一个示例,仅供参考,需要结合后端技术来完整实现上传和下载图片的功能。其中涉及到的 API 接口需要根据具体后端实现进行修改。
阅读全文