css保留滚动功能的同时隐藏滚动条(兼容Chrome和Safari)

标签: csshtml滚动条   时间:2017-11-03
一个可以滚动内容的div,超出高度隐藏内容,但是要保留滚动效果又不出现滚动条,同时兼容各种浏览器,该如何实现呢?本文章向大家介绍css保留滚动功能的同时隐藏滚动条(兼容Chrome和Safari),需要的朋友可以参考一下。

div隐藏滚动条仍保留滚动效果:

 

方法一:使用css -webkit-scrollbar属性

<style>
.element,
.outer-container {
  width: 200px;
  height: 200px;
}
.outer-container {
  border: 5px solid purple;
  position: relative;
  overflow: hidden;
}
.inner-container {
  position: absolute;
  left: 0;
  overflow-x: hidden;
  overflow-y: scroll;
  padding-right: 150px;
}
.inner-container::-webkit-scrollbar {
  display: none;
}
</style>
<div class="outer-container">
  <div class="inner-container">
    <div class="element">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vehicula quam nibh, eu tristique tellus dignissim quis. Integer condimentum ultrices elit ut mattis. Praesent rhoncus tortor metus, nec pellentesque enim mattis nec. Nulla vitae turpis ut
      dui consectetur pellentesque quis vel est. Curabitur rutrum, mauris ut mollis lobortis, sem est congue lectus, ut sodales nunc leo a libero. Cras quis sapien in mi fringilla tempus condimentum quis velit. Aliquam id aliquam arcu. Morbi tristique
      aliquam rutrum. Duis tincidunt, orci suscipit cursus molestie, purus nisi pharetra dui, tempor dignissim felis turpis in mi. Vivamus ullamcorper arcu sit amet mauris egestas egestas. Vestibulum turpis neque, condimentum a tincidunt quis, molestie
      vel justo. Sed molestie nunc dapibus arcu feugiat, ut sollicitudin metus sagittis. Aliquam a volutpat sem. Quisque id magna ultrices, lobortis dui eget, pretium libero. Curabitur aliquam in ante eu ultricies.
    </div>
  </div>
</div>

在线运行

 

方法二:使用css覆盖滚动条

实例代码如下:

<style>
#subparant{
    overflow:hidden;    
    width: 500px;
    border: 1px rgba(0,0,0,1.00) solid;
}

#parent{
    width: 515px;
    height: 100px;
    overflow-y: auto;
    overflow-x: hidden;
    opacity:10%;
}

#child{
    width:511px;
    background-color:rgba(123,8,10,0.42);
}
</style>
<body>
    <div id="subparant">
        <div id="parent">
            <div id="child">
                123<br/>123<br/>123<br/>123<br/>123<br/>123<br/>123<br/>123<br/>
            </div>
        </div>
     </div>
 </body>

在线运行

原文地址:http://www.manongjc.com/article/2199.html