diff --git a/index.html b/index.html index 4314a88..c012a27 100644 --- a/index.html +++ b/index.html @@ -278,6 +278,7 @@

mobile-select demo

{ data: numArr }, { data: numArr }, { data: numArr }, + { data: numArr }, { data: numArr } ], position: [0, 1, 0, 1, 0], diff --git a/src/ms-core.ts b/src/ms-core.ts index 6d74da1..0426ed7 100644 --- a/src/ms-core.ts +++ b/src/ms-core.ts @@ -4,7 +4,7 @@ import { CascadeData, OptionData } from "./types"; -import { checkIsPC } from "./utils/tools"; +import { checkIsPC, getFloorFloatStr } from "./utils/tools"; import "./style/mobile-select.less"; export default class MobileSelect { @@ -614,7 +614,8 @@ export default class MobileSelect { } fixRowStyle(): void { - // 自定义列宽度比例 用width不用flex的原因是可以做transition过渡 + // 自定义列宽度比例 + // 用width而不用flex的原因: flex虽然可以从num1到num2做transition宽度过渡, 但新增一列时,存量列的宽度无过渡效果 if ( this.initColWidth.length && this.initColWidth.length === this.wheelList.length @@ -622,13 +623,13 @@ export default class MobileSelect { const widthSum = this.initColWidth.reduce((cur, pre) => cur + pre, 0); this.initColWidth.forEach((item, index) => { this.wheelList[index].style.width = - ((item / widthSum) * 100).toFixed(2) + "%"; + getFloorFloatStr(item / widthSum) + "%"; }); return; } - const width = (100 / this.wheelList.length).toFixed(2); + const itemWidthStyle = getFloorFloatStr(100 / this.wheelList.length) + "%"; for (let i = 0; i < this.wheelList.length; i++) { - this.wheelList[i].style.width = width + "%"; + this.wheelList[i].style.width = itemWidthStyle; } } diff --git a/src/utils/tools.ts b/src/utils/tools.ts index 8b351c8..afd9051 100644 --- a/src/utils/tools.ts +++ b/src/utils/tools.ts @@ -1,7 +1,17 @@ -export function checkIsPC() { +export function checkIsPC(): boolean { return !navigator.userAgent .toLowerCase() .match( /ipad|iphone os|midp|rv:1.2.3.4|ucweb|android|windows ce|windows mobile/ ); } + +/** + * 获取向下取整的小数 + * @param num 待处理的数 e.g. 16.999 + * @param digits 保留位数 2 + * @returns '16.99' + */ +export function getFloorFloatStr(num: number, digits = 2): string { + return (Math.floor(num * 100) / 100).toFixed(digits); +}