Home  >  Article  >  Web Front-end  >  2 ways to implement CSS to hide scroll bars and scroll content

2 ways to implement CSS to hide scroll bars and scroll content

青灯夜游
青灯夜游Original
2018-09-12 17:47:251808browse

While hiding the scroll bar, you also need to support scrolling. We often encounter this situation in front-end development. The easiest thing to think of is to add an iscroll plug-in, but now CSS can also achieve this function. This chapter will introduce you to 2 methods to implement CSS to hide the scroll bar and scroll the content. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Method 1: Calculate the width of the scroll bar and hide it

html code:

<div class="outer-container">
    <div class="inner-container">
    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. Quisque vitae tincidunt purus. Vivamus feugiat bibendum erat, nec interdum urna porta sed. Nunc lobortis neque orci, ut suscipit nisl congue feugiat. Vivamus feugiat tellus quis cursus sollicitudin. Curabitur dolor massa, dictum ut ipsum in, porttitor pellentesque ante. Aenean egestas cursus tempor. Maecenas semper tortor sit amet egestas cursus. Mauris porttitor quis nisi ut tincidunt. Curabitur adipiscing eleifend nibh. Praesent mauris leo, consequat vitae orci eget, vestibulum bibendum nisi. Aliquam tempus diam ut tortor cursus, eget sodales augue adipiscing. Nulla at dignissim libero.
     </div>
</div>

css code:

.outer - container {
	width: 360 px;height: 200 px;position: relative;overflow: hidden;
}
.inner - container {
	position: absolute;left: 0;top: 0;right: -17 px;bottom: 0;overflow - x: hidden;overflow - y: scroll;
}

Rendering (before scrolling):

2 ways to implement CSS to hide scroll bars and scroll content

Rendering (after scrolling):

2 ways to implement CSS to hide scroll bars and scroll content

This code cleverly moves 17 pixels to the right, which is exactly equal to the width of the scroll bar. This value was obtained by manual debugging. No problem found in chrome and IE.

Method 2: Surrounded by three containers, there is no need to calculate the width of the scroll bar

The code is as follows:

<div class="outer-container">
     <div class="inner-container">
        <div class="content">
           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. Quisque vitae tincidunt purus. Vivamus feugiat bibendum erat, nec interdum urna porta sed. Nunc lobortis neque orci, ut suscipit nisl congue feugiat. Vivamus feugiat tellus quis cursus sollicitudin. Curabitur dolor massa, dictum ut ipsum in, porttitor pellentesque ante. Aenean egestas cursus tempor. Maecenas semper tortor sit amet egestas cursus. Mauris porttitor quis nisi ut tincidunt. Curabitur adipiscing eleifend nibh. Praesent mauris leo, consequat vitae orci eget, vestibulum bibendum nisi. Aliquam tempus diam ut tortor cursus, eget sodales augue adipiscing. Nulla at dignissim libero.
        </div>
     </div>
 </div>
.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;}
.inner-container::-webkit-scrollbar {  display: none;}

Rendering (before scrolling):

2 ways to implement CSS to hide scroll bars and scroll content

Rendering (after scrolling):

2 ways to implement CSS to hide scroll bars and scroll content

The above is the detailed content of 2 ways to implement CSS to hide scroll bars and scroll content. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn