javascript - 如何让 div scroll overflow-x 在移动浏览器上工作?

标签 javascript jquery html css

我有这段代码可以在桌面浏览器上运行。我怎样才能在移动浏览器上实现这种行为?基本上我想创建一个将滚动到两侧的菜单。

<style type="text/css">
    #navBar {
        height: 55px;
        width: 80px;
        overflow-x: scroll;
        overflow-y:hidden;
        white-space: nowrap;
    }

    #navBar div {
        display: inline-block;
    }
    </style>


    <div id="navBar">
            akdhbaIDBhbfbhwhfbaibf
            <div style="width: 100px; text-align: center; background-color: red;">
                <img src="" alt="Nav1" />
                <br />
                <span style="font-size: 80%">Nav1</span>
            </div>
            <div style=" width: 100px; text-align: center;">
                <img src="" alt="Nav2" />
                <br />
                <span style="font-size: 80%">Nav2</span>
            </div>
            <div style=" width: 100px; text-align: center; background-color: red;">
                <img src="" alt="Nav3" />
                <br />
                <span style="font-size: 80%">Nav3</span>
            </div>
    </div>

最佳答案

您可以使用 -webkit-overflow-scrolling: touch; 启用原生滚动

<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <style>
    div {
      overflow-y: hidden;
      overflow-x: scroll;
      -webkit-overflow-scrolling: touch;
      width: 150px;
      border: 1px solid grey;
    }

    h1 {
      width: 400px;
    }
    </style>
  </head>
  <body>
    <div>
      <h1>some content here</h1>
    </div>
  </body>
</html>

关于javascript - 如何让 div scroll overflow-x 在移动浏览器上工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11591321/

相关文章:

jquery - 使用 jQuery 从数组中的项目中剥离 HTML 代码

javascript - 如何避免 AJAX 调用中的 ERR_INSUFFICIENT_RESOURCES

html - 试图为消息实现这种样式

c# - ASP MVC - Html.BeginForm 从没有区域的 Controller 的区域内

php - 全局更改 javascript 变量的值

javascript - 将带参数的函数传递给事件处理程序

javascript - 显示内容的替代方法

javascript - 如何在正则表达式中使用变量并操作变量?

javascript - 创建淡出/淡入而不是显示无/阻止

javascript - Django 删除确认