文件
OmniCard/mpuniapp/pages/CSS/box-sizing/box-sizing.uvue

368 行
12 KiB
Plaintext

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<!-- #ifdef APP && !VUE3-VAPOR -->
<scroll-view style="flex: 1">
<!-- #endif -->
<view style="flex-grow: 1;">
<view>
<text class="desc-text">content-box(标准盒模型,默认值)</text>
<text class="help-text">width 只包括内容宽度,padding 和 border 在盒子外部向外扩展</text>
<text class="help-text">设置 width: 100px, padding: 10px, border: 10px</text>
</view>
<view class="uni-common-mt">
<view class="compare-container">
<view class="compare-box" style="box-sizing: content-box;">
<text class="box-label">content-box</text>
<text class="box-info">内容区: 100px</text>
</view>
</view>
<text class="calc-text">实际占用宽度 = 100(内容) + 20(padding) + 20(border) = 140px</text>
</view>
<view class="uni-common-mt">
<text class="help-text">拍平</text>
<view class="compare-container">
<view class="compare-box" style="box-sizing: content-box;" flatten>
<text class="box-label">content-box 拍平</text>
<text class="box-info">内容区: 100px</text>
</view>
</view>
</view>
<view class="uni-common-mt">
<text class="desc-text">border-box</text>
<text class="help-text">width 包括 border + padding + 内容,padding 和 border 在盒子内部向内挤压</text>
<text class="help-text">设置 width: 100px, padding: 10px, border: 10px</text>
</view>
<view class="uni-common-mt">
<view class="compare-container">
<view class="compare-box" style="box-sizing: border-box;">
<text class="box-label">border-box</text>
<text class="box-info">内容区: 60px</text>
</view>
</view>
<text class="calc-text">实际占用宽度 = 100px(width 设置值)</text>
<text class="calc-text">内容宽度 = 100 - 20(padding) - 20(border) = 60px</text>
</view>
<view class="uni-common-mt">
<text class="help-text">拍平</text>
<view class="compare-container">
<view class="compare-box" style="box-sizing: border-box;" flatten>
<text class="box-label">border-box 拍平</text>
<text class="box-info">内容区: 60px</text>
</view>
</view>
</view>
<view class="uni-common-mt">
<text class="title-text">并排对比(左:content-box 140px,右:border-box 100px)</text>
<text class="help-text">设置 width: 100px, padding: 10px, border: 10px</text>
</view>
<view class="uni-common-mt side-by-side">
<view class="compare-box" style="box-sizing: content-box;">
<text class="box-label-small">content-box</text>
</view>
<view class="compare-box" style="box-sizing: border-box; margin-left: 10px;">
<text class="box-label-small">border-box</text>
</view>
</view>
<text class="uni-title-text uni-common-mt">scroll-view 组件</text>
<text class="help-text">设置 width: 100px, padding: 10px, border: 10px</text>
<view class="demo-box">
<scroll-view class="common scroll-view-box-sizing" style="box-sizing: content-box;">
<text class="box-label-small">content-box</text>
</scroll-view>
<scroll-view class="common scroll-view-box-sizing" style="box-sizing: border-box;">
<text class="box-label-small">border-box</text>
</scroll-view>
</view>
<view class="uni-common-mt">
<text class="uni-title-text">setProperty 设置与 getPropertyValue 获取</text>
<text class="help-text">设置 width: 60px, padding: 10px, border: 10px</text>
</view>
<!-- 普通版本 -->
<view class="test-container">
<view class="test-item">
<text class="uni-subtitle-text">view 组件</text>
<text class="uni-info">设置值: {{data.boxSizing}}</text>
<text class="uni-info">获取值: {{data.boxSizingActual}}</text>
<view class="test-box">
<view ref="viewRef" class="common" :style="{ boxSizing: data.boxSizing }">
<text class="common-text font-size-12">view</text>
</view>
</view>
</view>
<view class="test-item">
<text class="uni-subtitle-text">text 组件</text>
<text class="uni-info">设置值: {{data.boxSizing}}</text>
<text class="uni-info">获取值: {{data.boxSizingActualText}}</text>
<view class="test-box">
<text ref="textRef" class="common font-size-12" :style="{ boxSizing: data.boxSizing }">text</text>
</view>
</view>
<view class="test-item">
<text class="uni-subtitle-text">image 组件</text>
<text class="uni-info">设置值: {{data.boxSizing}}</text>
<text class="uni-info">获取值: {{data.boxSizingActualImage}}</text>
<view class="test-box">
<image ref="imageRef" class="common" :style="{ boxSizing: data.boxSizing }" src="/static/test-image/logo.png"></image>
</view>
</view>
</view>
<!-- 拍平版本 -->
<view class="test-container">
<view class="test-item">
<text class="uni-subtitle-text">view 组件拍平</text>
<text class="uni-info">设置值: {{data.boxSizing}}</text>
<text class="uni-info">获取值: {{data.boxSizingActualFlat}}</text>
<view class="test-box">
<view ref="viewRefFlat" class="common" :style="{ boxSizing: data.boxSizing }" flatten>
<text class="common-text font-size-12">view</text>
</view>
</view>
</view>
<view class="test-item">
<text class="uni-subtitle-text">text 组件拍平</text>
<text class="uni-info">设置值: {{data.boxSizing}}</text>
<text class="uni-info">获取值: {{data.boxSizingActualTextFlat}}</text>
<view class="test-box">
<text ref="textRefFlat" class="common font-size-12" :style="{ boxSizing: data.boxSizing }" flatten>text</text>
</view>
</view>
<view class="test-item">
<text class="uni-subtitle-text">image 组件拍平</text>
<text class="uni-info">设置值: {{data.boxSizing}}</text>
<text class="uni-info">获取值: {{data.boxSizingActualImageFlat}}</text>
<view class="test-box">
<image ref="imageRefFlat" class="common" :style="{ boxSizing: data.boxSizing }" flatten src="/static/test-image/logo.png"></image>
</view>
</view>
</view>
<view class="uni-common-mt uni-common-mb">
<text class="uni-tips">第一个枚举值,'' (空字符串) - 空值情况</text>
<enum-data :items="boxSizingEnum" title="box-sizing 枚举值" @change="radioChangeBoxSizing" :compact="true"></enum-data>
<input-data :defaultValue="data.boxSizing" title="box-sizing 自定义值" type="text" @confirm="inputChangeBoxSizing"></input-data>
</view>
<view class="uni-common-mb">
<text>native-view组件: box-sizing: content-box 和 border-box</text>
<text class="help-text">设置 width: 100px, padding: 10px, border: 10px</text>
<view class="demo-box">
<test-native-view class="native-view-box-sizing" style="box-sizing: content-box;"></test-native-view>
<test-native-view class="native-view-box-sizing" style="box-sizing: border-box;"></test-native-view>
</view>
</view>
</view>
<!-- #ifdef APP && !VUE3-VAPOR -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
import { ItemType } from '@/components/enum-data/enum-data-types'
const boxSizingEnum: ItemType[] = [
{ value: 0, name: '' },
{ value: 1, name: 'content-box' },
{ value: 2, name: 'border-box' }
]
const data = reactive({
boxSizing: 'content-box',
boxSizingActual: '',
boxSizingActualText: '',
boxSizingActualImage: '',
boxSizingActualFlat: '',
boxSizingActualTextFlat: '',
boxSizingActualImageFlat: ''
})
const viewRef = ref(null as UniElement | null)
const textRef = ref(null as UniTextElement | null)
const imageRef = ref(null as UniImageElement | null)
const viewRefFlat = ref(null as UniElement | null)
const textRefFlat = ref(null as UniTextElement | null)
const imageRefFlat = ref(null as UniImageElement | null)
const getPropertyValues = () => {
data.boxSizingActual = viewRef.value?.style.getPropertyValue('box-sizing') ?? ''
data.boxSizingActualFlat = viewRefFlat.value?.style.getPropertyValue('box-sizing') ?? ''
data.boxSizingActualText = textRef.value?.style.getPropertyValue('box-sizing') ?? ''
data.boxSizingActualTextFlat = textRefFlat.value?.style.getPropertyValue('box-sizing') ?? ''
data.boxSizingActualImage = imageRef.value?.style.getPropertyValue('box-sizing') ?? ''
data.boxSizingActualImageFlat = imageRefFlat.value?.style.getPropertyValue('box-sizing') ?? ''
}
const changeBoxSizing = (value: string) => {
data.boxSizing = value
viewRef.value?.style.setProperty('box-sizing', value)
viewRefFlat.value?.style.setProperty('box-sizing', value)
textRef.value?.style.setProperty('box-sizing', value)
textRefFlat.value?.style.setProperty('box-sizing', value)
imageRef.value?.style.setProperty('box-sizing', value)
imageRefFlat.value?.style.setProperty('box-sizing', value)
// 使用 nextTick 确保样式已应用后再获取值
nextTick(() => {
getPropertyValues()
})
}
const radioChangeBoxSizing = (index: number) => {
const selectedItem = boxSizingEnum.find((item): boolean => item.value === index)
if (selectedItem != null) {
changeBoxSizing(selectedItem.name)
}
}
const inputChangeBoxSizing = (value: string) => {
changeBoxSizing(value)
}
onReady(() => {
getPropertyValues()
})
defineExpose({
radioChangeBoxSizing,
data
})
</script>
<style>
.desc-text {
font-size: 14px;
color: #333;
font-weight: bold;
line-height: 22px;
}
.title-text {
font-size: 16px;
color: #333;
font-weight: bold;
}
.help-text {
font-size: 12px;
color: #666;
margin-top: 5px;
line-height: 18px;
}
.calc-text {
font-size: 13px;
color: #ff6b6b;
font-weight: bold;
margin-top: 5px;
line-height: 20px;
}
.side-by-side {
flex-direction: row;
align-items: flex-start;
}
.compare-container {
background-color: #f5f5f5;
padding: 10px;
border: 1px dashed #999;
}
.compare-box {
width: 100px;
height: 80px;
padding: 10px;
border: 10px solid cyan;
background-color: #4ecdc4;
justify-content: center;
align-items: center;
}
.box-label {
font-size: 13px;
color: #fff;
font-weight: bold;
text-align: center;
}
.box-label-small {
font-size: 12px;
color: #fff;
font-weight: bold;
text-align: center;
}
.box-info {
font-size: 10px;
color: #fff;
text-align: center;
margin-top: 3px;
}
.common {
width: 60px;
height: 60px;
background-color: cyan;
padding: 10px;
border: 10px solid #d8dde5;
}
.common-text {
width: 50px;
}
.font-size-12{
font-size: 12px;
}
.test-container {
flex-direction: row;
justify-content: space-between;
margin-top: 10px;
}
.test-item {
flex: 1;
margin: 0 5px;
}
.test-box {
width: 100%;
height: 100px;
background-color: gray;
}
.demo-box {
flex-direction: row;
justify-content: space-around;
margin-top: 10px;
}
.scroll-view-box-sizing {
width: 100px;
height: 80px;
padding: 10px;
border: 10px solid cyan;
background-color: #4ecdc4;
}
.native-view-box-sizing {
width: 100px;
height: 80px;
padding: 10px;
border: 10px solid cyan;
background-color: cyan;
}
</style>