標籤:style blog io java ar strong div cti 代碼
function orientation_change() { var viewport = document.querySelector(‘meta[name="viewport"]‘); if (window.orientation == 0 || window.orientation == 180) viewport.setAttribute("content", "width=device-width, maximum-scale=1.0, initial-scale=1.0, user-scalable=no"); else if (window.orientation == -90 || window.orientation == 90) viewport.setAttribute("content", "width=device-height, maximum-scale=1.0, initial-scale=1.0, user-scalable=no"); }window.onorientationchange = orientation_change;
window.orientation
:這個屬性給出了當前裝置的螢幕方向,0表示豎屏,正負90表示橫屏(向左與向右)模式
onorientationchange
: 在每次螢幕方向在橫豎屏間切換後,就會觸發這個window事件,用法與傳統的事件類別似
#b{width:300px;height:200px;background:blue;}@media screen and (orientation:portrait){#b{background:red;}}@media screen and (orientation:landscape){#b{background:green;}}
防止橫豎屏時,iphone自動縮放的一段js代碼