var isW = function(){ return document.documentElement.clientWidth; }
var isH = function(){ return document.documentElement.clientHeight; }
var oVp = document.getElementById('viewport');
function css3(obj,attr,val){
var str = attr.charAt(0).toUpperCase() + attr.substring(1);
obj.style['Webkit'+str] = val;
obj.style['Moz'+str] = val;
obj.style['ms'+str] = val;
obj.style['O'+str] = val;
obj.style[attr] = val;
}
function setViewport(){
var gameS = oVp.offsetWidth/oVp.offsetHeight;
var screenS = isW()/isH();
if(gameS>screenS){
//高度拉伸
var _tc = isW()/oVp.offsetWidth;
css3(oVp,'transform','scale('+_tc+')');
}else{
//宽度拉伸
var _tc = isH()/oVp.offsetHeight;
css3(oVp,'transform','scale('+_tc+')');
}
}
//resize
function resizeSc(){
if(window.orientation){
window.addEventListener('orientationchange',function(){
setViewport();
},false);
}else{
window.addEventListener('resize',function(){
setViewport();
},false);
}
}
resizeSc();
网友评论