使用vue3實(shí)現(xiàn)簡(jiǎn)單的滑塊組件
滑塊組件主要是利用鼠標(biāo)事件,讓滑塊跟著鼠標(biāo)跟著x軸動(dòng),效果如下。

創(chuàng)建了一個(gè)名為 CustomSlider 的自定義滑塊組件。它接受 min、max 和 step 作為 props,用于設(shè)置滑塊的取值范圍和步長(zhǎng)。使用 emits 屬性聲明了一個(gè)名為 update:modelValue 的自定義事件,用于向父組件發(fā)送滑塊值的更新。
name: 'CustomSlider',
props: { min: { type: Number, default: 0 },
max: { type: Number, default: 100 },
step: { type: Number, default: 1 } },
emits: ['update:modelValue'],在 setup 函數(shù)中,我們使用了 Vue 3 的組合式 API 來(lái)定義滑塊組件的邏輯。我們使用了 ref 來(lái)創(chuàng)建了 thumbPosition 和 isDragging 兩個(gè)響應(yīng)式變量。thumbPosition 用于控制滑塊的位置,isDragging 用于標(biāo)記是否正在拖動(dòng)滑塊。
通過(guò)計(jì)算屬性 trackWidth,我們根據(jù)最小值和最大值計(jì)算出滑塊軌道的寬度,用于動(dòng)態(tài)設(shè)置樣式。
const thumbPosition = ref('0%');
const isDragging = ref(false);
const trackWidth = computed(() => {
const range = props.max - props.min;
return `${(100 * range) / (props.max - props.min)}%`;
});在 startDrag 方法中,我們監(jiān)聽(tīng)了滑塊的鼠標(biāo)按下事件,并在按下時(shí)開(kāi)始拖動(dòng)操作。在 handleDrag 方法中,我們根據(jù)鼠標(biāo)位置計(jì)算出滑塊的值,并通過(guò) emit 方法觸發(fā) update:modelValue 事件,將滑塊的值發(fā)送給父組件。
const startDrag = (event) => {
isDragging.value = true;
document.addEventListener('mousemove', handleDrag);
document.addEventListener('mouseup', stopDrag);
};
const handleDrag = (event) => {
if (isDragging.value) {
const sliderWidth = event.target.parentNode.offsetWidth;
const offsetX = event.pageX - event.target.parentNode.offsetLeft;
const percentage = (offsetX / sliderWidth) * 100;
const value = (percentage * (props.max - props.min)) / 100 + props.min;
const snappedValue = Math.round(value / props.step) * props.step;
const clampedValue = Math.max(props.min, Math.min(props.max, snappedValue));
thumbPosition.value = `${((clampedValue - props.min) / (props.max - props.min)) * 100}%`;
emit('update:modelValue', clampedValue);
}
};
const stopDrag = () => {
isDragging.value = false;
document.removeEventListener('mousemove', handleDrag);
document.removeEventListener('mouseup', stopDrag);
};在 onMounted 和 onUnmounted 鉤子中,我們分別添加和移除了監(jiān)聽(tīng)鼠標(biāo)抬起事件的事件處理函數(shù),以確保在組件銷(xiāo)毀時(shí)正確清理事件監(jiān)聽(tīng)器。
onMounted(() => {
document.addEventListener('mouseup', stopDrag);
});
onUnmounted(() => {
document.removeEventListener('mouseup', stopDrag);
});完整代碼如下:
<template>
<div class="slider">
<div class="track" :style="{ width: trackWidth }"></div>
<div class="thumb" :style="{ left: thumbPosition }" @mousedown="startDrag"></div>
</div>
</template>
<script>
import { ref, computed, onMounted, onUnmounted } from 'vue';
export default {
name: 'CustomSlider',
props: {
min: {
type: Number,
default: 0
},
max: {
type: Number,
default: 100
},
step: {
type: Number,
default: 1
}
},
emits: ['update:modelValue'],
setup(props, { emit }) {
const thumbPosition = ref('0%');
const isDragging = ref(false);
const trackWidth = computed(() => {
const range = props.max - props.min;
return `${(100 * range) / (props.max - props.min)}%`;
});
const startDrag = (event) => {
isDragging.value = true;
document.addEventListener('mousemove', handleDrag);
document.addEventListener('mouseup', stopDrag);
};
const handleDrag = (event) => {
if (isDragging.value) {
const sliderWidth = event.target.parentNode.offsetWidth;
const offsetX = event.pageX - event.target.parentNode.offsetLeft;
const percentage = (offsetX / sliderWidth) * 100;
const value = (percentage * (props.max - props.min)) / 100 + props.min;
const snappedValue = Math.round(value / props.step) * props.step;
const clampedValue = Math.max(props.min, Math.min(props.max, snappedValue));
thumbPosition.value = `${((clampedValue - props.min) / (props.max - props.min)) * 100}%`;
emit('update:modelValue', clampedValue);
}
};
const stopDrag = () => {
isDragging.value = false;
document.removeEventListener('mousemove', handleDrag);
document.removeEventListener('mouseup', stopDrag);
};
onMounted(() => {
document.addEventListener('mouseup', stopDrag);
});
onUnmounted(() => {
document.removeEventListener('mouseup', stopDrag);
});
return {
thumbPosition,
trackWidth,
startDrag
};
}
};
</script>
<style>
.slider {
position: relative;
width: 100%;
height: 10px;
background-color: #ccc;
}
.track {
position: absolute;
top: 0;
left: 0;
height: 100%;
background-color: #409eff;
}
.thumb {
position: absolute;
top: -5px;
left: 0;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: #409eff;
cursor: pointer;
}
</style>到此這篇關(guān)于使用vue3實(shí)現(xiàn)簡(jiǎn)單的滑塊組件的文章就介紹到這了,更多相關(guān)vue3實(shí)現(xiàn)滑塊組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Hexo已經(jīng)看膩了,來(lái)手把手教你使用VuePress搭建個(gè)人博客
vuepress是尤大大4月12日發(fā)布的一個(gè)全新的基于vue的靜態(tài)網(wǎng)站生成器,實(shí)際上就是一個(gè)vue的spa應(yīng)用,內(nèi)置webpack,可以用來(lái)寫(xiě)文檔。這篇文章給大家介紹了VuePress搭建個(gè)人博客的過(guò)程,感興趣的朋友一起看看吧2018-04-04
vue3?關(guān)于reactive的重置問(wèn)題及解決
這篇文章主要介紹了vue3?關(guān)于reactive的重置問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
vue項(xiàng)目打包部署跨域的實(shí)現(xiàn)步驟
在前端 Vue 項(xiàng)目打包后,如果需要訪(fǎng)問(wèn)另一個(gè)域名下的接口,由于瀏覽器的同源策略限制,會(huì)出現(xiàn)跨域問(wèn)題,本文就介紹一下vue項(xiàng)目打包部署跨域的實(shí)現(xiàn)步驟,感興趣的可以了解一下2023-05-05
在Vue中引入SVG圖標(biāo)的多種實(shí)現(xiàn)方案
SVG圖標(biāo)集錦是一個(gè)豐富的資源庫(kù),包含了多個(gè)知名圖標(biāo)的集合,如bootstrap、feather、iconpark、ikonate和ionicons等,這些圖標(biāo)是為網(wǎng)頁(yè)開(kāi)發(fā)設(shè)計(jì)的,具有高質(zhì)量和高度可定制性,在 Vue 項(xiàng)目中優(yōu)雅地引入 SVG 圖標(biāo)可通過(guò)多種方案實(shí)現(xiàn),下面小編給大家詳細(xì)說(shuō)說(shuō)2025-07-07
vue與vue-i18n結(jié)合實(shí)現(xiàn)后臺(tái)數(shù)據(jù)的多語(yǔ)言切換方法
下面小編就為大家分享一篇vue與vue-i18n結(jié)合實(shí)現(xiàn)后臺(tái)數(shù)據(jù)的多語(yǔ)言切換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vue使用keep-alive后從部分頁(yè)面進(jìn)入不緩存示例詳解
這篇文章主要給大家介紹了關(guān)于vue使用keep-alive后從部分頁(yè)面進(jìn)入不緩存的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-03-03
vue中用H5實(shí)現(xiàn)文件上傳的方法實(shí)例代碼
本篇文章主要介紹了vue中用H5實(shí)現(xiàn)文件上傳的方法實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
vue+animation實(shí)現(xiàn)翻頁(yè)動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了vue+animation實(shí)現(xiàn)翻頁(yè)動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06

