移动端最强适配(rem适配之px2rem) 移动端结合Vuex实现简单loading加载效果

news/2024/7/3 15:12:33

一、rem之px2rem适配

前言:相信许多小伙伴上手移动端时面对各式各样的适配方案,挑选出一个自己觉得简便、实用、高效的适配方案是件很纠结的事情。 深有体会...

经过多个移动端项目从最初的 viewport --> 百分比 --> rem --> rem的升级版px2rem可谓是一把鼻涕一把泪啊 ,在px2rem之前总觉得之前的不够完美或者麻烦;

进入正题: 首先 px2rem 也是基于 rem  适配的,但是他的好用之处在于灵活、简便、高效不用我们自己去换算、px2rem-loader  会帮我们换算转换成rem适配各种机型; 

1、安装 px2rem-loader  (webpack构建的项目)

npm i px2rem-loader --save-dev

2、(build/utils.js文件)配置 px2rem-loader

// utils.js
const cssLoader = {loader: 'css-loader',options: {sourceMap: options.sourceMap}
}/*  px2rem */
const px2remLoader = {loader: 'px2rem-loader',options: {remUnit: 75 // 设计稿宽度/10  remUnit的值自定义多少都无所谓,最终都会转换成相应的rem 设计稿参照iphone的话推荐75 或者100}
};/*  添加到loaders数组中 */
function generateLoaders(loader, loaderOptions) {const loaders = [cssLoader, px2remLoader]
}

用法: 标注图即量即所得;设计稿量多少就可以写多少了;最终显示时 px2remLoader会转换成相应的 rem 

代码: font-size: 40px;      浏览器控制台: font-size: 0.533333rem;   即 40/75  因为上面写的是75复制代码

完全不用自己去转换了!爽多了

小坑:当 border 或者 height 为1px时  你会发现最终转换下页面看不到了 ;解决如下

border: 1px solid #e6e6e6; /*no*/    后面加个注释  /*no*/ 目的是告诉 px2remLoader 这个用做转换复制代码

 二、结合Vuex自定义loading组件

这里只说如何实现,具体的vuex怎么使用注册请看以往博文 www.cnblogs.com/ljx20180807…

loading.vue

<template><!--loading--><div class="comp-loading"><div class="comp-loading-box"><img src="@/assets/img/loading.png"/><p>Loading...</p></div></div>
</template><style lang="stylus" rel="stylesheet/stylus" scoped>
.comp-loading {&-box {z-index: 10000;position: fixed;top: 40%;left: 50%;width: 160px;margin-left: -80px;padding: 30px 0;border-radius: 10px;background-color: rgba(0,0,0,.7);img {display: block;width: 60px;height: 60px;margin: 0 auto;animation: comp-loading-spin 1200ms infinite linear;}p {font-size: 26px;color: #fff;text-align: center;line-height: 26px;padding-top: 14px;}}
}
@keyframes comp-loading-spin {0%   { transform: rotate(0deg); }100% { transform: rotate(360deg); }
}
</style>

App.vue

<template><!-- App.vue --><div id="app"><!--loading--><Loading v-show="showHttpLoading"></Loading><router-view v-wechat-title="$route.meta.title"></router-view></div>
</template><script>import Loading from './components/loading'import error from './services/error'export default {name: 'App',data () {return {showHttpLoading: false}},components: {Loading},watch: {// 监听 showHttpLoading 触发loading效果'$store.state.showHttpLoading' (val) {// set loadingthis.showHttpLoading = val}}}
</script>

在哪里调用呢;我项目是在所有请求的时候和路由跳转的时候调用;请求成功则关闭loading;具体效果可根据你公司需求确认

// config.js
import Vue from 'vue'
import axios from 'axios'
import store from '../store'
import router from '../router/index'import { Toast } from 'cube-ui'Vue.use(Toast)const init = function () {// 请求拦截器axios.interceptors.request.use(function (config) {// 触发loadingstore.commit('UPDATE_SHOW_HTTP_LOADING', true).......}, function (err) {// 抛出错误store.commit('UPDATE_SHOW_HTTP_LOADING', false).....})// 响应拦截器 Add a response interceptoraxios.interceptors.response.use(function (response) {// 请求成功关闭loadingstore.commit('UPDATE_SHOW_HTTP_LOADING', false).........}, function (error) {store.commit('UPDATE_SHOW_HTTP_LOADING', false).......})// 前置守卫  只为触发loading效果觉得不需要则去掉router.beforeEach((to, from, next) => {if (to.matched && to.matched.length && to.matched[0].path) {// 已授权情况   触发loadingstore.commit('UPDATE_SHOW_HTTP_LOADING', true)next()}})// 后置守卫  只为关闭loading  不用loading则去掉router.afterEach((to, from) => {// 关闭loadingstore.commit('UPDATE_SHOW_HTTP_LOADING', false)})

注意的是需要在main.js 里引入config.js 并初始化去 config.init()

效果图不存在卡顿,看起来稍微有些卡顿是录制gif图工具的原因。

 

结语: 以上就是今天要分享的内容了,有问题欢迎留言

转载于:https://www.cnblogs.com/ljx20180807/p/10319776.html


http://lihuaxi.xjx100.cn/news/268626.html

相关文章

重磅!国内首个面向自动驾驶领域的多传感器数据融合系统课程

应用背景介绍多传感器融合是一项结合多传感器数据的综合性前沿内容&#xff0c;主要包括Camera、激光雷达、IMU、毫米波雷达等传感器的融合&#xff0c;在自动驾驶、移动机器人的感知和定位领域中占有非常重要的地位&#xff1b;随着AI技术的大规模落地&#xff0c;图森、百度、…

Mac zsh not found jupyter notebook

zsh是一个很好的终端模拟器&#xff0c;在自动补全上面很好用。但是在初次使用的过程中&#xff0c;发现以前在bash里面的配置文件不能移植过来&#xff0c;解决的办法如下&#xff1a; 打开vim &#xff5e;/.zshrc 加入 source &#xff5e;/.bash_profile :wq 保存 source …

图像预处理第7步:标准归一化

图像预处理第7步&#xff1a;标准归一化将分割出来的各个不同宽、高的数字字符宽、高统一 //图像预处理第7步&#xff1a;标准归一化 //将分割出来的各个不同宽、高的数字字符宽、高统一 void CChildView::OnImgprcStandarize() {StdDIBbyRect(m_hDIB,w_sample,h_sample);//在…

爬虫之xpath语法-常用节点选择语法

爬虫之xpath语法-常用节点选择语法 可以通过通配符来选取未知的html、xml的元素 1.1 选取未知节点的语法 通配符描述*匹配任何元素节点。node()匹配任何类型的节点。 1.2 语法练习 从itcast的页面中 http://www.itcast.cn/ &#xff0c;选中全部的标签、全部的属性 全部的标签…

linux终端程序如何编写,[LINUX]利用Ncursesw编写支持中文的终端程序

1. Ncursesw库的安装与使用Ncurses库的安装(Ubuntu):查看相关软件包sudo apt-cache search ncursesw安装ncursew必须软件包sudo apt-get install libncurses5 libncurses5-dbg libncurses5-dev使用Ncursesw库时&#xff0c;需要包含头文件 ncurses.h&#xff0c;没错&#xff0…

开发板运行linux下虚拟机ubuntu的ping

虚拟机网卡设置可以选择好几种方式&#xff0c;常用的就是NAT和桥接&#xff08;bridged&#xff09;虚拟机要和开发板进行网络通信&#xff0c;只能通过桥接方式连接。虚拟机要想被开发板ping通&#xff0c;设置步骤如下&#xff1a; 第一步&#xff1a;虚拟机设置成桥接方式。…

《Android开发从零开始》——11.AbsoluteLayoutFrameLayout学习

本节课的主要内容有&#xff1a; 1.介绍AbsoluteLayout布局的使用 2.介绍FrameLayout布局的使用 课程下载地址&#xff1a;http://u.115.com/file/f15a9d5411 课件及地址&#xff1a;http://u.115.com/file/f1b56ce345 【转】转载于:https://blog.51cto.com/professor/1573001

使用OpenCV构建会玩石头剪刀布的AI

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达这个项目的代码可以在我的Github上找到https://github.com/HOD101s/RockPaperScissor-AI- 简介这个项目的基础是深度学习和图像分类&#xff0c;目的是创建一个简单而有趣…