在做h5展示頁面的時(shí)候 考慮到許多css樣式在橫屏?xí)霈F(xiàn)布局錯(cuò)亂, 為防止提示用戶橫屏顯示
demo介紹
在做h5展示頁面的時(shí)候 考慮到許多css樣式在橫屏?xí)霈F(xiàn)布局錯(cuò)亂, 為防止提示用戶橫屏顯示
使用 html5屏幕旋轉(zhuǎn)事件
onorientationchange
事件;使用 css3
animation
動(dòng)畫;
html
<div id="tip">
<i class="icon"></i>
<p>為了更好的體驗(yàn)菇篡,請(qǐng)使用豎屏瀏覽</p>
</div>
css
*{
margin: 0;
padding: 0;
}
/* 容器 */
#tip {
position: absolute;
top: 0;
left: 0;
z-index: 999;
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 1);
}
/* 手機(jī)圖片 */
.icon {
display: inline-block;
width: 67px;
height: 109px;
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIYAAADaCAMAAABU68ovAAAAXVBMVEUAAAD29vb////x8fH////////x8fH5+fn29vby8vL////5+fn39/f6+vr////x8fH////////+/v7////09PT////x8fH39/f////////////////////x8fH///+WLTLGAAAAHXRSTlMAIpML+gb4ZhHWn1c2gvHBvq1uKJcC6k8b187lQ9yhhboAAAQYSURBVHja7d3blpowFIDhTUIAOchZDkre/zE7ycySrbUUpsRN2/1fzO18KzEqxEVgTiZNfgmmtxRc8iaR8HNe8x4BtjQePKayYCIoyBSgvNNE1AkNSHqZyLqk97EgUCCHBzZ5mkg7ScvIJuIyOyXBRFxgpqWZyGsAZLB1KjsJi8nutHU4JCRbFRH8tmirI9k8Jx2sqNs8K/m0LQkrktO2crgcgXGB4AiTEsB0hJfo9MGgX7CGcYiYwQxmMOOvZwRhBG8tCoMXjBDeXvWCEcHbi14wgCBmMIMZzGAGM5jxETNwzMAxA8cMHDNwzMAxA8cMHDNwzMAxA8cMHDNwzMAxY6E2rUQxnH2tz9cirlJFwFBJedaPnUv0M7++egPDE8iAJcIDmxwH5ww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67px 109px;
margin-bottom: 2em;
}
/* 提示文本 */
#tip p {
color: white;
font-size: 2em;
}
/* 手機(jī)旋轉(zhuǎn)動(dòng)畫 */
@keyframes rotation {
10% {
transform: rotate(90deg);
}
50% {
transform: rotate(0);
}
60% {
transform: rotate(0);
}
90% {
transform: rotate(90deg);
}
100% {
transform: rotate(90deg);
}
}
JavaScript
;(function screenStatus() {
const element = document.querySelector('#tip');
element.style.visibility = 'hidden';
const status = "onorientationchange" in window ? "orientationchange" : "resize";
window.addEventListener(status, e => {
if (window.orientation === 180 || window.orientation === 0) {
element.style.visibility = 'hidden';
}
if (window.orientation === 90 || window.orientation === -90) {
element.style.visibility = 'visible';
}
});
}());
判斷 瀏覽器是否支持h5屏幕旋轉(zhuǎn)事件
onorientationchange
,否則用resize
事件