开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 309|回复: 11
收起左侧

[已解决] html容器为啥不能左右滑动

 关闭 [复制链接]
结帖率:100% (1/1)
发表于 2024-3-27 15:12:36 | 显示全部楼层 |阅读模式   江西省赣州市
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!

回答提醒:如果本帖被关闭无法回复,您有更好的答案帮助楼主解决,请发表至 源码区 可获得加分喔。
友情提醒:本版被采纳的主题可在 申请荣誉值 页面申请荣誉值,获得 1点 荣誉值,荣誉值可兑换荣誉会员、终身vip用户组。
快捷通道:申请荣誉值无答案申请取消悬赏投诉有答案未采纳为最佳
结帖率:55% (17/31)

签到天数: 7 天

发表于 2024-3-27 15:12:37 | 显示全部楼层   河北省邢台市
父容器是flex,试试按钮添加样式flex-shrink!

[CSS] 纯文本查看 复制代码
flex-shrink: 0;
回复

使用道具 举报

结帖率:100% (2/2)

签到天数: 1 天

发表于 2024-3-27 15:26:27 | 显示全部楼层   四川省成都市
width=device-width  ,定义了区域宽度随设备,那么页面就会自动缩放。 你这里可以定义一个固定宽度,就会有左右滚动条了
回复

使用道具 举报

结帖率:100% (1/1)
 楼主| 发表于 2024-3-27 15:31:36 高大上手机用户 | 显示全部楼层   江西省赣州市
frankiy 发表于 2024-3-27 15:26
width=device-width  ,定义了区域宽度随设备,那么页面就会自动缩放。 你这里可以定义一个固定宽度,就会 ...

但是奇怪的是这段代码就可以,我始终没看出来差别
<!DOCTYPE html>
<html>
<head>
<style>
.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; /* 这里设置圆角半径 */
}
</style>
</head>
<body>
<div class="container">
<button class="button" onclick="uploadImage(this)" onmousedown="startLongPress(this)" onmouseup="endLongPress()">上传图片</button>
</div>

<script>
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 = '点击重新上传\n长按删除此图'; // 添加这行代码来改变按钮的文本
createNewButton(button);
} else {
button.style.backgroundImage = "url(" + reader.result + ")";
button.innerText = '点击重新上传\n长按删除此图'; // 添加这行代码来改变按钮的文本
}
};
reader.readAsDataURL(file);
};
input.click();
}

function createNewButton(button) {
const newButton = document.createElement('button');
newButton.innerText = '上传图片';
newButton.className = 'button';
newButton.onclick = function() {
uploadImage(newButton);
};
newButton.onmousedown = function() {
startLongPress(newButton);
};
newButton.onmouseup = function() {
endLongPress();
};
button.parentNode.insertBefore(newButton, button.nextSibling);
}

let timer;
var targetElement;

function startLongPress(element) {
if (event.target.style.backgroundImage !== "") { // 检查背景图片是否为空
timer = setTimeout(showTooltip, 1000); // 设置延时为1000毫秒(1秒)
targetElement = element;
}
}

function endLongPress() {
clearTimeout(timer); // 清除定时器
}

function showTooltip() {
targetElement.remove();
}


// 新增的代码
async function uploadImageAndFetchUrl(button) {
  var input = document.createElement("input");
  input.type = "file";
  input.accept = "image/*";
  input.onchange = async function () {
    var file = this.files[0];
    var formData = new FormData();
    formData.append("image", file);

    try {
      const response = await fetch("YOUR_API_ENDPOINT", {
        method: "POST",
        body: formData,
      });

      if (response.ok) {
        const jsonResponse = await response.json();
        const imageUrl = jsonResponse.imageUrl; // 假设返回的JSON数据中包含名为imageUrl的字段
        console.log("图片URL:", imageUrl);
      } else {
        console.error("上传失败");
      }
    } catch (error) {
      console.error("上传过程中出现错误:", error);
    }
  };
  input.click();
}
</script>
</body>
</html>
回复

使用道具 举报

结帖率:100% (1/1)
 楼主| 发表于 2024-3-27 15:32:45 高大上手机用户 | 显示全部楼层   江西省赣州市
frankiy 发表于 2024-3-27 15:26
width=device-width  ,定义了区域宽度随设备,那么页面就会自动缩放。 你这里可以定义一个固定宽度,就会 ...

哦,我看到了你说的,是顶部设备那里width=device-width,有没有其他解决办法。。。一定得定义固定宽度吗
回复

使用道具 举报

结帖率:100% (2/2)

签到天数: 1 天

发表于 2024-3-27 15:35:22 | 显示全部楼层   四川省成都市
hu947177386 发表于 2024-3-27 15:32
哦,我看到了你说的,是顶部设备那里width=device-width,有没有其他解决办法。。。一定得定义固定宽度吗 ...

还可以用CSS表解决。 但是我不会。
回复

使用道具 举报

结帖率:100% (1/1)
 楼主| 发表于 2024-3-27 15:37:51 高大上手机用户 | 显示全部楼层   江西省赣州市
frankiy 发表于 2024-3-27 15:35
还可以用CSS表解决。 但是我不会。

回复

使用道具 举报

结帖率:100% (1/1)
 楼主| 发表于 2024-3-27 15:39:02 | 显示全部楼层   江西省赣州市
frankiy 发表于 2024-3-27 15:35
还可以用CSS表解决。 但是我不会。

我问了ai问了半天,每次回答的都是css里我已经写了的代码,我也是无语了
回复

使用道具 举报

结帖率:75% (9/12)

签到天数: 28 天

发表于 2024-3-27 15:41:57 | 显示全部楼层   河南省商丘市
可以的,得设置一下下才行。设置宽度大于屏幕就好了
回复

使用道具 举报

结帖率:100% (2/2)

签到天数: 1 天

发表于 2024-3-27 15:43:25 | 显示全部楼层   四川省成都市
hu947177386 发表于 2024-3-27 15:39
我问了ai问了半天,每次回答的都是css里我已经写了的代码,我也是无语了

你的确可以自己在CSS里设置展示容器的大小啊。你的代码里也定义了, 其实有很多办法可以弄。

display: flex;
align-items: flex-start;
width: 100%;   定义容器宽度
height: 100%;  
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表