使用JavaScript生成可编辑能够插入图片改变图片大小点击图片能够放大的网页表格
时间: 2024-03-24 20:41:48 浏览: 63
可编辑javascript表格
以下是使用JavaScript生成可编辑能够插入、改变大小、点击放大的网页表格的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Editable Table with Resizable and Zoomable Images</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 5px;
text-align: center;
}
th {
background-color: #f2f2f2;
}
td {
position: relative;
}
.resizable {
border: 2px dotted black;
position: absolute;
right: 0;
bottom: 0;
width: 20px;
height: 20px;
cursor: nwse-resize;
}
.zoomable {
cursor: zoom-in;
max-width: 100%;
max-height: 100%;
}
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: none;
justify-content: center;
align-items: center;
}
.overlay img {
max-width: 90%;
max-height: 90%;
}
</style>
</head>
<body>
<h1>Editable Table with Resizable and Zoomable Images</h1>
<table id="editable-table">
<thead>
<tr>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<tbody>
<tr>
<td contenteditable="true">Product 1</td>
<td>
<div class="image-container">
<img class="zoomable" src="" alt="No Image">
<div class="resizable"></div>
</div>
</td>
</tr>
<tr>
<td contenteditable="true">Product 2</td>
<td>
<div class="image-container">
<img class="zoomable" src="" alt="No Image">
<div class="resizable"></div>
</div>
</td>
</tr>
</tbody>
</table>
<div class="overlay">
<img src="" alt="">
</div>
<script>
const resizable = document.querySelectorAll('.resizable');
const zoomable = document.querySelectorAll('.zoomable');
const overlay = document.querySelector('.overlay');
const overlayImage = document.querySelector('.overlay img');
// Make images resizable
resizable.forEach(handle => {
const container = handle.parentElement;
const image = container.querySelector('.zoomable');
let startX, startY, startWidth, startHeight;
const handleResize = event => {
const newWidth = startWidth + (event.clientX - startX);
const newHeight = startHeight + (event.clientY - startY);
container.style.width = `${newWidth}px`;
container.style.height = `${newHeight}px`;
image.style.width = `${newWidth}px`;
image.style.height = `${newHeight}px`;
};
handle.addEventListener('mousedown', event => {
event.preventDefault();
event.stopPropagation();
startX = event.clientX;
startY = event.clientY;
startWidth = parseInt(document.defaultView.getComputedStyle(container).width, 10);
startHeight = parseInt(document.defaultView.getComputedStyle(container).height, 10);
document.addEventListener('mousemove', handleResize);
document.addEventListener('mouseup', () => {
document.removeEventListener('mousemove', handleResize);
});
});
});
// Make images zoomable
zoomable.forEach(image => {
image.addEventListener('click', () => {
overlayImage.src = image.src;
overlay.style.display = 'flex';
});
});
// Close overlay on click
overlay.addEventListener('click', () => {
overlay.style.display = 'none';
});
</script>
</body>
</html>
```
该代码会生成一个带有两行可编辑的表格,每行包含一个可编辑的文本单元格和一个包含可调整大小和可放大的图像的单元格。当用户选择图像并上传时,JavaScript代码会在单元格中显示图像,并允许用户拖动边缘来调整大小,以及单击图像以放大它。单击放大的图像会使其在另一个覆盖层中显示。您可以根据需要添加更多行或自定义样式。
阅读全文