前端常用功能16:自定義滾動(dòng)條

來源:https://css-tricks.com/custom-scrollbars-in-webkit/
https://www.cnblogs.com/vicky-li/p/css.html

image.png

效果圖

image.png

一.結(jié)構(gòu)和樣式
1.整體有一個(gè)main,里面最上第一個(gè)div就是scrollbar
上下小圖標(biāo)做成一個(gè),調(diào)整位置


<div class="main">
      <div class="scrollbar"></div>
</div>
.content .main .scrollbar{
    float:right;
    width:16px;
    height:616px;
}

.content .main::-webkit-scrollbar{
    width: 16px;
    height:16px;

}
/*滑塊*/
.content .main::-webkit-scrollbar-thumb{
    background:url(../images/scrollbar_thumb.png) center no-repeat;
}
/*軌道*/
.content .main::-webkit-scrollbar-track{
    background:url(../images/scrollbar_track.png) center no-repeat;
}

.content .main::-webkit-scrollbar-button:vertical:start{
    background:url(../images/up_bg.png) center top no-repeat;
}
.content .main::-webkit-scrollbar-button:vertical:end{
    background:url(../images/up_bg.png) center bottom no-repeat;
}

滾動(dòng)條需要建立在表格內(nèi)容父級(jí)的div里,如下圖

綠色背景是表格本身寬高,粉色是綠色table父級(jí)div,滾動(dòng)條需要建在父級(jí)div里,和table是同級(jí)


image.png

自定義滾動(dòng)條

http://www.itdecent.cn/p/da5d52630f36

image.png

image.png

image.png

tbody單獨(dú)滾動(dòng)的代碼

效果圖:
代碼有缺陷問題,主要思想是把thead,tbody變成display:block,給tbody加高度,加滾動(dòng)
單元格加padding,但是會(huì)出現(xiàn)如下情況,文字不一致就不行


image.png
.chart{
padding:20px;
width: 100%;
}
.table{
width: 100%;
color:#777777;
font-size:14px;
text-align: center;
 border-collapse:collapse;
 border-spacing:0;
}
.table thead,.table tbody,.table tbody tr{
display: block;
max-width:573px;
width:100%;
}
.table tbody{
height: 170px;
overflow-y: auto;
}
.table thead th,.table tbody>tr>td{
width: 25%;
height:30px;
border:1px solid #e2e2e2;
vertical-align: middle;
}
.table tbody>tr>td{
 padding:0 10px;   
}
.table thead th{
padding:0 40px;
}
.table th{
font-weight: bold;
background:#fafafa;
}

.scroll{
width: 100%;
height: 100%;
overflow: scroll;
}
.scrollbar{
width: 6px;
height:100%;
position:absolute;
z-index:50;
top:20px;
right:20px;
float:right;
}
.table tbody::-webkit-scrollbar{
width: 6px;
background-color:#fafafa;
}
.table tbody::-webkit-scrollbar-track{
background-color:#fafafa;    
}
.table tbody::-webkit-scrollbar-thumb{
background-color:green;    
}
<div class="chart">
<table class="table table_1">
<thead class="table_header">
<th>訂單編號(hào)</th>
<th>工件物碼</th>
<th>工步名稱</th>
<th>狀態(tài)</th>
</thead>
<tbody id="table_data">
</tbody>
</table>       
</div>

正確方法

來源:https://www.cnblogs.com/wangEddy/p/8074535.html

image.png
# [CSS 設(shè)置table下tbody滾動(dòng)條](https://www.cnblogs.com/wangEddy/p/8074535.html)

table tbody {

    display:block;

    height:195px;

    overflow-y:scroll;

}

table thead, tbody tr {

    display:table;//仍有伸縮性

    width:100%;

    table-layout:fixed;

}

table thead {

    width: calc( 100% - 1em )

}

css layout屬性

image.png

image.png

image.png

正確效果如下

image.png

解法

1.整體tbody display:block, tr,thead,display:table,讓tbody整體可以滾動(dòng)

  1. tr,thead,display:table具有伸縮性
    3.tbody固定高度,overflow-y:auto
    4.tr,thead width:100% table-layout:fixed;固定寬度跟著內(nèi)容走
    5.滾動(dòng)條會(huì)壓縮tbody的寬度,用jq寫一個(gè)變量+寬度,內(nèi)容超出時(shí),讓tbody自動(dòng)加寬和thead一樣寬。這里注意用parseInt($("tr").css("width"));來解析數(shù)字,去掉px,記住變量加減乘除,賦屬性給變量,要保持字符串一致性。
    6.滾動(dòng)條直接加在tbody上

代碼

//html
<table class="table table_1">
<thead class="table_header">
<th>訂單編號(hào)</th>
<th>工件物碼</th>
<th>工步名稱</th>
<th>狀態(tài)</th>
</thead>
<tbody id="table_data">
</tbody>
</table>   

//css
.table{
width: 100%;
color:#777777;
font-size:14px;
text-align: center;
 border-collapse:collapse;
 border-spacing:0;
}
.table thead,.table tbody tr{
display:table;
width:98%;
table-layout:fixed;
}

.table tbody{
width: 100%;
height: 170px;
display:block;
overflow-y: auto;
}
.table thead th,.table tbody tr td{
height:30px;
border:1px solid #e2e2e2;
vertical-align: middle;
}

.table th{
font-weight: bold;
background:#fafafa;
}

.table tbody::-webkit-scrollbar{
width: 6px;
background-color:#fafafa;
}
.table tbody::-webkit-scrollbar-track{
background-color:#fafafa; 
}
.table tbody::-webkit-scrollbar-thumb{
background-color:green;
}

//js
$(function(){ 
"use strict";
   var rowCount=$("#table_data").find("tr").length;
   var x=parseInt($("tr").css("width"));
   var y= x + 1 + "px";
        if(rowCount > 5){
              $("tr").css("width",y);           
    }
});   
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 參考鏈接: web自動(dòng)化測(cè)試教案:http://www.cnblogs.com/zidonghua/p/74300...
    永杰gg閱讀 1,237評(píng)論 0 0
  • 因?yàn)閷懗鰜淼哪J(rèn)樣式被測(cè)試MM說太丑了,所以上網(wǎng)找了一些資料,通過css來修改滾動(dòng)條樣式。百度了挺多,最中意的還是...
    腦袋空空也閱讀 1,136評(píng)論 0 0
  • 動(dòng)態(tài)圖片按鈕做完功能以后,發(fā)現(xiàn)在第一次切換圖片時(shí),會(huì)發(fā)現(xiàn)圖片有一個(gè)非??斓拈W爍,這個(gè)閃爍會(huì)造成一次 不佳的用...
    用戶400826閱讀 200評(píng)論 0 0
  • 瀏覽器與服務(wù)器的基本概念 瀏覽器(安裝在電腦里面的一個(gè)軟件) 作用: ①將網(wǎng)頁(yè)內(nèi)容渲染呈現(xiàn)給用戶查看。 ②讓用戶通...
    云還灬閱讀 1,281評(píng)論 0 0
  • 概述 相信大家在寫css屬性的時(shí)候,會(huì)遇到一些問題,比如說:垂直對(duì)齊,垂直居中,背景漸變動(dòng)畫,表格寬度自適應(yīng),模糊...
    精神蛙閱讀 271評(píng)論 0 1

友情鏈接更多精彩內(nèi)容