vue使用計(jì)算屬性完成動態(tài)滑竿條制作
布局部分:
<div id="slider">
<!-- 主要動畫效果:字體和進(jìn)度條以及表情隨情緒程度百分比變化 -->
<label for="range" :style="{'color':getHappinessColor}">情緒程度: {{val}}%</label>
<!-- 滑動桿的顏色應(yīng)該與預(yù)先設(shè)置好的顏色進(jìn)行綁定,顏色可隨意更改 -->
<!-- 情緒程度的值也應(yīng)該隨val值變動 -->
<input type="range" name="" id="range" min="0" max="100" v-model="val">
<!-- 滑動桿的值應(yīng)該與val綁定,val寫入了計(jì)算屬性,為了與下方滑動桿填充顏色的范圍同步 -->
<div class="slider outer">
<!-- 讓我們label 的寬度 等于們數(shù)據(jù)中心 val 的寬度,這樣就會隨著滑動桿的運(yùn)動帶動label 運(yùn)動,達(dá)到更改表情的效果 -->
<label for="" class="slider inner" :style="{'width':val+'%',
'border-radius':greaterThanFifty}">
<span :style="{'right':getPlacement}">{{getHappiness}}</span>
</label>
</div>
</div>
樣式部分:
*{
padding: 0;
margin: 0;
list-style: none;
}
:root {
/* 全局css變量 */
--yellow: #ffd100;
--orange: #ff6a13;
--darkGray: #53565a;
--midGray: #888b8d;
--white: #fff;
}
*,*::after,*::before{
color: var(--darkGray);
box-sizing: border-box;
}
html,body {
width: 100%;
height: 100%;
}
body{
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: var(--white);
}
#slider{
/* 局部css變量 */
--roundness: 20px;
width: 100%;
max-width: 600px;
outline: 1px dashed red;
padding: 4vh;
/* 網(wǎng)格布局 */
display: grid;
justify-content: stretch;
}
#slider>label{
width: 100%;
font-size: 1.5em;
padding: 0 0 0.5em;
margin: auto;
}
#slider input{
grid-row: 2 / 3;
grid-column: 1 / 2;
width: 100%;
position: relative;
z-index: 1;
opacity: 0;
height: calc(var(--roundness) * 2);
cursor: pointer;
}
#slider .outer{
width: 100%;
height: var(--roundness);
background-color: var(--midGray);
border-radius: var(--roundness);
display: flex;
align-items: center;
align-content: center;
position: relative;
z-index: 0;
margin: auto;
grid-row: 2/3;
grid-column: 1/2;
}
#slider input:focus + .outer {
outline: 1px dashed var(--orange);
}
#slider label.inner {
position: absolute;
width: 100%;
height: 100%;
background: var(--white);
background: linear-gradient(to left, var(--yellow), var(--orange));
border-top-left-radius: var(--roundness) !important;
border-bottom-left-radius: var(--roundness) !important;
position: absolute;
transition: all 0.3s cubic-bezier(0.5, 0.4, 0.2, 1);
text-align: right;
font-size: calc(var(--roundness) * 2);
line-height: 1;
}
#slider label.inner span {
position: absolute;
right: -2px;
top: calc(50% - var(--roundness) * 2);
top: calc(var(--roundness) * -.3);
transition: inherit;
}
Vue 部分:
<script src="./js/vue.js"></script>
<script>
let a = new Vue({
el:"#slider",
data() {
return {
val: 70,
// 關(guān)鍵點(diǎn),同時(shí)用來動態(tài)關(guān)聯(lián) 1:情緒百分比,2:顯示出來的文本表情
}
},
mounted() {
this.val = Math.floor(Math.random() * 101)
},
computed: {
getPlacement: function () {
return `${(-0.009 * ((this.val * -1) + 104))}em`;
// 獲取位置,因?yàn)槭怯?jì)算屬性,相當(dāng)于是與val綁定了,給最下方的span綁定后就等于與上方與val綁定的width,進(jìn)行“綁定”
},
greaterThanFifty: function () {
return this.val > 50 ? `var(--roundness)` : `0`;
// val值大于五十之后,邊框的變化,可以省略或者不綁定,不關(guān)鍵
},
getHappinessColor: function () {
return `rgba(255, ${106 + (103 / 100 * this.val)}, ${(Math.floor(this.val * -1 / 7.692)) + 13}`;
// 獲取顏色的函數(shù),可以隨意更改數(shù)值,不過上方顏色過渡更加自然
},
getHappiness: function () {
let mood;
// 將val值與所有表情“物理綁定”
if (this.val == 0) {
mood = "??"
} else if (this.val < 10) {
mood = "??"
} else if (this.val < 20) {
mood = "??"
} else if (this.val < 30) {
mood = "??"
} else if (this.val < 40) {
mood = "??"
} else if (this.val < 50) {
mood = "??"
} else if (this.val < 60) {
mood = "??"
} else if (this.val < 70) {
mood = "??"
} else if (this.val < 80) {
mood = "??"
} else if (this.val < 90) {
mood = "??"
} else if (this.val < 100) {
mood = "??"
} else if (this.val == 100) {
mood = "??"
}
return mood;
}
}
})
</script>
最終樣式:

到此這篇關(guān)于vue使用計(jì)算屬性完成動態(tài)滑竿條制作的文章就介紹到這了,更多相關(guān)vue使用計(jì)算屬制作動態(tài)滑竿條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue mintui-Loadmore結(jié)合實(shí)現(xiàn)下拉刷新和上拉加載示例
本篇文章主要介紹了vue mintui-Loadmore結(jié)合實(shí)現(xiàn)下拉刷新和上拉加載示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Vant的Tabbar標(biāo)簽欄引入自定義圖標(biāo)方式
這篇文章主要介紹了Vant的Tabbar標(biāo)簽欄引入自定義圖標(biāo)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue實(shí)現(xiàn)星級評價(jià)效果實(shí)例詳解
這篇文章主要介紹了Vue實(shí)現(xiàn)星級評價(jià)效果的實(shí)例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
詳解vue express啟動數(shù)據(jù)服務(wù)
本篇文章主要介紹了vue express啟動數(shù)據(jù)服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
vue-cli開發(fā)環(huán)境實(shí)現(xiàn)跨域請求的方法
本篇文章主要介紹了vue-cli開發(fā)環(huán)境實(shí)現(xiàn)跨域請求的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04

