Screen.orientation
åºçº¿
广æ³å¯ç¨
èª 2023å¹´3æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
å®éªæ§: è¿æ¯ä¸é¡¹å®éªæ§ææ¯
å¨å°å
¶ç¨äºç产ä¹åï¼è¯·ä»ç»æ£æ¥æµè§å¨å
¼å®¹æ§è¡¨æ ¼ã
orientation æ¯ Screen æ¥å£çä¸ä¸ªåªè¯»å±æ§ï¼è¿åå±å¹å½åçæ¹åã
è¯æ³
var orientation = window.screen.orientation;
è¿åå¼
ä¸ä¸ª ScreenOrientation çå®ä¾ï¼è¡¨ç¤ºå±å¹çæ¹åã
注æå¨æ´æ©çãæåç¼ççæ¬ä¸ä¼è¿åä¸ä¸ª String å¼ï¼ç¸å½äº ScreenOrientation.type çå¼ã
示ä¾
js
var orientation =
(screen.orientation || {}).type ||
screen.mozOrientation ||
screen.msOrientation;
if (orientation === "landscape-primary") {
console.log("That looks good.");
} else if (orientation === "landscape-secondary") {
console.log("Mmmh... the screen is upside down!");
} else if (
orientation === "portrait-secondary" ||
orientation === "portrait-primary"
) {
console.log("Mmmh... you should rotate your device to landscape");
} else if (orientation === undefined) {
console.log("The orientation API isn't supported in this browser :(");
}
è§è
| è§è |
|---|
| Screen Orientation > # dom-screen-orientation > |