You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript动态生成列表项布局异常,求助修复多类对齐问题

修复视频播放状态下分屏布局的对齐问题

我来帮你逐个解决这四个对齐异常,咱们从布局样式和结构入手调整,先给你修改后的完整代码,再逐一解释修复点:

修改后的完整代码

<!DOCTYPE html>
<html>
<head>
    <style>
        /* 默认隐藏分屏容器 */
        #splitScreen {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100vw;
            height: 100vh;
            /* 父容器用flex实现左右分栏 */
            display: flex;
            z-index: 9999; /* 确保在视频上方显示 */
        }

        .left-panel {
            flex: 1;
            background-color: rgba(0,0,0,0.8);
            color: white;
            /* 左面板居中文本 */
            display: flex;
            justify-content: center;
            align-items: center;
            text-align: center;
        }

        .right-panel {
            flex: 1;
            background-color: rgba(0,0,0,0.8);
            color: white;
            /* 右面板整体居中列表 */
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px; /* 避免列表贴边 */
        }

        #dynamicList {
            list-style: none;
            padding: 0;
            margin: 0;
            width: 100%;
            max-width: 400px; /* 限制列表宽度,避免太宽 */
        }

        .list-item {
            display: flex;
            align-items: center; /* 修复文本垂直居中 */
            margin-bottom: 15px;
            padding: 10px;
            background-color: #333;
            border-radius: 4px;
            /* 让列表项内容靠左对齐 */
            justify-content: flex-start;
        }

        .list-item img {
            width: 50px;
            height: 50px;
            margin-right: 15px;
            /* 确保图片靠左且不被压缩 */
            flex-shrink: 0;
        }

        .list-item span {
            flex: 1;
        }
    </style>
</head>
<body>
    <!-- 视频元素(示例) -->
    <video id="videoPlayer" controls src="your-video.mp4"></video>

    <!-- 分屏容器 -->
    <div id="splitScreen">
        <div class="left-panel">
            <span>视频播放中</span>
        </div>
        <div class="right-panel">
            <ul id="dynamicList"></ul>
        </div>
    </div>

    <script>
        const videoPlayer = document.getElementById('videoPlayer');
        const splitScreen = document.getElementById('splitScreen');
        const dynamicList = document.getElementById('dynamicList');

        // 监听键盘事件
        document.addEventListener('keydown', (e) => {
            if (e.keyCode === 77 && splitScreen.style.display !== 'flex') {
                // 显示分屏
                splitScreen.style.display = 'flex';
                // 动态生成列表项
                generateListItems();
            } else if (e.keyCode === 77) {
                // 隐藏分屏
                splitScreen.style.display = 'none';
                // 清空列表
                dynamicList.innerHTML = '';
            }
        });

        function generateListItems() {
            // 示例数据
            const items = [
                { img: 'item1.jpg', text: '列表项1' },
                { img: 'item2.jpg', text: '列表项2' },
                { img: 'item3.jpg', text: '列表项3' }
            ];

            items.forEach(item => {
                const li = document.createElement('li');
                li.className = 'list-item';
                
                const img = document.createElement('img');
                img.src = item.img;
                img.alt = item.text;
                
                const span = document.createElement('span');
                span.textContent = item.text;
                
                li.appendChild(img);
                li.appendChild(span);
                dynamicList.appendChild(li);
            });
        }
    </script>
</body>
</html>

逐个修复问题的说明

1. 整个列表未在右半部分水平垂直居中

  • 给.right-panel添加display: flex,同时设置justify-content: center和align-items: center,让容器内的列表整体在垂直和水平方向居中。
  • 依托父容器的height: 100vh,让右面板自动占满屏幕高度,确保居中效果覆盖整个右半区域。

2. 列表项未从右半部分的起始位置开始显示

  • 移除了ul标签默认的padding-left(设置padding: 0),避免列表整体偏移。
  • 给.right-panel添加padding: 20px,防止列表贴住屏幕边缘,同时给#dynamicList设置width: 100%和max-width,让列表在右面板内自适应且内容靠左对齐。

3. 列表项内的图片未靠左显示

  • 给.list-item设置justify-content: flex-start,让内部元素从左侧开始排列。
  • 给图片添加flex-shrink: 0,防止图片被压缩变形,同时设置margin-right和文本保持合理间距。

4. 列表项内的文本未实现垂直居中

  • 给.list-item设置display: flex和align-items: center,让图片和文本在垂直方向上居中对齐。
  • 文本标签span设置flex: 1,确保文本占据剩余空间,同时保持垂直居中状态。

另外,我还给分屏容器加了z-index: 9999,确保它能显示在视频上方,避免被视频遮挡。你可以根据实际需求调整背景色、间距、列表宽度等样式参数。

内容的提问来源于stack exchange,提问作者kadina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:06:27