最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue實現(xiàn)雙向滑動輸入條

 更新時間:2022年03月03日 11:10:30   作者:theMuseCatcher  
這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)雙向滑動輸入條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Vue實現(xiàn)雙向滑動輸入條的具體代碼,供大家參考,具體內(nèi)容如下

效果圖如下:

組件支持傳入最小值(min)最大值(max)的范圍,并可設(shè)置初始最小值(initialMin)和初始最大值(initialMax)

拖動滑塊調(diào)整最大最小值,并可點擊輸入條位置,切換數(shù)值

①創(chuàng)建滑動輸入條組件Slider.vue

<template>
? <div class="m-slider" ref="slider">
? ? <div class="u-slider-rail" @click="onClickPoint"></div>
? ? <div class="u-slider-track" @click="onClickPoint" :style="`left: ${left}px; right: auto; width: ${right - left}px;`"></div>
? ? <div class="u-slider-handle" ref="left" @mousedown="onLeftMouseDown" :style="`left: ${left}px; right: auto; transform: translateX(-50%);`"></div>
? ? <div class="u-slider-handle" ref="right" @mousedown="onRightMouseDown" :style="`left: ${right}px; right: auto; transform: translateX(-50%);`"></div>
? </div>
</template>
<script>
export default {
? name: 'Slider',
? props: {
? ? min: { // 滑動輸入條最小值
? ? ? type: Number,
? ? ? default: 0
? ? },
? ? max: { // 滑動輸入條最大值
? ? ? type: Number,
? ? ? default: 100
? ? },
? ? initialMin: { // 滑動輸入條初始最小值,默認(rèn)在最左側(cè)
? ? ? type: Number,
? ? ? default: 0
? ? },
? ? initialMax: { // 滑動輸入條初始最大值,默認(rèn)在最右側(cè)
? ? ? type: Number,
? ? ? default: 100
? ? }
? },
? data () {
? ? return {
? ? ? left: '', // 左滑塊距離滑動條左端的距離
? ? ? right: '', // 右滑動距離滑動條左端的距離
? ? ? width: '' // 滑動輸入條在頁面中的寬度
? ? }
? },
? mounted () {
? ? this.width = this.$refs.slider.clientWidth
? ? this.left = this.initialMin * this.width / this.max
? ? this.right = this.initialMax * this.width / this.max
? },
? computed: {
? ? low () {
? ? ? return Math.round(this.left / this.width * this.max)
? ? },
? ? high () {
? ? ? return Math.round(this.right / this.width * this.max)
? ? }
? },
? watch: {
? ? low (to, from) {
? ? ? this.$emit('lowChange', to) // 左滑塊對應(yīng)數(shù)字回調(diào)
? ? },
? ? high (to, from) {
? ? ? this.$emit('highChange', to) // 右滑塊對應(yīng)數(shù)字回調(diào)
? ? }
? },
? methods: {
? ? onClickPoint (e) { // 點擊滑動條,移動滑塊
? ? ? var offsetLeft = this.$refs.slider.offsetLeft // 滑動條左端距離屏幕左側(cè)的距離
? ? ? var moveX = e.clientX - offsetLeft
? ? ? if (moveX <= this.left) {
? ? ? ? this.left = moveX
? ? ? } else if (this.moveX >= this.right) {
? ? ? ? this.right = this.moveX
? ? ? } else {
? ? ? ? if ((moveX - this.left) < (this.right - moveX)) {
? ? ? ? ? this.left = moveX
? ? ? ? } else {
? ? ? ? ? this.right = moveX
? ? ? ? }
? ? ? }
? ? },
? ? onLeftMouseDown (e) { // 在滾動條上拖動左滑塊
? ? ? // 滑塊水平方向上距離屏幕左端的距離,減去滑塊距離滑動條最左端的距離:e.clientX - this.$refs.left.offsetLeft
? ? ? var offsetLeft = this.$refs.slider.offsetLeft // 滑動條左端距離屏幕左側(cè)的距離
? ? ? document.onmousemove = (e) => {
? ? ? ? var moveX = e.clientX - offsetLeft
? ? ? ? if (moveX < 0) {
? ? ? ? ? this.left = 0
? ? ? ? } else if (moveX >= this.right) {
? ? ? ? ? this.left = this.right
? ? ? ? } else {
? ? ? ? ? this.left = moveX
? ? ? ? }
? ? ? }
? ? ? document.onmouseup = (e) => {
? ? ? ? document.onmousemove = null
? ? ? ? document.onmouseup = null
? ? ? }
? ? },
? ? onRightMouseDown (e) { // 在滾動條上拖動右滑塊
? ? ? /// 滑塊水平方向上距離屏幕左端的距離,減去滑塊距離滑動條最左端的距離:e.clientX - this.$refs.left.offsetLeft
? ? ? var offsetLeft = this.$refs.slider.offsetLeft // 滑動條左端距離屏幕左側(cè)的距離
? ? ? console.log('offsetLeft:', offsetLeft)
? ? ? document.onmousemove = (e) => {
? ? ? ? var moveX = e.clientX - offsetLeft
? ? ? ? if (moveX > this.width) {
? ? ? ? ? this.right = this.width
? ? ? ? } else if (moveX <= this.left) {
? ? ? ? ? this.right = this.left
? ? ? ? } else {
? ? ? ? ? this.right = moveX
? ? ? ? }
? ? ? }
? ? ? document.onmouseup = (e) => {
? ? ? ? document.onmousemove = null
? ? ? ? document.onmouseup = null
? ? ? }
? ? }
? }
}
</script>
<style lang="less" scoped>
.m-slider {
? height: 4px;
? position: relative;
? cursor: pointer;
? touch-action: none;
? &:hover {
? ? .u-slider-rail { // 灰色未選擇滑動條背景色
? ? ? background: #E3E3E3;
? ? }
? ? .u-slider-track { // 藍(lán)色已選擇滑動條背景色
? ? ? background: #1890ff;
? ? }
? }
? .u-slider-rail { // 灰色未選擇滑動條背景色
? ? position: absolute;
? ? z-index: 99;
? ? height: 4px;
? ? width: 100%;
? ? background: #f5f5f5;
? ? border-radius: 2px;
? ? transition: background .3s;
? }
? .u-slider-track { // 藍(lán)色已選擇滑動條背景色
? ? background: #91d5ff;
? ? border-radius: 4px;
? ? position: absolute;
? ? z-index: 99;
? ? height: 4px;
? ? cursor: pointer;
? ? transition: background .3s;
? }
? .u-slider-handle { // 滑塊
? ? position: absolute;
? ? z-index: 999;
? ? width: 14px;
? ? height: 14px;
? ? margin-top: -7px;
? ? box-shadow: 0;
? ? background: #fff;
? ? border: 2px solid #91d5ff;
? ? border-radius: 50%;
? ? cursor: pointer;
? ? transition: border-color .3s,box-shadow .6s,transform .3s cubic-bezier(.18,.89,.32,1.28);
? ? &:hover {
? ? ? border-color: #1890ff;
? ? }
? }
}
</style>

