以iphone6plus 為標准單位是px的頁面 在運行時轉換為rem



在頁面中引入以下代碼,把樣式中帶px單位的樣式放到本頁面中的<style>標簽中



/*
* * Created by Administrator on 2017-03-14. */ /*************************************設置頁面rem的長度***START***************************************/ var num=screen.width;//獲取屏幕的寬度 var font=num/10;//把寬度分成10份 document.getElementsByTagName('html')[0].style.fontSize=font+'px';//html下的font-size的大小就是1rem單位長度的大小 /*************************************設置頁面rem的長度***END***************************************/ /*************************************把樣式中的px替換成rem***START***************************************/ var htmlStr=document.getElementsByTagName("style")[0].innerHTML; var newStr=htmlStr.replace(/(\d+)px/g,function(a,b){ return ((b/41.4)+"").substr(0,6)+"rem"; }) document.getElementsByTagName("style")[0].innerHTML=newStr; /*************************************把樣式中的px替換成rem***END***************************************/ /***************************************監聽手機橫屏***START**************************************************/ window.onresize=function(){//監聽手機橫屏,重新定義1rem的長度 var num=screen.width;//獲取屏幕的寬度 var font=num/10;//把寬度分成10份 document.getElementsByTagName('html')[0].style.fontSize=font+'px';//html下的font-size的大小就是1rem單位長度的大小 } /***************************************監聽手機橫屏***END**************************************************/ //獲取url中的參數 function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i"); var r = window.location.search.substr(1).match(reg); if (r!=null) return unescape(r[2]); return null; } //跳轉頁面 function jumpPage(selector,url){ $(selector).click(function(){ window.location.href=url; }) }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM