当前位置: 代码网 > it编程>编程语言>Javascript > 前端常见的两种pc适配方案介绍

前端常见的两种pc适配方案介绍

2024年11月26日 Javascript 我要评论
1、自适应介绍1.1、场景1:电商网站: 在pc端,自适应设计能够确保商品页面在各种显示器上清晰可读,购物车和结算流程也能顺畅进行。用户能够方便地浏览商品、查看图片和提交订单,无论是在高分辨率的大屏显

1、自适应介绍

1.1、场景

1:电商网站: 在pc端,自适应设计能够确保商品页面在各种显示器上清晰可读,购物车和结算流程也能顺畅进行。用户能够方便地浏览商品、查看图片和提交订单,无论是在高分辨率的大屏显示器还是较小的电脑屏幕上。

2:企业官网: 企业网站通常需要展示丰富的信息和图表,包括公司介绍、服务内容和联系方式。自适应设计能够确保这些内容在不同的pc屏幕上都能以最优方式呈现,使潜在客户和合作伙伴能够快速获取信息。

3:在线教育平台: 教育平台上的课程内容、视频和互动模块需要在各种屏幕上都能正常显示。自适应设计使得学习者能够在不同设备上享受一致的学习体验,提高了学习的便利性和效果。

1.2、意义

1:提升用户体验: 通过自适应设计,用户在不同的设备和屏幕尺寸下都能享受到优化的浏览体验,减少了页面缩放和滚动的需要,提高了网站的可用性。

2:增加网站访问量: 自适应设计使网站能够适配更多类型的设备和屏幕,增加了访问的灵活性。这有助于吸引和留住更多用户,提高网站的整体流量。

3:提高开发效率: 采用自适应设计可以减少为不同设备开发和维护多个版本网站的需要,从而节省开发和维护的时间和成本。

4:增强seo表现: 搜索引擎通常更倾向于推荐适配性强的网站。良好的自适应设计能够提高网站的搜索引擎排名,从而增加曝光率和访问量。

2、方案一 适配宽高

2.1、介绍:适配小于比例的宽或高,进行缩放 并移动位置使其在页面中心点

2.2、缺点:两侧或上下可能会留白 字体变小

<template>
  <div ref="appref" class="appref">
    <router-view />
  </div>
</template>

<script setup>
import { ref, onmounted, onbeforeunmount } from 'vue';

const scale = {
  width: '1',
  height: '1'
};

const basewidth = 1920;
const baseheight = 1080;
const baseproportion = parsefloat((basewidth / baseheight).tofixed(5));

const appref = ref(null);
const drawtiming = ref(null);

const resize = function () {
  cleartimeout(drawtiming.value);
  drawtiming.value = settimeout(() => {
    calcrate();
  }, 200);
};

const calcrate = function () {
  if (!appref.value) return;
  const currentrate = parsefloat((window.innerwidth / window.innerheight).tofixed(5));
  if (currentrate > baseproportion) {
    // 更宽
    scale.width = ((window.innerheight * baseproportion) / basewidth).tofixed(5);
    scale.height = (window.innerheight / baseheight).tofixed(5);
  } else {
    // 更高
    scale.height = (window.innerwidth / baseproportion / baseheight).tofixed(5);
    scale.width = (window.innerwidth / basewidth).tofixed(5);
  }
  appref.value.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`;
  appref.value.style.transformorigin = 'top left'; // 设置变换的原点为左上角
};

onmounted(() => {
  window.addeventlistener('resize', resize);
  resize(); // 初始调用
});

onbeforeunmount(() => {
  window.removeeventlistener('resize', resize);
});
</script>

<style lang="scss" scoped>
.appref {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 1920px;
  height: 1080px;
  transform-origin: top left; // 与 javascript 中的设置保持一致
}
</style>

3、方案二 适配宽,高度滚动

3.1介绍:只适配页面宽度 高度跟随比例进行滚动

3.2、缺点:增加了滚动条

<template>
  <div ref="appref" class="appref">
    <router-view />
  </div>
</template>

<script setup>
import { ref, onmounted, onbeforeunmount } from 'vue';

const scale = {
  width: '1',
  height: '1'
};

const basewidth = 1920;
const baseheight = 1080;
const baseproportion = parsefloat((basewidth / baseheight).tofixed(5));

const appref = ref(null);
const drawtiming = ref(null);

const resize = function () {
  cleartimeout(drawtiming.value);
  drawtiming.value = settimeout(() => {
    calcrate();
  }, 200);
};

const calcrate = function () {
  if (!appref.value) return;
  const widthscale = window.innerwidth / 1920; // 基准宽度为1920px
  const offsetx = ((window.innerheight / widthscale) - window.innerheight) / 2;
  appref.value.style.height = `${window.innerheight / widthscale}px`;
  appref.value.style.transform = `scale(${widthscale})`;
  appref.value.style.transformorigin = 'left center'; // 设置变换的原点为左上角
  appref.value.style.position = 'absolute';
  appref.value.style.left = `0px`;
  appref.value.style.top = `${-offsetx}px`;
  appref.value.style.width = `1920px`;
};

onmounted(() => {
  window.addeventlistener('resize', resize);
  resize(); // 初始调用
});

onbeforeunmount(() => {
  window.removeeventlistener('resize', resize);
});
</script>

<style lang="scss" scoped>
.appref {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 1920px;
  height: 1080px;
  transform-origin: top left; // 与 javascript 中的设置保持一致
}
</style>

总结

总之,pc端自适应和适配技术不仅优化了用户体验,还提高了网站的整体效率和表现,是现代网页开发中不可或缺的一部分。

到此这篇关于前端常见的两种pc适配方案的文章就介绍到这了,更多相关前端pc适配方案内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com