重新认识box-sizing

深入理解CSS box-sizing属性
box-sizing属性决定了元素的总宽度和高度如何计算。content-box将边框和内填充包含在内,实际尺寸超出设置的宽高;而border-box则按设定的宽高计算,内容区域会受到内填充和边框的影响。理解这一属性对于前端布局至关重要。

重新认识box-sizing

之前只是知道box-sizing设置成border-box,那么布局的时候,元素的宽高就是我们设置的宽高,省心方便。

今天仔细看了下 box-sizing的定义,有了新的认识

The box-sizing CSS property sets how the total width and height of an element is calculated.

摘自:https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

定义很准确:box-sizing决定了元素在屏幕上占的总宽高如何被计算

  • 是我们设置的宽高 (border-box)
  • 还是要加上 border、padding的宽高 (content-box)

content-box

  • 实际占用的屏幕宽高 要加上border、padding
  • 内容区的宽高不会被挤压

在这里插入图片描述

border-box

  • 实际占用屏幕的宽高就是我们设置的宽高,
  • 内容区的 宽高会被padding margin 挤压

在这里插入图片描述

类型“PlusWebviewWebviewObject”上不存在属性“capture”。 怎么修改 <template> <view class="webview-container"> <cover-view class="webview-wrapper"> <web-view :src="webviewPath" id="targetWebview" ></web-view> </cover-view> <cover-view class="bottom-action-area"> <cover-view class="capture-btn" @click="generatePdfFromWebview"> <cover-view class="btn-text">生成PDF</cover-view> </cover-view> </cover-view> </view> </template> <script setup lang="ts"> import { onLoad } from '@dcloudio/uni-app'; import { ref } from 'vue'; const webviewPath = ref(''); onLoad((options: any) => { if (options.url) { webviewPath.value = decodeURIComponent(options.url); } }); const generatePdfPreview = () => { // 获取当前webview对象 const pages = getCurrentPages(); const currentPage = pages[pages.length - 1]; const webview = currentPage.$getAppWebview(); // 捕获网页内容并转换为PDF webview.capture((captureResult) => { if (captureResult) { // 生成PDF文件路径 const pdfPath = '_doc/' + Date.now() + '.pdf'; // 将网页截图保存为PDF plus.io.resolveLocalFileSystemURL(pdfPath, (entry) => { entry.createWriter((writer) => { writer.write(captureResult); writer.onwriteend = () => { // 跳转到PDF预览页 if (uni.getSystemInfoSync().platform === 'android') { // 安卓使用Intent唤起系统PDF查看器 plus.runtime.openFile(pdfPath); } else { // iOS使用PDF.js预览 uni.navigateTo({ url: `pages/home/components/Webpreview?pdfPath=${encodeURIComponent(pdfPath)}` }); } }; }); }); } }, { format: 'pdf', // 指定生成PDF格式 quality: 100, // 质量设置 filename: 'webview-content' // 文件名 }); }; </script> <style> .webview-container { position: relative; width: 100%; height: 100vh; overflow: hidden; } .webview-wrapper { height: calc(100vh - 120rpx); width: 100%; overflow: hidden; } .bottom-action-area { position: fixed; bottom: 0; left: 0; right: 0; z-index: 100; background-color: #007aff; padding: 30rpx; padding-bottom: constant(safe-area-inset-bottom); padding-bottom: env(safe-area-inset-bottom); display: flex; justify-content: center; align-items: center; } .capture-btn { width: 100%; height: 90rpx; background-color: #007aff; border-radius: 45rpx; box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.2); display: flex; align-items: center; justify-content: center; } .btn-text { color: #ffffff; font-size: 32rpx; font-weight: 500; } /* PDF预览组件样式 */ .custom-preview { position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 9999; background-color: rgba(0, 0, 0, 0.9); display: flex; flex-direction: column; align-items: center; padding: 0 0 40rpx 0; box-sizing: border-box; } .preview-header { width: 100%; height: 100rpx; background-color: #007aff; color: #ffffff; display: flex; align-items: center; justify-content: center; position: relative; } .title-container { display: flex; align-items: center; justify-content: center; width: 100%; } .preview-title { color: #ffffff; font-size: 34rpx; font-weight: bold; } .close-btn { position: absolute; right: 30rpx; font-size: 40rpx; color: white; } .preview-pdf { width: 100%; height: calc(100% - 100rpx - 120rpx); background-color: white; } .action-buttons { display: flex; width: 100%; justify-content: space-between; padding: 30rpx 5%; margin-top: auto; background-color: #353336; box-sizing: border-box; } .action-btn { flex: 1; height: 90rpx; border-radius: 12rpx; display: flex; align-items: center; justify-content: center; margin: 0 15rpx; color: #ffffff; font-size: 34rpx; font-weight: 500; } .cancel-btn { background-color: #666; } .confirm-btn { background-color: #007aff; } </style>
08-09
<script setup> import {onMounted,ref} from 'vue' const handleTabChange = ()=>{ uni.navigateTo({ url: '/pages/circle/circle' }) } const windowHeight = ref(0) const scale = ref(0) onMounted(()=>{ const windowInfo = uni.getSystemInfo() uni.getSystemInfo({ success: (res) => { // 提取可视区域高度(px),与 getWindowInfo() 的 windowHeight 一致 windowHeight.value = res.windowHeight*2; console.log('可视区域高度(px):', res.windowHeight); scale.value = windowHeight.value/800 console.log("压缩比例",scale.value); }, fail: (err) => { console.error('获取系统信息失败:', err); } }) }) </script> <template> <view class="container"> <!-- 消息通知 --> <view class="message"> <img src="../../static/images/铃铛.png" alt="" style="width: 65rpx;height: 65rpx;"> </view> <!-- 个人信息区 --> <view class="information"> <img src="@/static/images/未登录头像.png" alt="" style="width: 120rpx;height: 120rpx;"> <view class="personal-information"> <view class="name"> <text id="nickname">未登录</text> <text id="compileInformation">编辑资料</text> </view> <view class="id-location"> <text id="id">id:983602377</text> <text id="location">江苏</text> </view> </view> </view> <!-- 个人简介区 --> <view class="introduce"> <view id="show"> <text>个人简介</text> <img src="../../static/images/铅笔.png" alt="" style="width: 30rpx;height: 30rpx;"> </view> <view id="compileIntroduce"> <input type="text" placeholder="分享人生格言,让大家认识你..." > </view> </view> <!-- 兴趣标签 --> <view class="interest-tags"> <view class="title"> <text>兴趣标签</text> <img src="../../static/images/小箭头.png" alt="" style="width: 10rpx;height: 15rpx;"> </view> <view class="tags"> <view class="tags-item">民间故事</view> <view class="tags-item">京剧鉴赏</view> <view class="tags-item">京剧鉴赏</view> <view class="tags-item">京剧鉴赏</view> <view class="tags-item">京剧鉴赏</view> <view class="tags-item">京剧鉴赏</view> <view class="tags-item">京剧鉴赏</view> <view class="tags-item">京剧鉴赏</view> <view class="tags-item">京剧鉴赏</view> <view class="tags-item">京剧鉴赏</view> </view> </view> <!-- 发布的帖子和参加的活动 --> <view class="participation"> <view class="title"> <view> <text>帖子</text> <view style="width: 64rpx;height: 8rpx;border-radius: 20rpx;background: rgba(254, 212, 198, 1);"></view> </view> <view> <text>活动</text> <view style="width: 64rpx;height: 8rpx;border-radius: 20rpx;background: rgba(254, 212, 198, 1);"></view> </view> </view> <view class="content"> <view class="content-list"> <view class="content-item"> <view class="cover"> <img src="../../static/images/活动图片1.png" alt="" style="width: 280rpx; height: 356rpx;"> </view> <view class="message"> <view class="host"> <img src="../../static/images/活动头像1.png" alt="" style="width: 30rpx; height: 30rpx;"> <text class="nickname">风起</text> </view> <view class="count"> <img src="../../static/images/参加人数.png" alt="" style="width:17rpx;height:17rpx;"> <text class="particapant">321</text> </view> </view> </view> </view> </view> </view> <!-- 自定义导航栏 --> <view class="tabbar" :style="{backgroundSize:`${800*scale}rpx ${800*scale}rpx`,backgroundPosition:`-346rpx ${-(windowHeight-145)}rpx`}"> <view class="circle" @click="handleTabChange"> <img src="@/static/tabs/星球未选中.png" alt="" style="width: 50rpx; height: 50rpx; margin-bottom:10rpx;"> <text style="color: rgba(204, 204, 204, 1)">星趣圈</text> </view> <view class="my"> <img src="@/static/tabs/我的选中.png" alt="" style="width: 50rpx; height: 50rpx; margin-bottom:10rpx;"> <text style="color: rgba(255, 204, 184, 1);">我的</text> </view> </view> </view> </template> <style lang="scss"> page{ position:fixed; top:0rpx; left:0rpx; height:100%; width:100%; background-image:url('https://img.js.design/assets/img/68a6c2073f22157da6132401.png'); background-size: cover; background-repeat: no-repeat; background-position: -346rpx 0rpx; } .container{ overflow-y: scroll; overflow-x: scroll; height: 100%; position: relative; // 消息通知 .message{ position:absolute; left:640rpx; top:80rpx; } // 个人信息 .information{ position:absolute; top:147rpx; left:36rpx; width: 457rpx; height:120rpx; display:flex; gap: 34rpx; .personal-information{ display:flex; flex-direction: column; gap:26rpx; .name{ display:flex; align-items: center; gap:19rpx; #nickname{ font-size: 36rpx; font-weight: 700; } #compileInformation{ width: 120rpx; height: 36rpx; background-color: rgba(254, 212, 198, 0.62); border:1rpx solid rgba(128, 128, 128, 1); border-radius: 20rpx; font-size: 20rpx; font-weight: 500; line-height: 36rpx; color: rgba(128, 128, 128, 1); text-align: center; } } .id-location{ display:flex; gap:29rpx; align-items: center; #id{ font-size: 24rpx; font-weight: 500; color: rgba(166, 166, 166, 1); } #location{ width: 96rpx; height: 35rpx; border-radius: 30rpx; background: rgba(254, 212, 198, 0.62); border:1rpx solid rgba(128, 128, 128, 1); border-radius: 20rpx; font-size: 24rpx; font-weight: 500; text-align: center; line-height: 35rpx; color: rgba(128, 128, 128, 1); } } } } // 个人简介 .introduce{ position:absolute; top:300rpx; left:60rpx; display:flex; flex-direction: column; gap:28rpx; #show{ margin-left:4rpx; display:flex; gap:5rpx; width: 131rpx; height: 30rpx; text{ font-size: 24rpx; font-weight: 500; color: rgba(0, 0, 0, 1); } } #compileIntroduce{ input{ font-size: 24rpx; font-weight: 400; background-color: rgba(255, 250, 247, 1); border-radius: 10rpx; padding:10rpx; width: 610rpx;height: 45rpx; } } } // 兴趣标签 .interest-tags{ position:absolute; left:64rpx; top:480rpx; .title{ display:inline-block; height:25rpx; margin-bottom:28rpx; text{ font-size: 24rpx; font-weight: 500; margin-right:17rpx; } img{ vertical-align: bottom; } } .tags{ width:630rpx; display:grid; grid-template-columns: repeat(5,1fr); gap:12rpx; .tags-item{ background-color: rgba(255, 201, 201, 1); width: 101rpx; height: 25rpx; text-align: center; border-radius: 10rpx; font-size: 18rpx; font-weight: 500; line-height:25rpx; color:white; } } } // 参与 .participation{ position:absolute; top:648rpx; width: 100%; box-sizing: border-box; .title{ height:85rpx; width: 670rpx; border-radius: 20rpx; background-color: rgba(255, 255, 255, 1); margin:0 auto; margin-bottom:21rpx; font-size: 28rpx; font-weight: 500; display:flex; gap:91rpx; align-items: center; text-align: center; &>view:first-child{ margin-left:91rpx; } } .content{ width: 670rpx; margin: 0 auto; padding:19rpx 26rpx; border-radius: 20rpx; background-color: rgba(255, 247, 247, 0.7); box-sizing: border-box; margin-bottom:145rpx; .content-list{ display:grid; grid-template-columns: repeat(2,1fr); gap:16rpx; .content-item{ display: flex; flex-direction: column; gap:20rpx; align-items: center; width: 300rpx; height:429rpx; border-radius: 10rpx; background-color: rgba(255, 255, 255, 1); padding:8rpx 10rpx 15rpx 10rpx; box-sizing: border-box; border:1rpx solid red; .cover{ width:280rpx; border:1rpx solid red; } .message{ width: 280rpx; height:30rpx; display:flex; justify-content: space-between; align-items: center; border:1rpx solid red; .host{ display:flex; gap:12rpx; .nickname{ font-size: 14rpx; font-weight: 500; line-height:30rpx; } } .count{ display:flex; gap:7rpx; height:30rpx; align-items: center; margin-right:20rpx; .particapant{ font-size: 11rpx; font-weight: 500; color: rgba(128, 128, 128, 1); } } } } } } } // 导航栏 .tabbar{ position:fixed; bottom:0rpx; width: 100%; height:145rpx; display:flex; justify-content: space-evenly; font-size: 20rpx; font-weight: 400; background:url('https://img.js.design/assets/img/68a6c2073f22157da6132401.png'); .circle{ padding-top:21rpx; display:flex; flex-direction: column; align-items: center; } .my{ padding-top:21rpx; display:flex; flex-direction: column; align-items: center; } } } </style>
08-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值