学习咸虾米壁纸项目
灵活使用flex布局
.notice{
width:690rpx;
height:80rpx;
line-height:80rpx;
background-color: #f9f9f9;
margin:0 auto;
border-radius:80rpx;
display:flex;
.left{
width:140rpx;
}
.center{
flex:1
}
.right{
width:70rpx;
}
}
这样实现了不使用定位和浮动,则使.left,.right,.center分成三部分
若想让文字呈现在一行中显示不全用省略号代替,则可使用
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
使两部分两端对齐是
display:flex;
justify-content: space-between;
align-items: center;
做出如上布局效果图,可采用
.userLayout{
.userInfo{
display:flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
}
平均分配:
justify-content: space-around
实现上述壁纸信息和叉号的布局
<view class="popHeader">
<view></view>
<view class="title">壁纸信息</view>
<view class="close">
<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
</view>
</view>
.popHeader{
display: flex;
align-items: center;
justify-content: space-between;
}
一个组件显示不同的东西
<view class="select">
<common-title>
<template #name>
每日推荐
</template>
</common-title>
<view class="content">
<scroll-view scroll-x>
<view class="box" v-for="item in 8">
<image src="../../common/images/preview_small.webp" mode="aspectFill"></image>
</view>
</scroll-view>
</view>
</view>
<view class="theme">
<common-title>
<template #name>
专题精选
</template>
</common-title>
</view>
网格布局
display: grid;
gap:15rpx;
grid-template-columns: repeat(3,1fr);
想实现自适应网格布局
.layout{
width:80%;
display: grid;
gap:15rpx;
grid-template-columns: repeat(auto-fill,min(260px,1fr);
}
B站布局
.layout .box1{
grid-row:1/3;
grid-column:1/3;
}
磨砂半透明效果
backdrop-filter: blur(20rpx);
background-color: rgba(0,0,0,0.2);
最小文字缩放
在页面开发中最小文字是12px,当文字需要再小的时候,则需要利用transform进行缩放
font-size: 22rpx;
transform: scale(0.8);
transform-origin: left top;
条件编译
若想实现在不同平台上展示不同的东西,则使用条件编译,比如想实现在小程序上展现联系客服,在不是小程序的平台上展示呼叫电话
<!-- #ifdef MP -->
<button open-type="contact">联系客服</button>
<!-- #endif -->
<!-- #ifndef MP -->
<button open-type="contact">拨打电话</button>
<!-- #endif -->
宽度随内容自适应
width:fit-content;
利用vue2写弹出层
<uni-popup ref="infoPopup">
<view class="infoPopup">
<view class="title">壁纸</view>
<view class="close">
<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
</view>
</view>
</uni-popup>
<script setup>
import {ref} from 'vue';
const infoPopup = ref(null);
//点击info弹窗
const clickInfo = ()=>{
infoPopup.value.open();
}
</script>
布局标签
.tabs{
display: flex;
flex-wrap: wrap;
.tab{
border:1px solid $brand-theme-color;
color:$brand-theme-color;
font-size:22rpx;
padding:10rpx 30rpx;
border-radius:40rpx;
line-height:1em;
margin: 0 10rpx 10rpx 0;
}
}
跟往常写法不同的是没有写宽高,而是直接进行padding
标题栏布局
<view class="layout">
<view class="navbar">
<view class="statusBar" :style="{height:statusBarHeight + 'px'}">
</view>
<view class="titleBar" :style="{height:titleBarHeight + 'px'}">
<view class="title">
标题
</view>
<view class="search">
<uni-icons class="icon" type="search" color="#888" size="18"></uni-icons>
<text class="text">搜索</text>
</view>
</view>
</view>
<view class="fill" :style="{height:statusBarHeight+titleBarHeight+'px'}">
</view>
</view>
.titleBar{
display:flex;
align-items: center;
}
<script setup>
import {ref} from 'vue'
let SYSTEM_INFO = uni.getSystemInfoSync();
let statusBarHeight = ref(SYSTEM_INFO.statusBarHeight);
//获取胶囊按钮的状态
let {top,height} = uni.getMenuButtonBoundingClientRect();
let titleBarHeight = ref(height + (top - statusBarHeight.value)*2)
</script>
减少加载额外图片所产生的网络消耗
function readImgsFun(){
readImgs.value.push(
currentIndex.value<=0 ? classList.value.length-1 : currentIndex.value-1,
currentIndex.value,
currentIndex.value>=classList.value.length-1 ? 0 : currentIndex.value+1
)
//实现数组去重的快速办法
readImgs.value = [...new Set(readImgs.value)]
}
下载图片到设备
// 下载保存到设备
const clickDownload = async () => {
// #ifdef H5
uni.showModal({
content: "请长按保存壁纸",
showCancel::false
})
// #endif
// #ifndef H5
try {
uni.showLoading({
title: "下载中...",
mask: true;
})
let {
classid,
_id: wallId
} = currentInfo.value
let res = await apiWriteDownload({
classid,
wallId
})
if (res.errCode != 0) throw res;
uni.getImageInfo({
src: currentInfo.value.picurl,
success: (res) => {
uni.saveImageToPhotosAlbum({
// 非网络照片
filePath: res.path,
success: (res) => {
console.log(res)
},
fail: err => {
if (err.errMsg == 'saveImageToPhotoAlbum:fail cancel') {
uni.showToast({
title: '保存失败,请重新点击下载',
icon: "none"
})
return;
}
uni.showModal({
title: "提示",
content: "需要授权保存相册",
success: res => {
if (res.confirm) {
uni.openSetting({
success: (setting) => {
console.log(
setting)
if (setting
.authSetting[
'scope.writePhotosAlbum'
]) {
uni.showToast({
title: "获取授权成功",
icon: 'none'
})
} else {
uni.showToast({
title: "获取授权失败",
icon: 'none'
})
}
}
})
}
}
})
},
complete: () => {
uni.hideLoading();
}
})
}
})
} catch (err) {
console.log(err);
uni.hideLoading();
}
// #endif
}
关于setup函数和onLoad
由于setup函数比onLoad函数执行的快,所以在setup函数声明的,而在onLoad函数里面赋值的变量可能会报错
比如以下情况加解决办法,就是在父元素加上v-if,让 变量有值的时候再渲染该元素,就不会报错
<view class="content" v-if="currentInfo">
</view>
const currentInfo = ref(null);
onLoad((e) => {
currentId.value = e.id;
currentIndex.value = classList.value.findIndex(item => {
return item._id == currentId.value
})
currentInfo.value = classList.value[currentIndex.value]
// readImgs.value.push(currentIndex.value-1,currentIndex.value,currentIndex.value+1)
readImgsFun();
console.log('currentIndex', currentIndex.value)
})
给对象数组的每个元素添加属性值
classList.value = res.data.map(item=>{
return {
...item,
picurl: item.smallPicurl.replace("_small.webp", ".jpg")
}
})