隐藏“水平”滚动条但仍能滚动

时间:2016-04-04 14:38:01

标签: css

我需要一些帮助来隐藏我的水平滚动条并仍然可以滚动。我使用过webkit但在IE和Firefox中不起作用。我已经看到很多垂直滚动条的帮助,但不适用于水平。有什么帮助吗?

更新: 我创建了一个JSFiddle来显示我的问题。我想隐藏水平滚动条,仍然可以不使用

滚动
 ::-webkit-scrollbar { 
      display: none; 
   }

http://jsfiddle.net/o1xoh9w8/1/

3 个答案:

答案 0 :(得分:1)

以下是您的操作方法,我在Chrome,IE,Firefox,Opera,Safari(Windows)和Edge

中进行了测试
<h1>You can scroll with mouse wheel</h1>

<div id="box">
    <div id="content">
        <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
    </div>
</div>

h1{font-weight:bold;font-size:2em;} /* ignore only for header */
/* *********************** */
div#box{
    height:200px;
    width:300px;     
    overflow:hidden;
    border:1px solid black;
    padding: 10px;
}

div#content{
    height:200px;
    width:326px;
    /*
     * Uncomment to see scrollbar
    width:300px;
    */
    overflow:auto;
}

这是一个JSFiddle:http://jsfiddle.net/JoshMesser/VUSuZ/

积分归JsFiddle的创作者

编辑:

对于垂直,只需改变高度即可。你正在做的是你只是将滚动条推到用户可以看到的外面,所以对它们来说它不存在,而实际上它是隐藏在元素背后的。这是一个基于我最后一个的JS小提琴。您将看到我只是强制p在单行中进行水平滚动,然后增加高度以隐藏滚动条。

http://jsfiddle.net/VUSuZ/575/

答案 1 :(得分:0)

我使用固定高度方法。

注意:该方法仅在特定情况下有用。

#container1{
    height: 50px;

    /* Just for presentation. Can be removed */
    border: 1px solid red;

    /* Hides content outside this container */
    overflow: hidden;
}

#container2{
    /* Height is significantly greater than the height of container#1 to hide
       any possible scroll */
    height: 100px;
    overflow: auto;
    white-space: nowrap;
}
<div id="container1">
  <div id="container2">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam hendrerit, ante laoreet mattis blandit, arcu nisi blandit felis, et molestie justo lacus et sem. Nunc tempor tellus sit amet eleifend tristique. Integer eget condimentum lectus, nec viverra risus. Nullam leo lectus, placerat vitae porta eget, auctor et nisi. Suspendisse feugiat in lacus accumsan tincidunt. Fusce pulvinar accumsan sem sit amet finibus. Curabitur volutpat mi vitae eros mattis congue. In ut sem eu tellus egestas lobortis vitae eu felis. Maecenas sodales, nisl eu bibendum vulputate, neque leo finibus odio, sit amet bibendum libero dolor sed diam. In molestie magna vitae dui vulputate, eu consequat dui ullamcorper. In hac habitasse platea dictumst. Vestibulum pulvinar, mi quis mollis pulvinar, metus justo aliquet arcu, vel venenatis ipsum dolor at sapien. Sed ac odio bibendum, feugiat nibh at, viverra mi. Morbi sem nisi, ultricies non nulla pretium, gravida malesuada neque.
  </div>
</div>

(我的情况是手机屏幕的水平可滚动按钮容器-按钮被粘在上面的块(+边距)上并具有固定的高度)

enter image description here

答案 2 :(得分:0)

我认为您不想使用

::-webkit-scrollbar { 
  display: none; 
}

因为它将隐藏所有滚动条。 隐藏滚动条但仍允许在特定容器中滚动的更好方法是遵循以下示例:

HTML

<div class="container">
  <table>
    <tbody>
      <tr>
         <td>Example</td>
         <td>Example</td>
         <td>Example</td>
         <td>Example</td>
         <td>Example</td>
         <td>Example</td>
         <td>Example</td>
         <td>Example</td>
      </tr>
    </tbody>
  </table>
</div>

CSS

.container {
    overflow-x: auto;
    white-space: nowrap;
}

.container::-webkit-scrollbar {
    display: none;
}