2017-03-22 4 views
0

Ich möchte den API-Bildschirm in Js mit Chrom verwenden.screen.lockOrientation ist keine Funktion

if (navigator.userAgent.match(/(android|iphone)/gi)) { 
     if ('orientation' in screen) { 
      //console.log('// API supported, yeah!'); 
      //console.log('new orientation is ', screen.orientation); 
      screen.lockOrientation('landscape'); 
     } else { 
      console.log('// API not supported '); 
     } 
    } else { 
     //alert('none'); 
    } 

mein Fehler js: gefangen Typeerror: screen.lockOrientation ist keine Funktion

/* andere */

if (navigator.userAgent.match(/(android|iphone)/gi)) { 
    if ('orientation' in screen) { 
     let locOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation; 
     locOrientation('landscape'); 
     console.log('// API supported, yeah!' , locOrientation); 
     console.log('new orientation is ', screen.orientation); 
     //screen.lockOrientation('landscape'); 
    } else { 
     console.log('// API not supported '); 
    } 
} else { 
    //alert('none'); 
} 

mein Fehler js: locOrientation ist keine Funktion

update: 20/04/2017 -> Mit Javascript können wir die Orientierung nicht mit dem Navigator sperren.

+0

ist dies für eine normale Seite oder eine App? – dandavis

+0

eine einfache Webseite, mein Setup: (webpack, vuejs) – Seb

+0

es ist nicht für Seiten ... – dandavis

Antwort

0

Dies ist, weil lockOrientation immer noch das Präfix ist. Siehe die [2] Fußnote in this MDN Artikel. Auch in Chrome ist es unter orientation.lock. Siehe Fußnote [1] im MDN-Artikel.

locOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation || screen.orientation.lock; 
locOrientation('landscape'); 

Bitte beachten Sie, dass nicht alle Browser diese Funktion haben. Bitte überprüfen Sie, ob locOrientation eingestellt ist, bevor Sie es verwenden.

Bearbeiten: Hinzufügen der neuere orientation.lock zum Beispiel.

+0

Ich habe ein Problem mit Ihrem Quellcode, ich aktualisiere meinen Beitrag – Seb

+0

Ah, Chrome hat es unter orientation.lock! – JosiahDaniels

+0

Ich bevorzuge es, Navigator zu zwingen, eine Ausrichtung zu sperren – Seb