惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - 蓓蕾心晴

语雀思维导图如何导入到飞书文档 js 实现点击触发复制口令到剪贴板,并跳转 css 背景模糊在真机测试会出现黑色蒙层闪现问题解决 华为鸿蒙手机通过Chrome DevTools调试App内WebView页面 vscode左侧搜索栏搜索时排除不参与搜索的文件夹 css动画已经执行过一次如何再次执行? vscode设置单击选中带连字符的单词 js 判断设备类型包括异形屏 element ui 日期组件实现仅显示日期选择但值包含固定的时间 master远端代码更新,本地拉取不到 css 实现刘海屏样式兼容并支持 js 获取刘海屏高度后动态修改 css 判断在支持某些属性的情况下再添加样式 vue3 provide的值 在回调函数中改变,inject 如何获取到最新的值? vue3如何将 app 全局变量对象变为响应式并监听到某个属性的改变 ResizeObserver loop completed with undelivered notifications. 报错 git 修改本地仓库的远程仓库地址 git突然无法推送到远程仓库 css实现图片等比例完全展示,背景加图片 200%放大虚化 element-ui 使用 el-date-picker 如何限制时间选择范围? element-ui 使用 el-date-picker 如何监听数据变更?
移动端盒子元素实现左右可滑动且竖向页面可滑动
蓓蕾心晴 · 2025-09-09 · via 博客园 - 蓓蕾心晴

需求

移动端盒子元素实现左右可滑动且竖向页面可滑动,即盒子内部元素左右可滑动,上下拖动盒子可以滑动整个页面

代码

关键代码,盒子横向:  overflow-x: auto; 盒子竖向:overflow-y: hidden; 

详细demo 代码如下:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>双向滚动布局</title>
  <style>
    /* 页面整体容器 */
    .page-container {
      display: flex;
      flex-direction: column;
      gap: 20px;
      padding: 15px;
      max-width: 100%;
    }

    /* 横向滚动区域 */
    .horizontal-scroll-section {
      width: 100%;
      height: 236px;
      display: flex;
      align-items: center;
      justify-content: flex-start;  
      touch-action: pan-x pan-y;
      overflow-x: auto;
      overflow-y: hidden;
      -webkit-overflow-scrolling: touch;
      gap: 10px;
      padding: 10px 0;
      background-color: #fff;
      border-radius: 8px;
      box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    }
    .horizontal-scroll-section.visible {
      display: flex;
    }
    .scroll-item {
      flex-shrink: 0;
      width: 132px;
      height: 200px;
      border-radius: 8px;
      object-fit: cover;
      background-color: #f4f4f4;
    }

    /* 新增的竖向滚动区域(与横向区域并列) */
    .vertical-scroll-section {
      width: 100%;
      height: 400px; /* 可自定义高度 */
      overflow-y: auto;
      overflow-x: hidden;
      -webkit-overflow-scrolling: touch;
      padding: 15px;
      background-color: #fff;
      border-radius: 8px;
      box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    }
    .vertical-content {
      display: flex;
      flex-direction: column;
      gap: 15px;
    }
    .vertical-card {
      padding: 12px;
      border-radius: 6px;
      background-color: #f9f9f9;
      border: 1px solid #eee;
    }

    /* 滚动条美化 */
    ::-webkit-scrollbar {
      width: 6px;
      height: 6px;
    }
    ::-webkit-scrollbar-thumb {
      background: #ccc;
      border-radius: 3px;
    }
  </style>
</head>
<body>
  <div class="page-container">
    <h2>横向图片滚动</h2>
    <!-- 横向滚动区域 -->
    <div id="imageScrollContainer" class="horizontal-scroll-section">
      <!-- 图片通过JS动态添加 -->
    </div>

    <h2>竖向内容区域</h2>
    <!-- 竖向滚动区域(独立于横向区域) -->
    <div class="vertical-scroll-section">
      <div class="vertical-content">
        <!-- 竖向内容通过JS动态添加 -->
      </div>
    </div>
  </div>

  <script>
    // ===== 横向滚动数据 =====
    const imageData = [
      "https://picsum.photos/id/1018/300/400",
      "https://picsum.photos/id/1015/300/400",
      "https://picsum.photos/id/1019/300/400",
      "https://picsum.photos/id/1020/300/400",
      "https://picsum.photos/id/1021/300/400",
      "https://picsum.photos/id/1022/300/400"
    ];

    // 初始化横向滚动
    const horizontalContainer = document.getElementById('imageScrollContainer');
    if (imageData.length > 0) {
      horizontalContainer.classList.add('visible');
      imageData.forEach((url, index) => {
        const img = document.createElement('img');
        img.src = url;
        img.className = 'scroll-item';
        img.alt = `图片 ${index + 1}`;
        horizontalContainer.appendChild(img);
      });
    }

    // ===== 竖向滚动数据 =====
    const verticalContent = [
      "这是竖向滚动区域的第一段内容。你可以在这里放置任何HTML元素,比如文字、卡片等。",
      "<div class='vertical-card'><h3>分类标题1</h3><p>这里是分类1的详细描述内容,可以放多行文本。</p></div>",
      "<div class='vertical-card'><h3>分类标题2</h3><p>这里是分类2的详细描述内容,可以放多行文本。</p></div>",
      "<div class='vertical-card'><h3>分类标题3</h3><p>这里是分类3的详细描述内容,可以放多行文本。</p></div>",
      "更多文本内容...",
      "继续添加内容以测试滚动效果...",
      "最后一段内容"
    ];

    // 初始化竖向滚动
    const verticalContentContainer = document.querySelector('.vertical-content');
    verticalContent.forEach((content, index) => {
      const element = document.createElement('div');
      element.innerHTML = content;
      verticalContentContainer.appendChild(element);
    });
  </script>
</body>
</html>