flexbox div中的绝对div行为像固定的

如何解决flexbox div中的绝对div行为像固定的

我正在设计一个关于react的响应式聊天页面视图,并且正在努力解决以下问题。

我有一个绝对位置的div菜单,单击菜单按钮时,该菜单显示在所有元素的顶部,如您在此处看到的:

Page view on computer

一切似乎都正常,即使我将屏幕调整为电话尺寸时,它也可以按预期显示,因为我有一个媒体查询,该查询修改了left属性以适合我想要的div。但是在电话视图中,当我滚动页面时,div沿着屏幕在其偏移位置移动,而我无法修复它:

Page view on phone

据我所知,这种行为与固定位置相对应,我了解到,如果我采用绝对位置,则div应该留在他的位置上。但是我不知道这里发生了什么。也许是搞砸了,因为我正在使用flex(所有东西都通过flex的“方向”属性来排列元素)。

这是我的JSX代码:

return (
    <div id="chat-page-main-container" onClick={onMainContainerClick}>
      <div id="chat-main-menu-select" style={{ display: displayMenu }}>
        <ul className="list-group">
          <li className="list-group-item">Profile</li>
          <li className="list-group-item">Create new group</li>
          <li className="list-group-item">Log out</li>
        </ul>
      </div>

      <div id="chat-left-side-content">
        <div id="chat-main-menu">
          <img
            src="https://upload.wikimedia.org/wikipedia/commons/e/e8/The_Joker_at_Wax_Museum_Plus.jpg"
            alt="unavailable"
          />
          <div id="chat-main-menu-options">
            <div className="main-menu-options-icons">
              <i id="chat-comment-icon" className="fa fa-comments"></i>
            </div>
            <div
              className="main-menu-options-icons"
              style={{ backgroundColor: selectedItem }}
            >
              <i
                id="chat-menu-icon"
                className="fa fa-bars"
                onClick={onMenuIconClick}
              ></i>
            </div>
          </div>
        </div>
        <div id="search-conversation-bar">
          <i className="fa fa-search"></i>
          <input type="text" placeholder="Search for a conversation" />
        </div>
        <div id="chats-component-container">
          {data.map((object,index) => (
            <ChatCard key={index} data={object} />
          ))}
        </div>
      </div>

      <div id="chat-right-side-content">
        <div id="conversation-splash-screen">
          <img src={conversationImage} alt="unavailable" />
          <h3>Welcome to tellit chat page!</h3>
          <p>
            You can search for a contact or a conversation on the left menu.
          </p>
        </div>
      </div>
    </div>
  );

这是我的SASS代码:

::-webkit-scrollbar {
    width: 8px
}

::-webkit-scrollbar-track {
    background: whitesmoke;
}

::-webkit-scrollbar-thumb {
    background: #bababa;
}

#chat-page-main-container {
    display: flex;
    flex-direction: row;
    height: 100%;
    overflow-x: auto;
    white-space: nowrap;

    #chat-main-menu-select {
        position: absolute;
        top: 4em;
        left: 12em;

        ul {
            border-radius: 5px;
            cursor: pointer;
        }

        li:hover {
            background-color: #ededed;
        }
    }

    #chat-left-side-content {
        display: flex;
        flex-direction: column;
        min-width: 400px;
        height: 100%;
        border-right: solid 1px #bababa;

        #chat-main-menu {
            width: 100%;
            height: 71px;
            padding: 10px 25px;
            border-bottom: solid 1px #bababa;
            background-color: #EDEDED;

            img {
                width: 50px;
                height: 50px;
                border-radius: 50%;
                float: left;
            }

            #chat-main-menu-options {
                display: flex;
                flex-direction: row;
                float: right;
                height: 100%;
                padding-top: 5px;

                .main-menu-options-icons {
                    font-size: 25px;
                    opacity: 0.5;
                    text-align: center;
                    width: 40px;
                    border-radius: 50%;
                    margin: 0 10px;

                    i {
                        cursor: pointer;
                    }
                }
            }
        }

        #search-conversation-bar {
            display: flex;
            flex-direction: row;
            justify-content: center;
            width: 100%;
            background-color: #EDEDED;
            padding: 10px 0;
            border-bottom: solid 1px #bababa;

            input {
                border-radius: 30px;
                border: none;
                width: 85%;
                height: 40px;
                font-family: "Segoe UI";
                font-size: 13px;
                padding: 0 43px;

                &:focus {
                    outline: none;
                }
            }

            i {
                font-size: 20px;
                position: relative;
                left: 30px;
                top: 10px;
            }
        }

        #chats-component-container {
            overflow-y: auto;
            overflow-x: hidden;
        }
    }

    #chat-right-side-content {
        display: flex;
        flex-direction: row;
        justify-content: center;
        width: 100%;
        height: 100%;
        background-color: white;

        #conversation-splash-screen {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding-top: 5em;

            img {
                width: 30em;
                height: 30em;
            }
        }
    }
}

@media (max-width: 500px) {
    #chat-left-side-content {
        min-width: 300px !important;
    }

    #chat-right-side-content {
        min-width: 600px !important;

        #conversation-splash-screen {
            padding-top: 3em !important;
        }
    }

    #chat-main-menu-select {
        left: 6em !important;
    }
}

我要补充的另一个事实是,我也尝试将绝对位置更改为相对位置,但这最后使容器div混乱,显示了空白或内部元素的重新排列。

欢迎任何建议或代码更正。谢谢。

解决方法

设置位置:父级的相对位置(#chat-page-main-container)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-