vue实现项目对不同设备展示的缩放

我们看到我们当前的网站在iphone6/7/8上基本上没什么问题

但是在ipad上展示效果就不尽如人意了

我们希望我们的网站能够针对不同的设备类型能够展示不同的展示效果的一个网站

我们的整个项目都通过rem来进行页面布局

base.scss

html {
    font-size: 100px; // rem的大小是相对于这个根姿态的fontSize的
}
body {
    font-size: .12rem;
}

我们要检查一下项目里面有没有笔误的地方使用了px,把它改成rem

我们现在开发的尺寸都是根据iphone678,也就是375这个尺寸来开发的,如果你屏幕变宽的话,我这个屏幕就等比例放大

如果屏幕榨,那么我就等比例缩小

我们可以在index.html里面写一段js代码,这里是不参与编译的,所以我们要用原生ES5的语法来写

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title><%= htmlWebpackPlugin.options.title %></title>
    <script>
      // 这里表示我一进入页面,我就知道设备现在的屏幕宽度是多少
      var width = document.documentElement.clientWidth || document.body.clientWidth;
      // 我们再计算一下当前的设备和我们网页开发的默认的设备之间的比例
      var ratio = width / 375;
      // 我们要等比例缩放,我们整个网站都是使用rem布局的,那么我们只要使用改变fontSize的大小就行了    
      // 计算新的设备字体大小
      var fontSize = 100 * ratio;
      // 把这个数值赋值给html的fontsize
      document.getElementsByTagName('html')[0].style['font-size'] = fontSize + 'px'
    </script>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

这个时候我们再看一下iPad上的展示效果

这样我的网站就可以适配不同的设备了

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue项目做可视化大屏自适应缩放可以尝试以下几个方案: 1. 使用CSS3的transform属性进行缩放Vue组件中设置一个容器元素,通过CSS3的transform属性对容器元素进行缩放,可以实现自适应缩放的效果。在容器元素的父元素中监听resize事件,根据父元素的宽度和高度来计算缩放比例,然后通过CSS3的transform属性对容器元素进行缩放。 示例代码: ```html <template> <div class="container" ref="container"> <!-- 可视化大屏内容 --> </div> </template> <script> export default { mounted() { window.addEventListener('resize', this.handleResize) }, beforeDestroy() { window.removeEventListener('resize', this.handleResize) }, methods: { handleResize() { const container = this.$refs.container const parentWidth = container.parentNode.clientWidth const parentHeight = container.parentNode.clientHeight const contentWidth = container.offsetWidth const contentHeight = container.offsetHeight const scaleX = parentWidth / contentWidth const scaleY = parentHeight / contentHeight container.style.transform = `scale(${Math.min(scaleX, scaleY)})` } } } </script> <style> .container { transform-origin: top left; } </style> ``` 2. 使用Vue的自定义指令进行缩放Vue项目中可以创建一个自定义指令,通过该指令来实现自适应缩放的效果。在指令中监听resize事件,根据父元素的宽度和高度来计算缩放比例,然后通过CSS3的transform属性对元素进行缩放。 示例代码: ```html <template> <div class="container" v-resize-scale> <!-- 可视化大屏内容 --> </div> </template> <script> export default { directives: { resizeScale: { inserted(el) { function handleResize() { const parentWidth = el.parentNode.clientWidth const parentHeight = el.parentNode.clientHeight const contentWidth = el.offsetWidth const contentHeight = el.offsetHeight const scaleX = parentWidth / contentWidth const scaleY = parentHeight / contentHeight el.style.transform = `scale(${Math.min(scaleX, scaleY)})` } window.addEventListener('resize', handleResize) handleResize() }, unbind() { window.removeEventListener('resize', this.handleResize) } } } } </script> <style> .container { transform-origin: top left; } </style> ``` 以上两种方案可以根据实际需求进行选择,第一种方案比较简单,但是需要在组件中手动监听resize事件,第二种方案可以通过Vue的自定义指令来实现缩放效果,使得组件代码更加简洁。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值