document.body

本文探讨了在Web开发中,使用document.body与document.getElementsByTagName('body')[0]的区别,强调了后者在某些场景下的优势。
比如document.body,最好是写成document.getElementsByTagName("body")[0];

转载于:https://www.cnblogs.com/loveMis/p/9246513.html

在Vue2中使用处理屏幕横竖屏切换的JavaScript代码,可将代码放置在Vue组件的生命周期钩子函数里。以下是具体示例: ```vue <template> <div id="app"> <!-- 页面内容 --> </div> </template> <script> export default { name: 'App', mounted() { window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() { if (window.orientation === 90 || window.orientation === -90) { document.body.style.transform = 'none'; } else { document.body.style.transform = 'rotate(90deg)'; document.body.style.transformOrigin = 'left bottom'; document.body.style.width = '100vh'; document.body.style.height = '100vw'; document.body.style.position = 'absolute'; document.body.style.top = '100%'; document.body.style.left = '0'; } }); }, beforeDestroy() { // 在组件销毁前移除事件监听器,防止内存泄漏 const handler = function() { if (window.orientation === 90 || window.orientation === -90) { document.body.style.transform = 'none'; } else { document.body.style.transform = 'rotate(90deg)'; document.body.style.transformOrigin = 'left bottom'; document.body.style.width = '100vh'; document.body.style.height = '100vw'; document.body.style.position = 'absolute'; document.body.style.top = '100%'; document.body.style.left = '0'; } }; window.removeEventListener("onorientationchange" in window ? "orientationchange" : "resize", handler); } }; </script> <style> /* 样式部分 */ </style> ``` 在上述代码中,将横竖屏切换的监听逻辑放在`mounted`生命周期钩子函数中,这样在组件挂载到DOM后就会开始监听屏幕方向变化。同时,在`beforeDestroy`钩子函数中移除事件监听器,避免组件销毁后事件监听器仍然存在,造成内存泄漏。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值