|
10精币
代码如下,新建按钮数量多的情况下,只会在容器里越来越小,不会让容器左右滑动,问了ai半天都没给出正确的答案,求大佬帮忙看看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>图片预览</title>
<style>
body {
background-image: url('/pic/4.jpeg');
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
overflow-y: auto;
overflow-x: hidden;
}
.container {
display: flex;
align-items: flex-start;
width: 100%;
height: 100%;
overflow-x: auto;
white-space: nowrap;
border: 1px solid black;
border-radius: 12px; /* 这里设置圆角半径 */
}
.button {
display: inline-block;
margin-right: 10px;
width: 100px; /* 设置按钮宽度 */
height: 200px; /* 设置按钮高度 */
background-color: #4CAF50;
background-size: cover;
background-position: center;
border: none;
color: white;
padding: 15px auto;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 15px;
margin: 4px 2px;
cursor: pointer;
border-radius: 12px; /* 这里设置圆角半径 */
}
/* 隐藏默认的模态框 */
.modal {
display: none;
z-index: 10001;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.4);
}
/* 模态框内容 */
.modal-content {
display: flex;
z-index: 10000;
flex-direction: column;
align-items: center;
justify-content: center;
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
border-radius: 10px; /* 添加圆角 */
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19); /* 添加阴影 */
}
.modal button:hover {
background-color: #45a049;
}
.modal button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
transition-duration: 0.4s;
border-radius: 5px; /* 添加圆角 */
width: 100%;
}
</style>
</head>
<body>
<div class="container">
<button class="button">上传图片</button>
</div>
<div id="xzModal" class="modal">
<div class="modal-content">
<label for="dhjfye">请选择要执行的操作</label>
<br>
<button>重新上传</button>
<button>删除图片</button>
<button>返回</button>
</div>
</div>
<script>
var duiyingan;
function cxsc() {
xzcloseModal();
uploadImage(duiyingan);
}
function pdpicsf(button) {
duiyingan = button;
if (button.style.backgroundImage === "") {
uploadImage(button);
}else {
xzopendhModal();
}
}
function uploadImage(button) {
var input = document.createElement("input");
input.type = "file";
input.accept = "image/*";
input.onchange = function() {
var file = this.files[0];
var reader = new FileReader();
reader.onload = function() {
if (button.style.backgroundImage === ""){
button.style.backgroundImage = "url(" + reader.result + ")";
button.innerText = ''; // 添加这行代码来改变按钮的文本
createNewButton(button);
}else{
button.style.backgroundImage = "url(" + reader.result + ")";
button.innerText = ''; // 添加这行代码来改变按钮的文本
}
};
reader.readAsDataURL(file);
};
input.click();
}
function createNewButton(button) {
const newButton = document.createElement('button');
newButton.innerText = '上传图片';
newButton.className = 'button';
newButton.onclick = function() {
pdpicsf(newButton);
};
button.parentNode.insertBefore(newButton, button.nextSibling);
}
function sctp() {
xzcloseModal();
duiyingan.remove();
}
function xzopendhModal() {
var xzmodal = document.getElementById("xzModal");
xzmodal.style.display = "block";
xzmodal.style.animationName = "fadeIn";
xzmodal.style.animationDuration = "1s";
}
function xzcloseModal() {
var xzmodal = document.getElementById("xzModal");
xzmodal.style.display = "none";
}
</script>
</body>
</html>
|
最佳答案
查看完整内容
父容器是flex,试试按钮添加样式flex-shrink!
|