Javascript full screen display of page elements

Source: Internet
Author: User

The simplest way is to dynamically change the style of the part you want to display in full screen. For example, change position to absolute, and set both height and width to the window size, and change the background color to full White (to cover the remaining elements on the page ). In this way, you can only see the parts you want to highlight on the webpage, visually equivalent to full screen. At the same time, you can use javascript to listen to Keyboard Events. Once the user presses the ESc return key, it will be restored. Some code is as follows:

Copy codeThe Code is as follows:
Document. onkeydown = function (event ){
Var e = event | window. event | arguments. callee. caller. arguments [0];
If (e & e. keyCode = 27) {// ESC key

$ ('. Navbar-inner'). fadeIn (100 );
Var maintable = document. getElementById ("holder ");
Maintable. style. position = "relative ";
Maintable. style. height = "100% ";
Maintable. style. width = "100% ";
Maintable = document. getElementById ("main ");
Maintable. style. height = "100% ";
Maintable. style. width = "100% ";
Maintable. style. left = 0 + "px ";
Maintable. style. top = 0 + "px ";
ResizePlots ();
}
};


FullScreenClick: function (){

$ ('. Navbar-inner'). fadeOut (100 );

Var maintable = document. getElementById ("holder ");
Maintable. style. position = "absolute ";
Maintable. style. background = "# fff ";
// Maintable. style. zIndex = 5;
Maintable. style. height = $ (window). height () + "px ";
Maintable. style. width = $ (window). width () + "px ";
Maintable. style. left = 0 + "px ";
Maintable. style. top = 0 + "px ";
Maintable = document. getElementById ("main ");
Maintable. style. height = "90% ";
Maintable. style. width = "90% ";
Maintable. style. left = $ (window). width () * 0.05 + "px ";
Maintable. style. top = $ (window). height () * 0.02 + "px ";
ResizePlots ();
},

However, the disadvantage is that you need to manually press F11 to achieve real full screen.

The following method does not require F11:

Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Body>

<Button id = "btn"> full screen </button>

<Div id = "content" style = "height: 500px; width: 500px; background: # fff">
<H1> welcome to Weibo! </H1>
<H2> weibo.com/leavingseason <P> believe in music, believe in day 1 </p>
</Div>

</Body>

<Script language = "JavaScript">
Document. getElementById ("btn"). onclick = function (){
Var elem = document. getElementById ("content ");
RequestFullScreen (elem );
};

Function requestFullScreen (element ){

Var requestMethod = element. requestFullScreen | element. webkitRequestFullScreen | element. Required requestFullScreen | element. msRequestFullScreen;

If (requestMethod ){
RequestMethod. call (element );
} Else if (typeof window. ActiveXObject! = "Undefined "){
Var wscript = new ActiveXObject ("WScript. Shell ");
If (wscript! = Null ){
Wscript. SendKeys ("{F11 }");
}
}
}

</Script>
</Html>

This supports most browsers. However, the annoying IE still does not support the full screen function of HTML5. You need to simulate the F11 action. You can see it in the code.

You can also exit the full screen interface in the Code:

Copy codeThe Code is as follows:
Function cancelFullScreen (el ){
Var requestMethod = el. cancelFullScreen | el. webkitCancelFullScreen | el. canccancelfullscreen | el. exitFullscreen;
If (requestMethod) {// cancel full screen.
RequestMethod. call (el );
} Else if (typeof window. ActiveXObject! = "Undefined") {// Older IE.
Var wscript = new ActiveXObject ("WScript. Shell ");
If (wscript! = Null ){
Wscript. SendKeys ("{F11 }");
}
}
}

I am still curious about full screen display. How can video websites achieve full screen functionality compatible with IE and other browsers. If anyone knows this, please share it with us. Thank you very much.

Updated (2013/09/22)

Most of the time, I want to do some customized things during full screen switching. You can bind the event as follows:

Copy codeThe Code is as follows:
Document. addEventListener ("fullscreenchange", function (){
Doit ();
}, False );

Document. addEventListener ("mozfullscreenchange", function (){
Doit ();
}, False );

Document. addEventListener ("webkitfullscreenchange", function (){
Doit ();
}, False );

It triggers the doit () operation every time it enters or exits the full screen.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.