在頁面中引入以下代碼,把樣式中帶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; }) }