picker显示行数切换为3行,样式修改

This commit is contained in:
yajun 2022-10-25 21:04:50 +08:00
parent f6f3d8e750
commit 5732906583
2 changed files with 556 additions and 466 deletions

View File

@ -1,6 +1,10 @@
import { css } from "lit";
import {css} from 'lit'
export default css`
/* 滚轮被选项透明化,防止遮挡 */
.hasSelected {
opacity: 0;
}
.p-scroll {
position: fixed;
bottom: 0;
@ -19,11 +23,11 @@ export default css`
width: 100%;
height: 100%;
cursor: pointer;
background: rgba(0,0,0,.7);
-webkit-animation: p-scroll-fade-in .3s ease both;
background: rgba(0, 0, 0, 0.7);
-webkit-animation: p-scroll-fade-in 0.3s ease both;
}
.p-scroll.hide .p-select-mask {
-webkit-animation: p-scroll-fade-out .3s ease both;
-webkit-animation: p-scroll-fade-out 0.3s ease both;
}
.p-select-wrap {
position: absolute;
@ -43,48 +47,74 @@ export default css`
left: 0;
right: 0;
z-index: 2;
background: #fff;
box-shadow: 0 -5px 10px rgba(0,0,0,.1);
-webkit-animation: p-scroll-in .3s ease both;
/*background: #fff;*/
box-shadow: 0 -5px 10px rgba(0, 0, 0, 0.1);
-webkit-animation: p-scroll-in 0.3s ease both;
/**样式修改 */
background: linear-gradient(
134.78deg,
#f7f5f7 2.34%,
#fafafa 34.11%,
#e1e4f2 100%
);
}
.p-scroll.hide .p-select-main {
-webkit-animation: p-scroll-out .3s ease both;
-webkit-animation: p-scroll-out 0.3s ease both;
}
@-webkit-keyframes p-scroll-in {
0%{ -webkit-transform: translate3d(0,100%,0);}
100%{ -webkit-transform: translate3d(0,0,0);}
0% {
-webkit-transform: translate3d(0, 100%, 0);
}
100% {
-webkit-transform: translate3d(0, 0, 0);
}
}
@-webkit-keyframes p-scroll-out {
0%{ -webkit-transform: translate3d(0,0,0);}
100%{ -webkit-transform: translate3d(0,100%,0);}
0% {
-webkit-transform: translate3d(0, 0, 0);
}
100% {
-webkit-transform: translate3d(0, 100%, 0);
}
}
@-webkit-keyframes p-scroll-fade-in {
0%{ opacity: 0;}
100%{ opacity: 1;}
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@-webkit-keyframes p-scroll-fade-out {
0%{ opacity: 1;}
100%{ opacity: 0;}
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
.p-select-head {
position: relative;
height: 45px;
/**height: 45px;*/
line-height: 45px;
display: -webkit-flex;
-webkit-align-items: center;
-webkit-justify-content: space-between;
background: #f2f2f2;
/*background: #f2f2f2;*/
z-index: 5;
/**修改 */
height: 106px;
top: 22px;
}
.p-select-head:before {
content: "";
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 1px;
-webkit-transform: scaleY(.5);
background-color: #d5d5d6;
-webkit-transform: scaleY(0.5);
}
.p-select-title {
-webkit-flex: 1;
@ -108,7 +138,7 @@ export default css`
background-color: #fff;
}
.p-select-foot:before {
content: "";
content: '';
position: absolute;
top: 0;
left: 0;
@ -129,7 +159,7 @@ export default css`
text-align: center;
}
.p-select-foot a:before {
content: "";
content: '';
position: absolute;
top: 0;
right: 0;
@ -158,13 +188,16 @@ export default css`
position: relative;
display: -webkit-flex;
-webkit-align-items: center;
height: 170px;
height: 372px;
text-align: center;
/* overflow: hidden; */
-webkit-flex: 1;
justify-content: center;
}
.p-select-col {
height: 100%;
position: relative;
overflow: hidden;
}
.p-select-ul {
margin: 0;
@ -175,42 +208,44 @@ export default css`
.p-select-wheel,
.p-select-line {
position: absolute;
top: 50%;
/* top: 50%; */
top: 124px;
left: 0;
right: 0;
height: 34px;
margin-top: -17px;
height: 124px;
/* margin-top: -17px; */
box-sizing: border-box;
-webkit-transition: all .3s;
-webkit-transition: all 0.3s;
}
.p-select-wheel {
padding: 0;
-webkit-transform-style: preserve-3d;
height: 34px;
height: 124px;
z-index: 1;
margin: 0px;
}
.p-select-list {
position: relative;
top: 68px;
top: 124px;
margin-top: 0;
z-index: 45;
}
.p-select-line {
height: 34px;
z-index: 100;
height: 124px;
/* z-index: 100; */
pointer-events: none;
box-sizing: border-box;
background: linear-gradient( 134.78deg, #f7f5f7 2.34%, #fafafa 34.11%, #e1e4f2 100% );
}
.p-select-line:before,
.p-select-line:after {
content: "";
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
height: 1px;
-webkit-transform: scaleY(.5);
-webkit-transform: scaleY(0.5);
background: #a5a5a5;
}
.p-select-line:after {
@ -238,6 +273,7 @@ export default css`
z-index: 50;
}
/* 选项字体大小及选项栏高度 */
.p-select-ul > li,
.p-select-wheel > li {
white-space: nowrap;
@ -245,8 +281,8 @@ export default css`
text-overflow: ellipsis;
width: 100%;
padding: 0 10px;
line-height: 34px;
font-size: 21px;
line-height: 124px;
font-size: 28px;
box-sizing: border-box;
cursor: pointer;
-webkit-user-select: none;
@ -254,13 +290,13 @@ export default css`
}
.p-select-ul > li {
color: #666;
font-size: 22px;
font-size: 36px;
}
.p-select-wheel > li {
-webkit-backface-visibility: hidden;
position: absolute;
top: 0;
color: #999;
color: rgba(38, 38, 38, 0.45);
}
.p-select-wheel > li:active {
/* opacity: .7;*/
@ -276,7 +312,7 @@ export default css`
margin: 0 15px;
width: 1px;
height: 160px;
-webkit-transform: scaleX(.7) translateZ(200px);
-webkit-transform: scaleX(0.7) translateZ(200px);
background: -webkit-linear-gradient(90deg, #fff, #ccc 15%, #ccc 85%, #fff);
}
@ -285,22 +321,23 @@ export default css`
-webkit-perspective: 1200;
}
.p-select-wrap.p-3d .p-select-list {
height: 34px;
height: 124px;
overflow: hidden;
background-color: #fff;
background-color: transparent;
}
/* 选项栏颜色 */
.p-select-wrap.p-3d .p-select-ul > li,
.p-select-wrap.p-3d .p-select-label {
color: #2a2b2c;
color: #1d98f0;
}
.p-select-wrap.p-3d .p-select-body:before {
content: "";
content: '';
position: absolute;
top: -15px;
bottom: -15px;
left: 0;
right: 0;
background: -webkit-linear-gradient(#fff,rgba(255,255,255,0) 52%,rgba(2555,255,255,0) 48%,#fff);
pointer-events: none;
-webkit-transform: translate3d(0, 0, 120px);
z-index: 5;
@ -312,13 +349,17 @@ export default css`
bottom: 1px;
}
.p-select-wrap.dark .p-select-main,
.p-select-wrap.dark .p-select-body {
background-color: #080808;
}
.p-select-wrap.dark .p-select-body:before {
background: -webkit-linear-gradient(#080808,rgba(8,8,8,0) 52%,rgba(8,8,8,0) 48%,#080808);
background: -webkit-linear-gradient(
#080808,
rgba(8, 8, 8, 0) 52%,
rgba(8, 8, 8, 0) 48%,
#080808
);
}
.p-select-wrap.dark .p-select-head {
background-color: #1c1c1c;
@ -340,7 +381,13 @@ export default css`
background-color: #444;
}
.p-select-wrap.dark .p-select-time-split-1 {
background: -webkit-linear-gradient(90deg,transparent,#333 15%,#333 85%,transparent);
background: -webkit-linear-gradient(
90deg,
transparent,
#333 15%,
#333 85%,
transparent
);
}
.p-select-wrap.dark .p-select-title,
.p-select-wrap.dark .p-select-label,
@ -367,37 +414,38 @@ export default css`
-webkit-transform: none;
width: auto;
}
.p-center .p-select-head{
background-color: #f2f2f2;
}
.p-center .p-select-body {
min-width: 300px;
padding: 0 25px;
box-sizing: border-box;
background-color: #fff;
}
.p-center .p-select-body:before{
background: -webkit-linear-gradient(#f7f7f7,rgba(245,245,245,0) 52%,rgba(245,245,245,0) 48%,#f7f7f7);
/* 样式修改 */
top: 22px;
height: 394px;
}
.p-center .p-select-main {
position: relative;
margin: auto;
bottom: auto;
max-width: 400px;
/**max-width: 400px;*/
border-radius: 10px;
overflow: hidden;
-webkit-animation: p-scroll-fade-in .3s ease both;
-webkit-animation: p-scroll-fade-in 0.3s ease both;
/* 样式修改 */
width: 620px;
height: 522px;
}
.p-scroll.hide .p-center .p-select-main {
-webkit-animation: p-scroll-fade-out .3s ease both;
-webkit-animation: p-scroll-fade-out 0.3s ease both;
}
.p-center .p-select-line {
border-color: #ccc;
}
/*
.p-center .p-select-list {
background-color: #fff;
}
*/
/*center dark*/
.p-center.dark .p-select-wrap.dark {
@ -412,5 +460,27 @@ export default css`
.p-center.dark .p-select-foot {
background-color: #000;
}
span {
font-family: 'OPPOSans';
font-style: normal;
font-weight: 400;
font-size: 28px;
line-height: 30px;
letter-spacing: 1px;
color: #262626;
}
/**图标样式 */
.p-select-cancel-btn {
border-radius: 0px;
position: absolute;
top: 29px;
left: 40px;
}
.p-select-submit-btn {
border-radius: 0px;
position: absolute;
top: 29px;
left: 532px;
}
`

View File

@ -7,6 +7,8 @@ import {
} from 'lit'
import {customElement, property, query} from 'lit/decorators.js'
import datepickerCss from './datepicker.css.js'
import check from './svg/check.svg'
import close_lm from './svg/close_lm.svg'
function extend(..._args: any[]) {
let name, options, /* src, */ copy
@ -145,7 +147,7 @@ export class PickerBase extends LitElement {
}
// 行数、选项栏高度、选项大小?
public rows = 5
public itemHeight: number = 34
public itemHeight: number = 124
public itemSize2d: number = 9
public itemSize3d: number = 9
public scrolltest: any
@ -202,7 +204,6 @@ export class PickerBase extends LitElement {
let idx = (i < 0 ? len + (i % len) : i) % len
value = data[idx].value
display = data[idx].display
if (is3d) {
let deg = 0
let show = 'list-item'
@ -215,6 +216,24 @@ export class PickerBase extends LitElement {
show = 'list-item'
}
}
// 通过translateZ控制显示层级
// 对被选中项添加类名hasSelected用于控制透明度
if(i == selectedIdx){
html +=
'<li data-index="' +
i +
'" data-val="' +
value +
'" style="transform:rotateX(' +
deg +
'deg) translateZ(' +
(_this.itemHeight * _this.rows) / 2 +
'px); display: ' +
show +
'" class="hasSelected">' +
display +
'</li>'
}else {
html +=
'<li data-index="' +
i +
@ -229,6 +248,7 @@ export class PickerBase extends LitElement {
'">' +
display +
'</li>'
}
} else {
let opacity = 1
if (!infinite) {
@ -262,25 +282,21 @@ export class PickerBase extends LitElement {
'">'
html += '<div class="p-select-main">'
html += '<div class="p-select-head">'
if (_this.option.display != 'center') {
html +=
'<a href="javascript:void(0)" class="p-select-cancel-btn">取消</a>'
}
'<img src="'+close_lm+'" class="p-select-cancel-btn"></img>'
if (_this.option.headTitle != '') {
html +=
'<div class="p-select-title">' + _this.option.headTitle + '</div>'
}
if (_this.option.display != 'center') {
html +=
'<a href="javascript:void(0)" class="p-select-submit-btn">确认</a>'
}
'<img src="'+check+'" class="p-select-submit-btn"></img>'
html += '</div>'
html += '<div class="p-select-body">'
html +=
'<div class="p-select-line" ' +
(_this.option.scrollType == '3d'
? 'style="transform: translateZ(' +
((_this.itemHeight * _this.rows) / 2 + 4) +
((_this.itemHeight * _this.rows) / 2 + 2) +
'px)"'
: '') +
'></div>'
@ -331,14 +347,6 @@ export class PickerBase extends LitElement {
html += '</div>'
}
html += '</div>'
if (_this.option.display == 'center') {
html += '<div class="p-select-foot">'
html +=
'<a href="javascript:void(0)" class="p-select-cancel-btn">取消</a>'
html +=
'<a href="javascript:void(0)" class="p-select-submit-btn">确认</a>'
html += '</div>'
}
html += '</div>'
html += '<div class="p-select-mask"></div>'
html += '</div>'
@ -362,7 +370,7 @@ export class PickerBase extends LitElement {
function getVal(
pos: number,
data: itemdate[],
// infinite: boolean,
infinite: boolean,
selectedIdx: number
) {
let len = data.length,
@ -700,7 +708,7 @@ export class PickerBase extends LitElement {
let res = getVal(
this.opt.scrollY,
this.opt.data,
// this.opt.infinite,
this.opt.infinite,
this.opt.selectedIdx
)
_this.rs.result[this.index] = res
@ -790,14 +798,14 @@ export class PickerBase extends LitElement {
createEL(wheels)
let el = _this.shadowRoot!.querySelectorAll('.p-select-item')
for (let i = 0; i < el.length; i++) {
//滚动事件
//滚动
let scroll = new Scroll(el[i], wheels[i], i)
//初始结果
let res = getVal(
scroll.opt.scrollY,
scroll.opt.data,
// scroll.opt.infinite,
scroll.opt.infinite,
scroll.opt.selectedIdx
)
_this.rs.result.push(res)
@ -808,8 +816,8 @@ export class PickerBase extends LitElement {
_this.option.init(_this.rs)
let submitBtn = _this.shadowRoot!.querySelector('.p-select-submit-btn'),
cancelBtn = _this.shadowRoot!.querySelector('.p-select-cancel-btn'),
mask = _this.shadowRoot!.querySelector('.p-select-mask')
cancelBtn = _this.shadowRoot!.querySelector('.p-select-cancel-btn')
// mask = _this.shadowRoot!.querySelector('.p-select-mask')
submitBtn!.addEventListener(
'click',
function () {
@ -842,14 +850,14 @@ export class PickerBase extends LitElement {
},
false
)
mask!.addEventListener(
'click',
function () {
_this.option.cancel()
destroy()
},
false
)
// mask!.addEventListener(
// 'click',
// function () {
// _this.option.cancel()
// destroy()
// },
// false
// )
_this.shadowRoot!.querySelector('.p-scroll')!.addEventListener(
'touchmove',
function (e) {
@ -922,11 +930,11 @@ export class PickerBase extends LitElement {
container: 'body',
scrollType: '3d',
background: 'light',
showLabel: true, //显示label
labelType: 'symbol', //symbol符号 text文字 split分割(需date和time同时存在)
display: 'bottom',
headResult: false,
date: true,
showLabel: true, //是否显示label
labelType: 'symbol', //label样式symbol符号 text文字 split分割(需date和time同时存在)
display: 'center', //显示位置
headResult: false, //头部显示结果信息
date: false,
time: true,
beginYear: new Date().getFullYear() - 100,
endYear: new Date().getFullYear() + 100,
@ -972,13 +980,13 @@ export class PickerBase extends LitElement {
{
infinite: true,
selected: this.hour,
label: '<span>:</span>',
label: '',
data: this.hourData,
},
{
infinite: true,
selected: this.minute,
label: '',
label: '',
data: this.minuteData,
},
]
@ -1019,10 +1027,22 @@ export class PickerBase extends LitElement {
}
} else {
if (this.opt.labelType == 'text') {
let text = ['年', '月', '日', '时', '分', '秒']
for (let i = 0; i < this.wheels.length; i++) {
if (this.wheels.length == 5) {
var text = ['年', '月', '日', '时', '分', '秒']
for (var i = 0; i < this.wheels.length; i++) {
this.wheels[i].label = text[i]
}
} else if (this.wheels.length == 3) {
var text = ['年', '月', '日']
for (var i = 0; i < this.wheels.length; i++) {
this.wheels[i].label = text[i]
}
} else {
var text = ['时', '分']
for (var i = 0; i < this.wheels.length; i++) {
this.wheels[i].label = text[i]
}
}
}
if (this.opt.labelType == 'split' && this.opt.date && this.opt.time) {
for (let i = 0; i < this.wheels.length; i++) {
@ -1044,8 +1064,7 @@ export class PickerBase extends LitElement {
// 判断大小月
public bigSmallMonth(dayObj: any, year: number, month: number) {
let smallMonth =
month == 4 || month == 6 || month == 9 || month == 11
let smallMonth = month == 4 || month == 6 || month == 9 || month == 11
dayObj.appendItem([
{value: '29', display: '29'},
{value: '30', display: '30'},
@ -1175,7 +1194,8 @@ export class PickerBase extends LitElement {
scrollType: this.opt.scrollType,
background: this.opt.background,
display: this.opt.display,
headTitle: this.opt.headResult ? 1 : '',
// headTitle: this.opt.headResult ? 1 : '',
headTitle: '<span>新建闹钟</span>',
container: '.screen',
init: (rs: any) => {
if (this.opt.date) {