uView修改样式(持续更新)

场景

通过样式穿透修改uView2.0组件样式,用于app
注意版本不一样方法可能不同

实现

  1. 通用
.uni-body{
	line-height: 0;
}
  1. u-input
::v-deep .u-input{
	height: 20.51rpx !important;
	padding: 0 6.59rpx !important;
}
::v-deep .uni-input-input{
	height:50%;
	font-size: 8.79rpx;
}
::v-deep .uni-input-placeholder{
	font-size: 8.79rpx;
}
  1. u-button
<u-button type="primary" shape="circle" text="登录" class="loginBtn"/>
...
.u-button__text{
	font-size: 10.25rpx !important;
}
.loginBtn{
	height: 20.51rpx;
	width: 100%;
	background-color: #fff;
	font-family: 'PingFang SC';
	font-weight: 500;
	font-size: 10.25rpx;
	color: #333;
	border: none;
}
.prompt{
	font-family: 'PingFang SC';
	font-weight: 500;
	font-size: 8.79rpx;
	color: #fff;
	text-align: center;
	margin-top: 8.79rpx;
	text{
		color: #2A81EB;
	}
}
  1. u–form
::v-deep .u-form-item__body{
	padding: 0;
}
// rules样式
::v-deep .u-form-item__body__right__message{
	margin: 0;
	padding: 0 !important;
	line-height: 0 !important;
	height: 0 !important;
	span{
		font-size: 8.79rpx;
	}
}
  1. u-tabs
.u-tabs{
	// height: 18.31rpx;
}
.u-tabs__wrapper__nav__item{
	height: 18.31rpx !important;
}
.u-tabs__wrapper__nav__item__text{
	span{
		font-size: 10.25rpx !important;
	}
}
.u-tabs__wrapper__nav__line{
	height: 2.2rpx !important;
}

6.u-checkbox-group 单个复选框

::v-deep .u-checkbox__icon-wrap{
	width: 11.72rpx !important;
	height: 11.72rpx !important;
}
::v-deep .u-icon-checkbox-mark{
	font-size: 8.79rpx !important;
	line-height: 8.79rpx !important;
}
  1. u-slider 滑块
