vue移動端屏幕適配詳解
flexible
vue移動端屏幕適配,查看項目地址
效果預(yù)覽
# 項目clone
git clone git@github.com:NicolasGui/flexible.git
# 進入項目目錄
cd flexible
# 安裝依賴
npm install
# 啟動服務(wù) localhost:8080
npm run dev
原理概述
插件安裝
# 插件一:amfe-flexible
npm install amfe-flexible --save
# 插件二: node-sass
npm install amfe-flexible --save # 同時,在main.js文件內(nèi)引入
npm install sass-loader --save
編寫js處理方法
在utils目錄下創(chuàng)建flex.js文件,內(nèi)容如下:
import Vue from 'vue'
Vue.prototype.$setTitle = function (text) {
document.title = text
}
Vue.prototype.$getPX = function (design, designWidth = 750) { // 750為設(shè)計稿寬度
// 獲取窗口尺寸
let width = document.documentElement.getBoundingClientRect().width
// 計算縮放比例
let scale = width / designWidth
// 獲取實時尺寸
return design * scale
}
同時,在main.js文件內(nèi)引入該js文件
import Vue from 'vue'
import App from './App'
import router from './router'
import 'amfe-flexible'
import './utils/flex'
Vue.config.productionTip = false
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
編寫css處理方法
在src目錄創(chuàng)建styles目錄,同時在該目錄新增common.scss文件,內(nèi)容如下:
body,div,ul,ol,dl,li,dt,dd,h1,h2,h3,h4,p,form,iframe,input,textarea,a,span,em,strong,img,html,nav,header,article,button,footer,var {
padding:0;
margin:0;
}
body { font:12px/1.2rem "Microsoft YaHei",tahoma,arial,sans-serif;min-width:320px;position:relative; }
form,input {background:none;border:none;}
ul,dl,ol {list-style-type:none;}
h1, h2, h3, h4, h5 { font:12px/1.2rem "Microsoft YaHei",arial,tahoma; }
a { text-decoration:none; }
a:hover,a:focus { outline:none; }
table { border-collapse:collapse;border-spacing:0; }
img { border:none; }
strong,b { font-weight:normal; }
em,i,var { font-style:normal; }
p { text-indent:0; }
.clear { clear:both;height:0;line-height:0;overflow:hidden;width:0; }
.clearfix:after { clear:both;content:".";display:block;font-size:0;height:0;visibility:hidden; }
// 尺寸轉(zhuǎn)換
@function px2rem($px, $base-font-size: 75px) { /*設(shè)計稿寬度為750,因此此處為75*/
@if (unitless($px)) {
@return px2rem($px + 0px);
} @else if (unit($px) == rem) {
@return $px;
}
@return ($px / $base-font-size) * 1rem;
}
// 字體轉(zhuǎn)換
@mixin font-dpr($font-size) {
font-size: $font-size;
[data-dpr="2"] & {
font-size: $font-size * 2;
}
[data-dpr="3"] & {
font-size: $font-size * 3;
}
}
css內(nèi)使用
<style scoped lang='scss'>
@import '../styles/common.scss';
.content{
width:px2rem(750); /*750為設(shè)計稿實際尺寸*/
font-size:px2rem(20) /*20為設(shè)計稿實際尺寸*/
</style>
js內(nèi)使用
export default {
name:'test',
data() {
return {
w:0;
}
},
watch:{
getWidth() {
this.w=this.$getPX(500);
}
}
},
computed: {
fun() {
return (this.w/this.$getPX(500)*100).toFixed(2) + '';
}
}
}
以上所述是小編給大家介紹的vue移動端屏幕適配詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue實現(xiàn)樹形結(jié)構(gòu)樣式和功能的實例代碼
這篇文章主要介紹了vue樹形結(jié)構(gòu)樣式和功能,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
Vue3中關(guān)于getCurrentInstance的大坑及解決
這篇文章主要介紹了Vue3中關(guān)于getCurrentInstance的大坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
詳解Vue的watch中的immediate與watch是什么意思
這篇文章主要介紹了詳解Vue的watch中的immediate與watch是什么意思,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

