uni-Identify-quality/tm-vuetify/components/tm-sticky/tm-sticky.vue
2023-09-19 15:48:24 +08:00

100 lines
1.6 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view
class="tm-sticky fulled"
:class="[model_val]"
:style="{
top: model_val == 'top' || model_val == 'static' ? `${top_val}px` : 'auto',
bottom: model_val == 'bottom' ? `${top_val}px` : 'auto'
}"
>
<slot></slot>
</view>
</template>
<script>
/**
* 粘性布局
* @property {String|Number} top = [] 当悬浮时的的偏移量当model=bottom时为底部偏移量单位px
* @property {String} model = [static|top|bottom] 固定的位置
* @example <tm-sticky ></tm-sticky>
*/
export default {
name: 'tm-sticky',
props: {
top: {
type: String | Number,
default: 0
},
model: {
type: String,
default: 'static' //static,top,bottom
}
},
destroyed() {
uni.$off('onPageScroll');
},
computed:{
top_val:function(){
return this.top;
},
model_val:function(){
let p = this.model;
// #ifndef H5
if(this.model=='static' && this.istrueStic==false){
return 'top';
}
// #endif
return p;
},
istrueStic:function(){
if(this.nowScrollTop <= this.eleTop){
return true;
}
return false;
}
},
mounted() {
let t= this;
uni.$on('onPageScroll', e => {
t.nowScrollTop = e.scrollTop;
});
this.$nextTick(async function(){
let p = await this.$Querey('.tm-sticky').catch(e=>{})
this.eleTop = p[0].top;
})
},
data() {
return {
nowScrollTop: 0,
eleTop:0
};
}
};
</script>
<style lang="scss" scoped>
.tm-sticky {
&.static {
position: sticky;
position: -webkit-sticky;
}
&.top {
position: fixed;
top: 0;
z-index: 301;
}
&.bottom {
position: fixed;
bottom: 0;
z-index: 301;
}
z-index: 300;
}
</style>