请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

左边6个盒子不能完全展示

老师你好,我是跟视频一样的布局样式,但是左边6个盒子不能完全展示,没找出来原因

老师你好,这次我加了浏览器检查元素的图片,我个人排查下来觉得不是样式影响的,因为在控制台上下滑动过程中,也就是视口改变的过程中,我可以看到其他被遮挡的盒子一闪而过,我觉得是容器组件哪里影响的,但是目前又没排查出来原因。
图片描述

图片描述

图片描述

<template>
	<div id="zj-container" :ref="refName">
		<template v-if="ready">
			<slot></slot>
		</template>
	</div>
</template>

<script>
import { ref, getCurrentInstance, onMounted, onUnmounted, nextTick } from 'vue'
import { debounce } from '../../utils'
export default {
	name: 'Container',
	props: {
		options: Object
	},
	setup(ctx) {
		const refName = 'zjContainer'
		const width = ref(0)
		const height = ref(0)
		const originalWidth = ref(0)
		const originalHeight = ref(0)
		const ready = ref(false)
		let context, dom, observer // dom 也要定义成全局的,它只需要获取一次

		const initSize = () => {
			return new Promise((resolve) => {
				nextTick(() => {
					dom = context.$refs[refName]
							// 获取大屏的真实尺寸
					if (ctx.options && ctx.options.width && ctx.options.height) {
						width.value = ctx.options.width
						height.value = ctx.options.height
					} else {
						width.value = dom.clientWidth
						height.value = dom.clientHeight
					}
					// 获取画布尺寸
					if (!originalWidth.value || !originalHeight.value) {
						originalWidth.value = window.screen.width
						originalHeight.value = window.screen.height // window可以省略
					}
					console.log(width.value, height.value, originalWidth.value, originalHeight.value)
					resolve()
				})
			})
		}

		const updateSize = () => {
			if (width.value && height.value) {
				dom.style.width = `${width.value}px`
				dom.style.height = `${height.value}px`
			} else {
				dom.style.width = `${originalWidth.value}px`
				dom.style.height = `${originalHeight.value}px`
			}
		}

		const updateScale = () => {
			// 获取真实视口尺寸
			const currentWidth = document.body.clientWidth
			const currentHeight = document.body.clientHeight
			// 获取大屏最终的宽高
			const realWidth = width.value || originalWidth.value
			const realHeight = width.height || originalHeight.value
			const widthScale = currentWidth / realWidth
			const heightScale = currentHeight / realHeight
			dom && (dom.style.transform = `scale(${widthScale}, ${heightScale})`)
		}

		const onResize = async (e) => {
			console.log('onResize', e)
			await initSize()
			updateScale()
		}

		const initMutationObserver = () => {
			// 兼容 WebKit 内核,兼容 firefox Moz 内核
			const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver

			observer = new MutationObserver(onResize)
			observer.observe(dom, {
				atrributes: true, // 监听属性
				atrributeFilter: ['style'], // 指定监听的属性
				attributeOldValue: true // 会在监听的方法 onResize里传入一个对象
			})
		}

		const removeMutationObserver = () => {
			if (observer) {
				observer.disconnect()
				observer.takeRecords()
				observer = null
			}
		}

		onMounted(async () => {
			ready.value = false
			context = getCurrentInstance().ctx
			await initSize()
			updateSize()
			updateScale()
			
			// 加上防重发方法,在一定时间内,只会执行一次,
			window.addEventListener('resize', debounce(100, onResize))

			initMutationObserver()
			ready.value = true
		})

		onUnmounted(() => {
			window.removeEventListener('resize', onResize)

			removeMutationObserver()
		})
		return {
			refName,
			ready
		}
	}
};
</script>

<style lang="scss" scoped>
	#zj-container {
		position: fixed;
		top: 0;
		left: 0;
		overflow: hidden;
		transform-origin: left top;
		z-index: 999;
	}
</style>

正在回答

2回答

在获取大屏最终宽高的时候出现问题https://img1.sycdn.imooc.com//szimg/6010da1a0933eb3412480344.jpg这里是获取传递过来的高度,height.value

0 回复 有任何疑惑可以回复我~
  • 提问者 永远幸运 #1
    谢谢老师!你的回复和帮助,让我更加坚定坚持学习的信念!谢谢!
    回复 有任何疑惑可以回复我~ 2021-01-27 11:48:55
扬_灵 2021-01-26 17:52:42

同学你好,这是因为在这是左边盒子的时候设置flex的布局对齐方式为space-between,每块之间留有空白,你的代码中没有设置对齐方式。https://img1.sycdn.imooc.com//szimg/600fe66109cc787f11320374.jpg


0 回复 有任何疑惑可以回复我~
  • 提问者 永远幸运 #1
    老师您好,首先这节视频进行到7分38秒时,老师也没有用到justify-content: space-between;  就已经能完整展示了。其次,我也试着加上justify-content: space-between; 但还是不能完整展示。求助。
    回复 有任何疑惑可以回复我~ 2021-01-26 22:11:59
  • 扬_灵 回复 提问者 永远幸运 #2
    在视频7分38秒时.left是最开始是设置为红色的,元素是堆在一起的,里面的元素没有充满left,添加space-between的时候中间的元素有了间隔,在浏览器上把left的布局截进去,截图发一下吧
    回复 有任何疑惑可以回复我~ 2021-01-27 09:33:48
  • 提问者 永远幸运 回复 扬_灵 #3
    老师您好,这次我加了浏览器检查元素的图片,我个人排查下来觉得不是样式影响的,因为在控制台上下滑动过程中,也就是视口改变的过程中,我可以看到其他被遮挡的盒子一闪而过,我觉得是容器组件哪里影响的,但是目前又没排查出来原因。
    回复 有任何疑惑可以回复我~ 2021-01-27 09:53:38
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号