②在要使用的頁面引入

<div class="m-num">
?? ?<p class="u-num">{{ low }}</p>
?? ?<p class="u-num">{{ high }}</p>
</div>
<div class="slider">
?? ?<Slider
?? ??? ?:min="0"
?? ??? ?:max="100"
?? ??? ?:initialMin="low"
?? ??? ?:initialMax="high"
?? ??? ?@lowChange="lowChange"
?? ??? ?@highChange="highChange"
?? ?/>
</div>
import Slider from '@/components/Slider'
components: {
? ? Slider
}
data () {
?? ?return {
?? ??? ?low: 20,
?? ??? ?high: 80
?? ?}
}
lowChange (val) {
? ? ? this.low = val
? ? ? console.log('lowChange:', val)
},
highChange (val) {
? ? ? this.high = val
? ? ? console.log('highChange:', val)
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

饶阳县| 鹤峰县| 洛阳市| 常山县| 车致| 从化市| 清原| 怀集县| 同德县| 新河县| 金溪县| 嘉鱼县| 清新县| 文山县| 临西县| 富民县| 榕江县| 洱源县| 屏东市| 象山县| 扶余县| 锡林浩特市| 阳西县| 进贤县| 克山县| 延长县| 新泰市| 永年县| 大宁县| 名山县| 扎鲁特旗| 改则县| 安徽省| 织金县| 温州市| 湟源县| 舒兰市| 静宁县| 莱阳市| 杭锦旗| 南城县|