实现一个Slider组件,方便用户经过拖动滑块在一个固定区间内进行选择,加强交互细节。css
概述: 在用户手动一些限定数字时,若是采用输入框的形式,会须要提示信息和错误信息来引导用户,这就存在一些冗余操做。因此衍生出Slider组件,方便用户拖动来选定一个值。html
该组件的痛点在于:代码git
<!-- 基础用法 -->
<fat-slider />
<!-- 设置Min、Max以及Step -->
<fat-slider :min="20" :max="40" step="5" />
<!-- 绑定v-model -->
<fat-slider v-model="initValue" />
<!-- 不现实Tool-Tip -->
<fat-slider v-model="value" :show-tooltip="false" />
<!-- disabled 该组件 -->
<fat-slider disabled />
复制代码
代码地址:Github UI-Librarygithub
该组件的实现是基于原生的<input type="range" />
,再经过改写样式以达到上图效果。web
组件的基本结构以下浏览器
<template>
<div :class="['slider-wrap', { 'is-disabled': disabled }]">
<input :class="['slider-inner', { 'is-disabled': disabled }]" :disabled="disabled" :min="min" :max="max" type="range" v-on="$listeners" v-model="rate" v-bind="$attrs" >
</div>
</template>
复制代码
将其从原生的形式bash
变成如下的样式app
基于cross-browser-range-input这篇博文,进行基础样式的修改。ide
为了兼容不一样的游览器,首先利用@mixin
抽离出thumb公共的样式。ui
@mixin thumb-common-style() {
position: relative;
z-index: 3;
border: 2px solid #409eff;
background: #fff;
cursor: grab;
transition: all 0.3s;
}
// 后续能够丰富rect-slider-thumb等类型
@mixin circle-slider-thumb() {
width: 18px;
height: 18px;
border-radius: 50%;
&:active {
cursor: grabbing;
}
}
复制代码
而后适配不一样的浏览器
&::-webkit-slider-thumb {
// thumb居中
margin-top: -6px;
@include circle-slider-thumb();
@include thumb-common-style();
}
&::-moz-range-thumb {
@include circle-slider-thumb();
@include thumb-common-style();
// z-index无效
transform: translateZ(1px);
}
&::-ms-thumb {
@include circle-slider-thumb();
@include thumb-common-style();
}
复制代码
而后以一样的方法来处理track的样式
@mixin common-track {
width: 100%;
height: 6px;
background: #e4e7ed;
border-radius: 3px;
cursor: pointer;
}
复制代码
适配不一样浏览器
&::-webkit-slider-runnable-track {
@include common-track();
}
&::-moz-range-track {
@include common-track();
}
/* 只有ms支持fill-lower、fill-upper */
&::-ms-track {
width: 100%;
height: 6px;
cursor: pointer;
background: transparent;
border-color: transparent;
border-width: 16px 0;
color: transparent;
}
&::-ms-fill-lower {
background: #409eff;
border-radius: 3px;
}
&::-ms-fill-upper {
background: #c4c4c4;
border-radius: 3px;
}
复制代码
此时Slider组件在不一样浏览器下的显示,以下图
Chrome
Firefox
Ie11
此时的Ie11优秀的不得了,不只提供了fill-upper
、fill-lower
还自带tool-tip提示功能。为了让其余浏览器向他靠齐,就须要实现上述两个功能。丰富组件的结构为
<div :class="['slider-wrapper', { 'is-disabled': disabled }]">
<div v-if="!isIE" class="progress" :style="progressStyle"></div>
<input :class="['slider-inner', { 'is-disabled': disabled }]" :disabled="disabled" :min="min" :max="max" type="range" v-on="$listeners" v-model="rate" v-bind="$attrs" >
<span v-if="!isIE && showTooltip" class="tooltip" :style="toolTipPosition">
{{ rate }}</span>
</div>
复制代码
组件中progress
、tooltip
的样式须要经过当前的rate值来进行修改,其规则为
computed: {
isIE() {
return (
!!window.ActiveXObject ||
"ActiveXObject" in window ||
navigator.userAgent.indexOf("Edge") > -1
);
},
progressStyle() {
const { rate, max, min } = this;
return {
width: `${((rate - min) * 100) / (max - min)}%`
};
},
toolTipPosition() {
const { rate, max, min } = this;
const xOffset = 9 - 18 * ((rate - min) / (max - min));
return {
left: `${((rate - min) * 100) / (max - min)}%`,
marginLeft: `${xOffset}px`,
transform: `translateX(-50%)`
};
}
}
复制代码
其中progressStyle
比较好理解,就是当前rate的值占总体的百分比,而toolTipPosition
则是利用
position: absolute;
/* <percentage>s of the width of the containing block */
left: 10%;
/* <percentage>s of the width of the element */
tansform: translateX(-50%);
复制代码
进一步将其封装成Vue的组件,配置其props
、data
export default {
props: {
showTooltip: { type: Boolean, default: true },
disabled: { type: Boolean, default: false },
min: { type: Number, default: 0 },
max: { type: Number, default: 100 },
value: { type: [Number, String] }
},
// 处理v-model
model: {
prop: "value",
event: "sliding"
},
watch: {
// 动态修改
rate(value) {
this.$emit("sliding", Number(value));
},
// 若是不存在初始值的话,以最小值为初始值
value: {
handler(value) {
this.rate = this.value || this.min;
},
immediate: true
}
}
}
复制代码
封装一个Slider组件,兼容<input type="range" />
在不一样浏览器下的样式,以及简化其内部逻辑,方便后续拓展。
原创声明: 该文章为原创文章,转载请注明出处。