2015-06-15 64 views
4

嗨,我是初學者,我想創建一個web應用程序,需要在全屏幫助的時候頁面載入...沒有用戶交互如何無需用戶交互初始化全屏

我有這樣的事情在單擊功能正常工作......但我想在開始頁面加載功能

addEventListener("click", function() 
    { 
     var el = document.documentElement , rfs = el.requestFullScreen || 
     el.webkitRequestFullScreen || el.mozRequestFullScreen ; 
     rfs.call(el); 
    }); 

有人幫我:)

謝謝!

+1

檢查[DOMContentLoaded](https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded)。你也可以嘗試'(function(){/ *你的代碼全屏* /})();' –

+0

謝謝你的建議。我檢查了「DOMContentLoaded」與我的代碼...功能不起作用:(。當替換任何其他代碼例如 「function myFunction(){alert(」Hello「);}」它的工作...... .........爲什麼會發生這種情況? – siasty

+2

您無法強制用戶在沒有用戶操作的情況下全屏顯示 – fcalderan

回答

2

這是不可能的。

我跑到下面的代碼片段在我的瀏覽器控制檯:

var e = document.getElementById('answers'); 
(e.webkitRequestFullScreen || e.mozRequestFullScreen).apply(e); 

鉻告訴我:

無法執行「requestFullScreen」上「元素」:API只能由用戶發起手勢。

Firefox的告訴我說:

請求全屏被拒絕,因爲Element.mozRequestFullScreen()不是從短期運行的用戶生成的事件處理程序內調用。

也就是說到位,以防止濫用,類似於上window.open的限制(例如,見thisthis問題)。

0

您試試這個。

$(document).ready(function() { 
    $('html').click(function() { 
     if (screenfull.isFullscreen !== true) { 
      screenfull.toggle(); 
     } 
    }); 
}); 

jQuery插件

/*! 
* screenfull 
* v3.0.0 - 2015-11-24 
* (c) Sindre Sorhus; MIT License 
*/ 
!function() { 
    "use strict"; 
    var a = "undefined" != typeof module && module.exports, b = "undefined" != typeof Element && "ALLOW_KEYBOARD_INPUT"in Element, c = function() { 
     for (var a, b, c = [["requestFullscreen", "exitFullscreen", "fullscreenElement", "fullscreenEnabled", "fullscreenchange", "fullscreenerror"], ["webkitRequestFullscreen", "webkitExitFullscreen", "webkitFullscreenElement", "webkitFullscreenEnabled", "webkitfullscreenchange", "webkitfullscreenerror"], ["webkitRequestFullScreen", "webkitCancelFullScreen", "webkitCurrentFullScreenElement", "webkitCancelFullScreen", "webkitfullscreenchange", "webkitfullscreenerror"], ["mozRequestFullScreen", "mozCancelFullScreen", "mozFullScreenElement", "mozFullScreenEnabled", "mozfullscreenchange", "mozfullscreenerror"], ["msRequestFullscreen", "msExitFullscreen", "msFullscreenElement", "msFullscreenEnabled", "MSFullscreenChange", "MSFullscreenError"]], d = 0, e = c.length, f = {}; e > d; d++) 
      if (a = c[d], a && a[1]in document) { 
       for (d = 0, b = a.length; b > d; d++) 
        f[c[0][d]] = a[d]; 
       return f; 
      } 
     return!1; 
    }(), d = {request: function (a) { 
      var d = c.requestFullscreen; 
      a = a || document.documentElement, /5\.1[\.\d]* Safari/.test(navigator.userAgent) ? a[d]() : a[d](b && Element.ALLOW_KEYBOARD_INPUT); 
     }, exit: function() { 
      document[c.exitFullscreen](); 
     }, toggle: function (a) { 
      this.isFullscreen ? this.exit() : this.request(a); 
     }, raw: c}; 
    return c ? (Object.defineProperties(d, {isFullscreen: {get: function() { 
       return Boolean(document[c.fullscreenElement]); 
      }}, element: {enumerable: !0, get: function() { 
       return document[c.fullscreenElement] 
      }}, enabled: {enumerable: !0, get: function() { 
       return Boolean(document[c.fullscreenEnabled]); 
      }}}), void(a ? module.exports = d : window.screenfull = d)) : void(a ? module.exports = !1 : window.screenfull = !1); 
}();