🏂 ~
スクロールバーをSimpleBarを使用して実装する
2023.04.15
RELATED CATEGORY
TABLE OF CONTENTS
設定
CDN
- https://cdnjs.cloudflare.com/ajax/libs/simplebar/6.2.4/simplebar.min.js
- デモ(CodePen)のバージョンは「v.6.2.4」で使用している。
npm
// 最新版
npm i simplebar
// バージョン指定
npm i simplebar@5.3.6
LibraryのCSS
// --------------------------
// lib | simplebar.css
// --------------------------
[data-simplebar] {
position: relative;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
align-content: flex-start;
align-items: flex-start;
}
.simplebar-wrapper {
overflow: hidden;
width: inherit;
height: inherit;
max-width: inherit;
max-height: inherit;
}
.simplebar-mask {
direction: inherit;
position: absolute;
overflow: hidden;
padding: 0;
margin: 0;
left: 0;
top: 0;
bottom: 0;
right: 0;
width: auto !important;
height: auto !important;
z-index: 0;
}
.simplebar-offset {
direction: inherit !important;
box-sizing: inherit !important;
resize: none !important;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
padding: 0;
margin: 0;
-webkit-overflow-scrolling: touch;
}
.simplebar-content-wrapper {
direction: inherit;
box-sizing: border-box !important;
position: relative;
display: block;
height: 100%; /* Required for horizontal native scrollbar to not appear if parent is taller than natural height */
width: auto;
max-width: 100%; /* Not required for horizontal scroll to trigger */
max-height: 100%; /* Needed for vertical scroll to trigger */
overflow: auto;
scrollbar-width: none;
-ms-overflow-style: none;
}
.simplebar-content-wrapper::-webkit-scrollbar,
.simplebar-hide-scrollbar::-webkit-scrollbar {
display: none;
width: 0;
height: 0;
}
.simplebar-content:before,
.simplebar-content:after {
content: " ";
display: table;
}
.simplebar-placeholder {
max-height: 100%;
max-width: 100%;
width: 100%;
pointer-events: none;
}
.simplebar-height-auto-observer-wrapper {
box-sizing: inherit !important;
height: 100%;
width: 100%;
max-width: 1px;
position: relative;
float: left;
max-height: 1px;
overflow: hidden;
z-index: -1;
padding: 0;
margin: 0;
pointer-events: none;
flex-grow: inherit;
flex-shrink: 0;
flex-basis: 0;
}
.simplebar-height-auto-observer {
box-sizing: inherit;
display: block;
opacity: 0;
position: absolute;
top: 0;
left: 0;
height: 1000%;
width: 1000%;
min-height: 1px;
min-width: 1px;
overflow: hidden;
pointer-events: none;
z-index: -1;
}
.simplebar-track {
z-index: 1;
position: absolute;
right: 0;
bottom: 0;
pointer-events: none;
overflow: hidden;
}
[data-simplebar].simplebar-dragging {
pointer-events: none;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
[data-simplebar].simplebar-dragging .simplebar-content {
pointer-events: none;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
[data-simplebar].simplebar-dragging .simplebar-track {
pointer-events: all;
}
.simplebar-scrollbar {
position: absolute;
left: 0;
right: 0;
min-height: 10px;
}
.simplebar-scrollbar:before {
position: absolute;
content: "";
background: black;
border-radius: 7px;
left: 2px;
right: 2px;
opacity: 0;
transition: opacity 0.2s 0.5s linear;
}
.simplebar-scrollbar.simplebar-visible:before {
opacity: 0.5;
transition-delay: 0s;
transition-duration: 0s;
}
.simplebar-track.simplebar-vertical {
top: 0;
width: 11px;
}
.simplebar-scrollbar:before {
top: 2px;
bottom: 2px;
left: 2px;
right: 2px;
}
.simplebar-track.simplebar-horizontal {
left: 0;
height: 11px;
}
.simplebar-track.simplebar-horizontal .simplebar-scrollbar {
right: auto;
left: 0;
top: 0;
bottom: 0;
min-height: 0;
min-width: 10px;
width: auto;
}
/* Rtl support */
[data-simplebar-direction="rtl"] .simplebar-track.simplebar-vertical {
right: auto;
left: 0;
}
.simplebar-dummy-scrollbar-size {
direction: rtl;
position: fixed;
opacity: 0;
visibility: hidden;
height: 500px;
width: 500px;
overflow-y: hidden;
overflow-x: scroll;
-ms-overflow-style: scrollbar !important;
}
.simplebar-dummy-scrollbar-size > div {
width: 200%;
height: 200%;
margin: 10px 0;
}
.simplebar-hide-scrollbar {
position: fixed;
left: 0;
visibility: hidden;
overflow-y: scroll;
scrollbar-width: none;
-ms-overflow-style: none;
}
共通のJavaScript(npm)
import SimpleBar from "simplebar";
export function SetSimplebar() {
const simplebarList = [];
const simplebarElementList = [...document.querySelectorAll(".js-simplebar")];
if (simplebarElementList && simplebarElementList.length > 0) {
simplebarElementList.forEach((ele) => {
const obj = new SimpleBar(ele);
simplebarList.push(obj);
});
}
}
縦スクロール
お問合せフォームの「個人情報保護」等で使用するケースが多い印象
CodePen
Code
<div class="l-container">
<div class="c-privacy js-simplebar" data-simplebar data-simplebar-auto-hide="false">
<div class="c-privacy__inner">
コンテンツ
</div>
</div>
</div>
// --------------------------
// lib simplebar 上書き
// --------------------------
// スクロールバーのボール
.simplebar-scrollbar {
top: 0px;
background: #707070;
border-radius: 10px;
padding: 1px 0;
overflow: hidden;
}
.simplebar-scrollbar::before {
background: #707070;
width: 100%;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
// /スクロールバーの背景
.simplebar-track {
border-radius: 10px;
background: #e9e9e9;
}
// スクロールバーを常に表示する
.simplebar-scrollbar.simplebar-visible::before {
opacity: 1;
}
// 縦スクロールバーの位置
.simplebar-track.simplebar-vertical {
top: 5%;
right: 16px;
height: 90%;
width: 4px;
}
横スクロール
概念図等で使用することが多い印象
CodePen
Code
<div class="l-container">
<div class="c-horizontal js-simplebar" data-simplebar data-simplebar-auto-hide="false">
<div class="c-horizontal__inner">
コンテンツ
</div>
</div>
</div>
// コンテンツを横並びにする
.c-horizontal__inner {
display: flex;
}
// --------------------------
// lib simplebar 上書き
// --------------------------
// スクロールバーのボールの調整
.simplebar-scrollbar {
top: 0px;
background: #707070;
border-radius: 10px;
padding: 1px 0;
overflow: hidden;
}
.simplebar-scrollbar::before {
background: #707070;
width: 100%;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
// スクロールバーの背景
.simplebar-track {
border-radius: 10px;
background: #e9e9e9;
}
// スクロールバーを常に表示
.simplebar-scrollbar.simplebar-visible::before {
opacity: 1;
}
// 横スクロールバーの調整
.simplebar-track.simplebar-horizontal {
height: 6px;
left: 3%;
width: 94%;
}