You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
236 lines
5.3 KiB
236 lines
5.3 KiB
<!--本文件由FirstUI授权予内蒙古晟誉网络科技有限责任公司(手机号:15 148228 1 08,身份证尾号:1R6T39)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
|
|
<template>
|
|
<view class="fui-grid__wrap" :class="[isShow?'fui-grid__wrap-show':'fui-grid__wrap-hidden']">
|
|
<view :id="elemId" ref="fui_grid" class="fui-grid"
|
|
:class="{ 'fui-grid__border': showBorder,'fui-grid__between':between }"
|
|
:style="{ 'border-left-color':borderColor,'border-top-color':borderColor}">
|
|
<slot></slot>
|
|
</view>
|
|
</view>
|
|
</template>
|
|
|
|
<script>
|
|
// #ifdef APP-NVUE
|
|
const dom = uni.requireNativePlugin('dom');
|
|
// #endif
|
|
export default {
|
|
name: "fui-grid",
|
|
emits: ['click'],
|
|
props: {
|
|
// 每行显示个数
|
|
columns: {
|
|
type: Number,
|
|
default: 3
|
|
},
|
|
// 是否显示边框
|
|
showBorder: {
|
|
type: Boolean,
|
|
default: true
|
|
},
|
|
// 边框颜色 仅nvue有效
|
|
borderColor: {
|
|
type: String,
|
|
default: '#EEEEEE'
|
|
},
|
|
// 是否正方形显示,默认为 true
|
|
square: {
|
|
type: Boolean,
|
|
default: true
|
|
},
|
|
// item 项是否两端对齐,仅在宽度无法拉满且影响美观时使用,谨慎使用 v2.0.0+
|
|
between: {
|
|
type: Boolean,
|
|
default: false
|
|
}
|
|
},
|
|
provide() {
|
|
return {
|
|
grid: this
|
|
}
|
|
},
|
|
data() {
|
|
const elemId = `fui_${Math.ceil(Math.random() * 10e5).toString(36)}`
|
|
return {
|
|
elemId: elemId,
|
|
width: 0,
|
|
height: 0,
|
|
isShow: false
|
|
};
|
|
},
|
|
created() {
|
|
this.children = []
|
|
let sys = uni.getSystemInfoSync()
|
|
this.width = (100 / this.columns) + '%'
|
|
if (this.square) {
|
|
this.height = (sys.windowWidth - 1) / this.columns + 'px'
|
|
}
|
|
},
|
|
watch: {
|
|
columns(val) {
|
|
this.$nextTick(() => {
|
|
this.init()
|
|
})
|
|
},
|
|
showBorder(val) {
|
|
this.childChange()
|
|
}
|
|
},
|
|
mounted() {
|
|
this.$nextTick(() => {
|
|
this.init()
|
|
})
|
|
},
|
|
methods: {
|
|
init() {
|
|
setTimeout(() => {
|
|
this.getWidth((width, height) => {
|
|
this.children.forEach((item, index) => {
|
|
item.width = width
|
|
item.height = height
|
|
})
|
|
this.isShow = true
|
|
})
|
|
}, 50)
|
|
},
|
|
childChange() {
|
|
this.children.forEach((item, index) => {
|
|
item.showBorder = this.showBorder
|
|
})
|
|
},
|
|
handleClick(e) {
|
|
this.$emit('click', e)
|
|
},
|
|
getWidth(fn) {
|
|
let isNoSupported = false
|
|
// #ifdef MP-BAIDU || MP-TOUTIAO || MP-QQ || MP-KUAISHOU || MP-JD || MP-360 || MP-LARK
|
|
isNoSupported = true
|
|
// #endif
|
|
|
|
// #ifndef APP-NVUE
|
|
this.width = (100 / this.columns) + '%'
|
|
if (this.square || isNoSupported) {
|
|
uni.createSelectorQuery()
|
|
// #ifndef MP-ALIPAY
|
|
.in(this)
|
|
// #endif
|
|
.select(`#${this.elemId}`)
|
|
.boundingClientRect()
|
|
.exec(ret => {
|
|
//使用 parseInt 不使用 Number 避免 部分android机 换行(小数渲染有兼容性问题) ,但是可能会出现误差无法铺满
|
|
let width = (ret[0].width - 1) / this.columns
|
|
this.width = parseInt(width * 10) / 10 + 'px'
|
|
if (this.square)
|
|
this.height = width;
|
|
if (isNoSupported)
|
|
this.width = width;
|
|
fn(this.width, this.height)
|
|
})
|
|
} else {
|
|
fn(this.width, this.height)
|
|
}
|
|
// #endif
|
|
// #ifdef APP-NVUE
|
|
dom.getComponentRect(this.$refs['fui_grid'], (ret) => {
|
|
//使用 parseInt 不使用 Number 可避免 部分android机 换行,但是可能会出现误差无法铺满
|
|
// 注:以真机测试为准,如果还有换行,则只能不保留小数
|
|
let width = (ret.size.width - 1) / this.columns
|
|
this.width = parseInt(width * 10) / 10 + 'px'
|
|
if (this.square) {
|
|
this.height = this.width;
|
|
}
|
|
fn(this.width, this.height)
|
|
})
|
|
// #endif
|
|
}
|
|
}
|
|
|
|
}
|
|
</script>
|
|
|
|
<style scoped>
|
|
.fui-grid__wrap {
|
|
/* #ifndef APP-NVUE */
|
|
width: 100%;
|
|
display: flex;
|
|
box-sizing: border-box;
|
|
/* #endif */
|
|
flex: 1;
|
|
flex-direction: column;
|
|
|
|
/* 以下css可能导致数据过多nvue端直接白屏不显示 */
|
|
/* #ifndef APP-NVUE */
|
|
transition-property: opacity;
|
|
transition-duration: .2s;
|
|
transition-timing-function: ease-in-out;
|
|
/* #endif */
|
|
}
|
|
|
|
.fui-grid__wrap-hidden {
|
|
opacity: 0;
|
|
}
|
|
|
|
.fui-grid__wrap-show {
|
|
opacity: 1 !important;
|
|
}
|
|
|
|
.fui-grid {
|
|
/* #ifndef APP-NVUE */
|
|
display: flex;
|
|
width: 100%;
|
|
/* #endif */
|
|
flex-direction: row;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
/* #ifndef APP-VUE || H5 || MP-WEIXIN */
|
|
.fui-grid__between {
|
|
justify-content: space-between;
|
|
}
|
|
/* #endif */
|
|
|
|
.fui-grid__border {
|
|
position: relative;
|
|
/* #ifdef APP-NVUE */
|
|
border-left-style: solid;
|
|
border-left-width: 0.5px;
|
|
border-top-style: solid;
|
|
border-top-width: 0.5px;
|
|
/* #endif */
|
|
/* #ifndef APP-NVUE */
|
|
z-index: 1;
|
|
border-left: 0;
|
|
/* #endif */
|
|
}
|
|
|
|
/* #ifndef APP-NVUE */
|
|
.fui-grid__border::before {
|
|
content: ' ';
|
|
position: absolute;
|
|
left: 0;
|
|
top: 0;
|
|
width: 100%;
|
|
height: 1px;
|
|
border-top: 1px solid var(--fui-color-border, #EEEEEE);
|
|
-webkit-transform-origin: 0 0;
|
|
transform-origin: 0 0;
|
|
-webkit-transform: scaleY(0.5);
|
|
transform: scaleY(0.5);
|
|
z-index: 1;
|
|
}
|
|
|
|
.fui-grid__border::after {
|
|
content: ' ';
|
|
position: absolute;
|
|
left: 0;
|
|
top: 0;
|
|
width: 1px;
|
|
height: 100%;
|
|
border-left: 1px solid var(--fui-color-border, #EEEEEE);
|
|
-webkit-transform-origin: 0 0;
|
|
transform-origin: 0 0;
|
|
-webkit-transform: scaleX(0.5);
|
|
transform: scaleX(0.5);
|
|
}
|
|
|
|
/* #endif */
|
|
</style>
|