uniapp 使用第一天遇到的问题

359 阅读1分钟

uniapp

  • 字体大小 要用rpx来写
  • 要用transfrom 来进行缩放div 布局
  • 尽量用vh布局
  • 做1080的时候尽量要用rpx 布好局再用缩放来实现 这样整体效果不会偏差太多
  • ifdef H5 是适配h5页面的

  • ifdef MP-WEIXIN 是适配微信小程序的

scroll-view使用个人理解

  • 开启scroll-y scroll-x 滚动方向
  • scroll-x 的时候要加入white-space: nowrap;样式强制在一行显示
  • scroll-y 开启的时候 计算好高度 以免造成遮挡
  • 注意white-space: nowrap; 单词不能写错 uniapp头部设定
  • 需要在pages.json中style里面加入"navigationStyle": "custom"就可以实现在定义头部

拖拽效果

  • 组件的方法实现拖拽效果 计算 它的宽高和鼠标位置
<template>
	<view>
		<view
			id="_drag_button"
			class="drag"
			:style="'left: ' + left + 'px; top:' + top + 'px;'"
			@touchstart="touchstart"
			@touchmove.stop.prevent="touchmove"
			@touchend="touchend"
			@click.stop.prevent="click"
			:class="{transition: isDock && !isMove }"
		>
		
			<view class="img" style="width: 75rpx;height: 85rpx;display: inline-block;vertical-align: middle;position: relative;" >
				<image src="../../../static/1644454139(1).jpg" style=" width: 100rpx; height: 100%;vertical-align: middle;position: absolute;top: 0;" mode=""></image>
				<view style="color: #fff;background: red;width: 40rpx; height: 40rpx;font-size: 24rpx;position: absolute;top: -20%;right: -35%;text-align: center;line-height: 40rpx;border-radius: 50%;">
					5
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'drag-button',
		props: {
			isDock:{
				type: Boolean,
				default: false
			},
			existTabBar:{
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				top:0,
				left:302,
				width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				isMove: true,
				edge: 10,
				text: '按钮'
			}
		},
		mounted() {
			const sys = uni.getSystemInfoSync();
			
			this.windowWidth = sys.windowWidth;
			this.windowHeight = sys.windowHeight;
			
			// #ifdef APP-PLUS
				this.existTabBar && (this.windowHeight -= 50);
			// #endif
			if (sys.windowTop) {
				this.windowHeight += sys.windowTop;
			}
			console.log(sys)
			
			const query = uni.createSelectorQuery().in(this);
			query.select('#_drag_button').boundingClientRect(data => {
				this.width = data.width;
				this.height = data.height;
				this.offsetWidth = data.width / 2;
				this.offsetHeight = data.height / 2;
				this.left = this.windowWidth - this.width - this.edge;
				this.top = 361;
			}).exec();
		},
		methods: {
			click() {
				this.$emit('btnClick');
			},
			touchstart(e) {
				this.$emit('btnTouchstart');
			},
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}
				
				this.isMove = true;
				
				this.left = e.touches[0].clientX - this.offsetWidth;
				
				let clientY = e.touches[0].clientY - this.offsetHeight;
				// #ifdef H5
					clientY += this.height;
				// #endif
				let edgeBottom = this.windowHeight - this.height - this.edge;

				// 上下触及边界
				if (clientY < this.edge) {
					this.top = this.edge;
				} else if (clientY > edgeBottom) {
					this.top = edgeBottom;
				} else {
					this.top = clientY
				}
				
			},
			touchend(e) {
				if (this.isDock) {
					let edgeRigth = this.windowWidth - this.width - this.edge;
					
					if (this.left < this.windowWidth / 2 - this.offsetWidth) {
						this.left = this.edge;
					} else {
						this.left = edgeRigth;
					}
					
				}
				
				this.isMove = false;
				
				this.$emit('btnTouchend');
			},
		}}
</script>

<style lang="scss">
	.drag {
		display: flex;
		justify-content: center;
		align-items: center;
		background: #2C2B5F;
		// box-shadow: 0 0 6upx rgba(0, 0, 0, 0.4);
		color: $uni-text-color-inverse;
		width: 150rpx;
		height: 150rpx;
		border-radius: 50rpx 0 40rpx 0;
		font-size: $uni-font-size-sm;
		position: fixed;
		z-index: 999;
		transform: scale(0.80);
		&.transition {
			transition: left .3s ease,top .3s ease;
		}
	}
	
</style>