代码拉取完成,页面将自动刷新
同步操作将从 清晨de阳光/x-scrollbar 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>x-scrollbar</title>
<!-- markdown -->
<link rel="stylesheet" href="./markdown/highlight/styles/a11y-light.css">
<link rel="stylesheet" href="./markdown/github-markdown.css">
<script src="./markdown/marked.min.js"></script>
<script src="./markdown/highlight/highlight.min.js"></script>
<!-- xscrollbar -->
<link rel="stylesheet" href="./xscrollbar.css">
<script src="./xscrollbar.js"></script>
</head>
<style>
* {
box-sizing: border-box;
}
html,
body {
height: 100%;
}
body {
font-size: 14px;
color: #333;
padding: 0;
margin: 0;
}
.page_conatiner {
height: 100%;
padding: 16px;
width: 80%;
margin: 0 auto;
background-color: rgba(255, 255, 255, 0.99);
box-shadow: 0 0 20px #ddd;
overflow: auto;
}
.page_conatiner>.x-scrollbar__rail-y {
background-color: transparent !important;
}
.page_conatiner>.x-scrollbar__rail-y>.x-scrollbar__thumb-y {
background-image: linear-gradient(120deg, rgb(240, 147, 251) 0%, rgb(245, 87, 108) 100%);
}
.row {
display: flex;
}
.row>* {
flex: 1;
}
.row>*.flexNone {
flex: none;
}
.table {
border-collapse: collapse;
overflow: visible!important;
margin: 0!important;
}
.table td {
min-width: 100px;
height: 30px;
border: 1px solid #ddd;
text-align: center;
color: #aaa;
font-size: 14px;
padding: 0!important;
}
</style>
<body>
<div class="page_conatiner markdown-body"></div>
<textarea id="markdown" style=" display: none; ">
<h1 style="text-align: center;">x-scrollbar</h1>
版本: v2.2.1
作者: 清晨的阳光(QQ:765550360)
许可: MIT
源码: https://gitee.com/null_720_0252/x-scrollbar
## 概述
这是一个自定义滚动条插件.
在现代前端环境下为什么还需要它, 它有什么特点:
* 滚动条可以半透明的悬浮于内容上方(类似于移动端).
* 可以设置仅水平滚动(拨动鼠标滚轮将作用于X轴).
## 安装
### 直接在浏览器中使用
```html
<link rel="stylesheet" href="./xscrollbar.css">
```
```html
<script src="./xscrollbar.js"></script>
```
### 通过npm安装
```bash
npm i x-scrollbar --save
```
> 需要自行引入 ` node_modules/x-scrollbar/xscrollbar.css ` 样式
```js
import XScrollbar from 'x-scrollbar';
```
## 基础示例
```html
<!-- 容器 -->
<div id="container" style="width: 300px; height: 300px; overflow: auto; border: 1px solid #000;">
<!-- 内容 -->
<table></table>
</div>
<script>
let $container = document.getElementById('container');
let xscrollbar = new XScrollbar($container);
</script>
```
### 常用API(与原生无异)
```js
// 读取
$container.scrollLeft
$container.scrollTop
// 设置
$container.scrollLeft = 100
$container.scrollTop = 100
// 监听滚动
$container.addEventListener('scroll', (e) => {
//...
})
```
<div id="base"></div>
## 样式
<div id="style"></div>
## API
### 构造函数
```js
new XScrollbar(dom: HTMLElement, options?: object)
```
### options
| 选项 | 默认值 | 描述 |
| --------------- | ------ | ---------------------------------------------------------------------- |
| autoHide | true | 是否自动隐藏滚动条 |
| disabledScrollY | false | 是否仅水平滚动(拨动鼠标滚轮将作用于X轴) |
| autoUpdate | true | 响应容器和内容大小改变(自动更新滚动条), 否则需要手动调用 update() 方法 |
| preventDefault | true | 是否阻止向上传递滚动事件 |
| smooth | true | 平滑滚动(仅作用于鼠标滚轮, 非触控板) |
### 实例方法
* **update()** 容器或内容大小发生改变时, 更新滚动滚动条的状态, 在 `options.autoUpdate = false` 的情况下使用
* **destroy()** 销毁滚动条实例
## 鼓励
<p style="color:green; text-shadow: 0px 0px 1px;">如果该插件对您有帮助, 就请把你的小星星(Star)送给我吧^_^ , 您的鼓励是我最大的动力!</p>
</textarea>
<div id="base_content">
<div class="row">
<div class="flexNone" style="margin-right: 16px;">
<div id="example_container" style="width: 300px; height: 300px; overflow: auto; border: 1px solid #000;">
<table class="table example_table">
<tbody></tbody>
</table>
</div>
</div>
<div>
<div style="margin-bottom: 16px;"><button type="button" id="new">初始化 !</button></div>
<div style="margin-bottom: 8px;">
<button type="button" id="add_row">[ + ] 添加一行</button>
<button type="button" id="remove_row">[ - ] 删除一行</button>
</div>
<div style="margin-bottom: 16px;">
<button type="button" id="add_col">[ + ] 添加一列</button>
<button type="button" id="remove_col">[ - ] 删除一列</button>
</div>
<div style="margin-bottom: 8px;"><button type="button" id="set_scrollLeft">设置 scrollLeft</button></div>
<div style="margin-bottom: 0px;"><button type="button" id="set_scrollTop">设置 scrollTop</button></div>
</div>
</div>
</div>
<div id="style_content">
<div class="row" style="margin-bottom: 16px;">
<div class="flexNone" style="margin-right: 16px;">
<span>红色:</span>
<style>
#red_container .x-scrollbar__rail-x:hover,
#red_container .x-scrollbar__rail-y:hover,
#red_container .x-scrollbar__rail-x.x-scrollbar__rail-draging,
#red_container .x-scrollbar__rail-y.x-scrollbar__rail-draging {
background-color: #ffd3d3;
}
#red_container .x-scrollbar__thumb-x,
#red_container .x-scrollbar__thumb-y {
background-color: #fd6969;
}
</style>
<div id="red_container" style="width: 300px; height: 300px; overflow: auto; border: 1px solid #000;">
<table class="table example_table">
<tbody></tbody>
</table>
</div>
</div>
<div class="flexNone" style="margin-right: 16px;">
<span>绿色:</span>
<style>
#green_container .x-scrollbar__rail-x:hover,
#green_container .x-scrollbar__rail-y:hover,
#green_container .x-scrollbar__rail-x.x-scrollbar__rail-draging,
#green_container .x-scrollbar__rail-y.x-scrollbar__rail-draging {
background-color: #c8ffd8;
}
#green_container .x-scrollbar__thumb-x,
#green_container .x-scrollbar__thumb-y {
background-color: #44b565;
}
</style>
<div id="green_container" style="width: 300px; height: 300px; overflow: auto; border: 1px solid #000;">
<table class="table example_table">
<tbody></tbody>
</table>
</div>
<script>
new XScrollbar(document.getElementById('green_container'), { autoHide: false });
</script>
</div>
<div class="flexNone" style="margin-right: 16px;">
<span>蓝色:</span>
<style>
#blue_container .x-scrollbar__rail-x:hover,
#blue_container .x-scrollbar__rail-y:hover,
#blue_container .x-scrollbar__rail-x.x-scrollbar__rail-draging,
#blue_container .x-scrollbar__rail-y.x-scrollbar__rail-draging {
background-color: #d1d8ff;
}
#blue_container .x-scrollbar__thumb-x,
#blue_container .x-scrollbar__thumb-y {
background-color: #4459b5;
}
</style>
<div id="blue_container" style="width: 300px; height: 300px; overflow: auto; border: 1px solid #000;">
<table class="table example_table">
<tbody></tbody>
</table>
</div>
</div>
</div>
<div class="row">
<div class="flexNone" style="margin-right: 16px;">
<span>直角:</span>
<style>
#right_container .x-scrollbar__thumb-x,
#right_container .x-scrollbar__thumb-y {
border-radius: 0px;
}
</style>
<div id="right_container" style="width: 300px; height: 300px; overflow: auto; border: 1px solid #000;">
<table class="table example_table">
<tbody></tbody>
</table>
</div>
</div>
<div class="flexNone" style="margin-right: 16px;">
<span>自动隐藏滚动条(默认):</span>
<div id="auto_hide_container" style="width: 300px; height: 300px; overflow: auto; border: 1px solid #000;">
<table class="table example_table">
<tbody></tbody>
</table>
</div>
</div>
<div class="flexNone" style="margin-right: 16px;">
<span>仅水平滚动(拨动鼠标滚轮将作用于X轴):</span>
<div id="only_x_container" style="width: 300px; overflow: auto; border: 1px solid #000;">
<table class="table">
<tr>
<td>1-1</td>
<td>1-2</td>
<td>1-3</td>
<td>1-4</td>
</tr>
<tr>
<td>2-1</td>
<td>2-2</td>
<td>3-3</td>
<td>4-4</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<script>
let $page_conatiner = document.querySelector('.page_conatiner');
let md = document.querySelector('#markdown').value;
$page_conatiner.innerHTML = marked(md, {
highlight: function (code, language) {
language = hljs.getLanguage(language) ? language : 'plaintext';
return hljs.highlight(code, { language }).value;
}
});
document.querySelector('#base').appendChild(document.querySelector('#base_content'));
document.querySelector('#style').appendChild(document.querySelector('#style_content'));
// 初始化页面滚动条
new XScrollbar(document.querySelector('.page_conatiner'));
// 初始化示例表格内容
let tbody_list = document.querySelectorAll('.example_table > tbody');
Array.from(tbody_list).forEach(tbody => {
let example_table_html = '';
Array.from({ length: 15 }).forEach((row, rowIndex) => {
let tds = '';
Array.from({ length: 4 }).forEach((col, colIndex) => {
tds += `<td>${rowIndex + 1} - ${colIndex + 1}</td>`
});
example_table_html += `<tr>${tds}</tr>`;
});
tbody.innerHTML = example_table_html;
});
// 样式
(() => {
new XScrollbar(document.getElementById('red_container'), { autoHide: false });
new XScrollbar(document.getElementById('blue_container'), { autoHide: false });
new XScrollbar(document.getElementById('right_container'), { autoHide: false });
new XScrollbar(document.getElementById('auto_hide_container'));
new XScrollbar(document.getElementById('only_x_container'), { autoHide: false, disabledScrollY: true });
})();
// 基础示例
(() => {
let $example_container = document.querySelector('#example_container');
let $example_table_tbody = document.querySelector('.example_table > tbody');
// 初始化滚动条
document.querySelector('#new').addEventListener('click', (e) => {
new XScrollbar($example_container, { autoHide: false });
});
//添加一行
document.querySelector('#add_row').addEventListener('click', (e) => {
let tr = document.createElement('tr');
let trIndex = $example_table_tbody.children.length;
let tdCount = $example_table_tbody.querySelector('tr').children.length;
tr.innerHTML = Array.from({ length: tdCount }).map((td, tdIndex) => `<td>${trIndex + 1} - ${tdIndex + 1}</td>`).join('');
$example_table_tbody.appendChild(tr);
});
//添加一列
document.querySelector('#add_col').addEventListener('click', (e) => {
let tdIndex = $example_table_tbody.querySelector('tr').children.length;
Array.from($example_table_tbody.children).forEach((tr, trIndex) => {
let td = document.createElement('td');
td.innerText = `${trIndex + 1} - ${tdIndex + 1}`;
tr.appendChild(td);
});
});
//删除一行
document.querySelector('#remove_row').addEventListener('click', (e) => {
if ($example_table_tbody.children.length == 1) return;
let tr = $example_table_tbody.querySelector('tr:last-child');
$example_table_tbody.removeChild(tr);
});
//删除一列
document.querySelector('#remove_col').addEventListener('click', (e) => {
let tdCount = $example_table_tbody.querySelector('tr').children.length;
if (tdCount == 1) return;
let tds = $example_table_tbody.querySelectorAll('td:last-child');
Array.from(tds).forEach(td => td.parentNode.removeChild(td));
});
//设置 ScrollLeft
document.querySelector('#set_scrollLeft').addEventListener('click', (e) => {
let value = window.prompt('请输入要设置的 scrollLeft', $example_container.scrollLeft);
$example_container.scrollLeft = value;
});
//设置 ScrollTop
document.querySelector('#set_scrollTop').addEventListener('click', (e) => {
let value = window.prompt('请输入要设置的 scrollTop', $example_container.scrollTop);
$example_container.scrollTop = value;
});
})();
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。