Vue實現(xiàn)固定定位圖標(biāo)滑動隱藏效果
寫在前面
移動端頁面,有時候會出現(xiàn)一些固定定位在底部圖標(biāo),比如購物車等。這時候如果添加一個滑動頁面,圖標(biāo)透明度變低,同時 移動到屏幕邊進行隱藏,效果如下。

所用原理
監(jiān)聽滑動事件,每次進行滑動時,觸發(fā)動畫,添加定時器,1.4s后顯示該圖標(biāo)。具體代碼如下:
<template>
<section class="fixed-icon"
:style="{ bottom: bottom + 'rem' }"
:class="[ !transition ? 'fixed-transition' : '']"
@click="event">
<slot></slot>
</section>
</template>
<script>
export default {
name: 'fixedIcon',
props: {
bottom: { // 改圖標(biāo)距離底部距離 單位 rem
type: Number,
default: 3,
},
},
data () {
return {
transition: true, // 是否觸發(fā)動畫
timer: null, // 定時器
};
},
methods: {
event() {
this.$emit('clickEvent'); // 綁定點擊圖表時間
},
handleScroll () { // 每次滑動都會執(zhí)行函數(shù)
this.transition = false;
if (this.timer) { // 判斷是否已存在定時器
clearTimeout(this.timer);
}
this.timer = setTimeout(() => { // 創(chuàng)建定時器,1.4s后圖標(biāo)回歸原位置
this.transition = true;
}, 1400);
}
},
mounted () {
window.addEventListener('scroll', this.handleScroll); // 監(jiān)聽頁面滑動
}
};
</script>
<style scoped lang="scss">
/*@media only screen and (min-width:750px){html{font-size:20px}} */
.fixed-icon{
position: fixed;
z-index: 1100;
right: 1.7rem;
display: flex;
justify-content: center;
align-items: center;
height: 4.1rem;
width: 4.1rem;
border-radius: 50%;
background-color: rgba(128, 128, 128, 0.8);
transition: 0.7s ease-in-out;
}
.fixed-transition{
right: -2.05rem;
opacity: 0.4;
transition: 1s ease-in-out;
}
</style>
引入代碼如下:
<template>
<section class="content">
<fixed-icon :bottom="3" @clickEvent="chat">
<i class="icon-chat"></i>
</fixed-icon>
</section>
</template>
<script>
import fixedIcon from './components/fixedIcon.vue';
export default {
name: 'test',
components: {
fixedIcon
},
data () {
return {
};
},
methods: {
chat() { // 圖標(biāo)點擊事件
console.log('你好');
},
},
mounted() {
document.title = 'Vue制作固定定位圖標(biāo)滑動隱藏效果';
},
};
</script>
<style scoped lang="scss">
.content{
height: 200vh;
}
.icon-chat{
width: 2rem;
height: 1.9rem;
background: url('http://pfpdwbdfy.bkt.clouddn.com/image/test/fixedIconTranstion/wechat.png') no-repeat;
background-size: 2rem 1.9rem;
}
</style>
總結(jié)
以上所述是小編給大家介紹的Vue實現(xiàn)固定定位圖標(biāo)滑動隱藏效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
詳解Vue+axios+Node+express實現(xiàn)文件上傳(用戶頭像上傳)
這篇文章主要介紹了詳解Vue+axios+Node+express實現(xiàn)文件上傳(用戶頭像上傳),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
maptalks+three.js+vue webpack實現(xiàn)二維地圖上貼三維模型操作
這篇文章主要介紹了maptalks+three.js+vue webpack實現(xiàn)二維地圖上貼三維模型操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
詳解Vue前端生產(chǎn)環(huán)境發(fā)布配置實戰(zhàn)篇
這篇文章主要介紹了詳解Vue前端生產(chǎn)環(huán)境發(fā)布配置實戰(zhàn)篇,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
Vue中使用echarts實現(xiàn)繪制人體動態(tài)圖
這篇文章主要為大家詳細(xì)介紹了Vue中如何使用echarts實現(xiàn)繪制人體動態(tài)圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
vue-cli 3.0 自定義vue.config.js文件,多頁構(gòu)建的方法
今天小編就為大家分享一篇vue-cli 3.0 自定義vue.config.js文件,多頁構(gòu)建的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09