uni-slider{
	margin: 0 8rpx !important; //左右边距可以不管,但上下必须消除
}
// .uni-slider-handle{
// 	width: 14.65rpx !important;
// 	height: 14.65rpx !important;
// 	margin-left: 7.32rpx !important;
// 	margin-top: 7.32rpx !important;
// }
// .uni-slider-thumb{
// 	width: 7.32rpx !important;
// 	height: 7.32rpx !important;
// 	margin-left: 3.66rpx !important;
// 	margin-top: 3.66rpx !important;
// }
// .uni-slider-handle-wrapper{
// 	height:10rpx !important; //线宽
// }
<template> <view class="check-in"> <u-form labelPosition="top" ref="form1" :model="state.userInfo" class='pnlForm' > <u-form-item label="姓名" prop="name" style="color:#fff"> <u-input v-model="state.userInfo.name" class="txtName"></u-input> </u-form-item> <u-form-item> <u-button @click="submit()" class='btnSubmit'>签到</u-button> </u-form-item> </u-form> </view> </template> <script setup> import { reactive,ref } from 'vue'; import { onLoad } from '@dcloudio/uni-app'; import {sign} from '@/common/ts/api/checkIn' import { showTips } from '@/utils'; const state=reactive({ userInfo:{ name:'' }, //定位信息 location: { address:'', latitude: '', longitude: '' }, deviceId:'', //设备Id }) const form1=ref(null); /** * 提交签到 或者 初始化的时候,检查签到信息 :isInitCheck:true,检查,false:提交 */ const submit=()=>{ const info = uni.getSystemInfoSync(); const deviceId = info.deviceId || info.deviceUUID || '未知设备ID'; if(!state.userInfo.name.trim()){ showTips('请填写姓名'); return; } let parms={ name:state.userInfo.name.trim(), deviceId:deviceId, location:"100,100", address:state.location.address||'', pageType:'250725', } sign(parms).then(res=>{ if(res.code==200){ uni.setStorageSync('checkIn-name',state.userInfo.name.trim()); uni.reLaunch({url:'/pages/check-thr/success?n='+state.userInfo.name.trim()+"&s="+res.data.seatNumber+"&m="+res.data.meetingRoom}); } else{ showTips(res?.msg||'签到发生错误,请联系主持人'); } }).catch(err=>{ showTips('发生错误:'+err?.msg); }) } const checkUserSign=()=>{ let name = uni.getStorageSync('checkIn-name'); if (!name) { return; } const info = uni.getSystemInfoSync(); const deviceId = info.deviceId || info.deviceUUID || '未知设备ID'; let parms={ name, deviceId:deviceId, location:"100,100", address:state.location.address||'', pageType:'250725', } sign(parms).then(res=>{ if(res.code==200){ uni.reLaunch({url:'/pages/check-thr/success?n='+name+"&s="+res.data.seatNumber+"&m="+res.data.meetingRoom}); } else{ showTips(res?.msg||'签到发生错误,请联系施珍珍老师'); } }).catch(err=>{ showTips('发生错误:'+err?.msg); }) } onLoad((options)=>{ // checkUserSign(); }) </script> <style scoped lang="scss"> .check-in{ // background-image:url('https://gateway-in-stg.rosino.com/stg1/biz-lease-mini-gateway/admin-api/infra/file/28/get/phbgi.jpg'); /* 替换为你的图片路径 */ background-image:url('@/static/image/banner_thr (2).png'); /* 替换为你的图片路径 */ background-size: cover; /* 背景图片覆盖整个容器 */ background-position: top; /* 背景图片居中显示 */ background-repeat: no-repeat; /* 背景图片不重复 */ // background-color: #F5F6F6; display: flex; flex-direction: column; width: 100%; height: 100%; min-height:100vh; // padding:20rpx 32rpx; max-width: 1000rpx; .pnlHeaderImg{ width: 100% !important; text-align: center; } .pnlForm{ // padding-top: 80%; // padding-left: 80rpx; // padding-right: 80rpx; padding:500rpx 80rpx 0; :deep(.u-form-item__body__left){ width:100% !important; } :deep(.u-form-item__body__left__content){ flex-direction: column !important; padding-bottom: 40rpx; } :deep(.u-form-item__body__left__content__label){ font-size: 40rpx; color: #fff !important; letter-spacing: 0; font-weight: 600; } :deep(.txtName){ .uni-input-input{ color:#fff !important; font-size: 30rpx; } } .btnSubmit{ // background: #FFDAA1; border-radius: 8rpx; font-size: 40rpx; color: #012AA0 !important; letter-spacing: 0; font-weight: 600; // margin-top: 20rpx; } } :deep(uni-image){ width: 100% !important; } } </style> 让代码中的pageType取路由上的pageType,并且.check-in的background-image:也取取路由上的pageType
最新发布
09-18
为了实现你的需求,我们需要: 1. **从路由中获取 `pageType` 参数**。 2. **动态设置 `background-image`**,根据 `pageType` 的不同加载不同的背景图。 我们可以通过 `onLoad` 生命周期钩子获取页面的路由参数,并将其保存在 `reactive` 中,然后在模板和样式中使用它。 --- ### ✅ 修改后的代码如下: #### ✅ `<template>` 部分 ```html <template> <view class="check-in" :style="{ backgroundImage: 'url(' + backgroundUrl + ')' }"> <u-form labelPosition="top" ref="form1" :model="state.userInfo" class='pnlForm'> <u-form-item label="姓名" prop="name"> <u-input v-model="state.userInfo.name" class="txtName"></u-input> </u-form-item> <u-form-item> <u-button @click="submit()" class='btnSubmit'>签到</u-button> </u-form-item> </u-form> </view> </template> ``` #### ✅ `<script setup>` 部分 ```js import { reactive, ref, computed } from 'vue'; import { onLoad } from '@dcloudio/uni-app'; import { sign } from '@/common/ts/api/checkIn'; import { showTips } from '@/utils'; const state = reactive({ userInfo: { name: '' }, location: { address: '', latitude: '', longitude: '' }, deviceId: '', pageType: '' // 新增 pageType 字段 }); const form1 = ref(null); // 动态计算背景图 URL const backgroundUrl = computed(() => { const backgrounds = { '250725': 'https://gateway-in-stg.rosino.com/stg1/biz-lease-mini-gateway/admin-api/infra/file/28/get/phbgi.jpg', 'otherType': '@/static/image/banner_thr_other.png' // 示例其他类型 }; return backgrounds[state.pageType] || '@/static/image/banner_thr.png'; // 默认图 }); const submit = () => { const info = uni.getSystemInfoSync(); const deviceId = info.deviceId || info.deviceUUID || '未知设备ID'; if (!state.userInfo.name.trim()) { showTips('请填写姓名'); return; } let parms = { name: state.userInfo.name.trim(), deviceId: deviceId, location: "100,100", address: state.location.address || '', pageType: state.pageType }; sign(parms).then(res => { if (res.code == 200) { uni.setStorageSync('checkIn-name', state.userInfo.name.trim()); uni.reLaunch({ url: '/pages/check-thr/success?n=' + state.userInfo.name.trim() + "&s=" + res.data.seatNumber + "&m=" + res.data.meetingRoom }); } else { showTips(res?.msg || '签到发生错误,请联系主持人'); } }).catch(err => { showTips('发生错误:' + err?.msg); }); }; const checkUserSign = () => { let name = uni.getStorageSync('checkIn-name'); if (!name) return; const info = uni.getSystemInfoSync(); const deviceId = info.deviceId || info.deviceUUID || '未知设备ID'; let parms = { name, deviceId: deviceId, location: "100,100", address: state.location.address || '', pageType: state.pageType }; sign(parms).then(res => { if (res.code == 200) { uni.reLaunch({ url: '/pages/check-thr/success?n=' + name + "&s=" + res.data.seatNumber + "&m=" + res.data.meetingRoom }); } else { showTips(res?.msg || '签到发生错误,请联系施珍珍老师'); } }).catch(err => { showTips('发生错误:' + err?.msg); }); }; onLoad((options) => { state.pageType = options.pageType || 'defaultType'; // 从路由中获取 pageType checkUserSign(); }); ``` #### ✅ `<style>` 部分(修改为内联样式控制背景图) 由于 `style` 标签中无法使用响应式变量,我们将背景图的设置从样式表中移除,改由模板中的 `:style` 控制。 ```scss <style scoped lang="scss"> .check-in { background-size: cover; background-position: top; background-repeat: no-repeat; display: flex; flex-direction: column; width: 100%; height: 100%; min-height: 100vh; max-width: 1000rpx; .pnlForm { padding: 500rpx 80rpx 0; :deep(.u-form-item__body__left) { width: 100% !important; } :deep(.u-form-item__body__left__content) { flex-direction: column !important; padding-bottom: 40rpx; } :deep(.u-form-item__body__left__content__label) { font-size: 40rpx; color: #fff !important; letter-spacing: 0; font-weight: 600; } :deep(.txtName .uni-input-input) { color: #fff !important; font-size: 30rpx; } .btnSubmit { font-size: 40rpx; color: #012AA0 !important; letter-spacing: 0; font-weight: 600; border-radius: 8rpx; } } :deep(uni-image) { width: 100% !important; } } </style> ``` --- ### ✅ 说明 - `onLoad((options) => { ... })`:从页面路由中获取 `pageType`,并保存在 `state` 中。 - `computed(() => { ... })`:根据 `pageType` 返回对应的背景图路径。 - 模板中使用 `:style="{ backgroundImage: 'url(' + backgroundUrl + ')' }"` 动态绑定背景图。 - 原来的 `background-image` 从 `style` 移除,改由动态绑定控制。 --- ### ✅ 示例 URL 你可以通过以下方式打开页面并传入 `pageType`: ```bash /pages/check-in/check-in?pageType=250725 ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值