How to put an element in Fullscreen

Asked

Viewed 2,135 times

2

I searched a little about the same on google and most people talk to use

position: absolute;
z-index:9999;
width:100%;
height:100%;
top:0;
left:0;

However the code above only works to put in fullscreen inside the window, and that’s not what I want, I want to use real Fullscreen, the whole user screen and then I found the code below:

if (!document.fullscreenElement &&
      !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement ) {  
    if (document.documentElement.requestFullscreen) {
      document.documentElement.requestFullscreen();
    } else if (document.documentElement.msRequestFullscreen) {
      document.documentElement.msRequestFullscreen();
    } else if (document.documentElement.mozRequestFullScreen) {
      document.documentElement.mozRequestFullScreen();
    } else if (document.documentElement.webkitRequestFullscreen) {
      document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
    }
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    } else if (document.msExitFullscreen) {
      document.msExitFullscreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
      document.webkitExitFullscreen();
    }
  }

This one works perfectly, but puts the whole document in full screen, and I just want a specific div in fullscreen, how can I do this?

They pointed out that this question is the same as this, but it is not, the solutions presented in it are the same that I am asking here to try to adapt to my use. Is there any way to enable full screen browser with Javascript?

  • Already tried to exchange Document for div id?

  • Yes I tried putting so $(div) and getting it by getElementById but it didn’t work

  • You want the element in fullscreen (the one that closes the whole browser, including the address bar (F11)) or just overwrites the other elements of the site (such as Sopt’s snnipet)?

  • @Randrade yes I want to put the element in fullscreen, 100% user monitor

  • Your question already has an answer in this question. See an example of a working answer in Jsfiddle.

  • @Randrade in my question I made clear that this code puts the whole document in fullscreen, I want to put only one element in fullscreen...

  • @You’re right, my fault. I removed the vote

  • @Leoletto if you read the answer to the end and tell me that you do not speak fullscreen there I will believe, but I recommend you read my answer specifically please http://answall.com/a/168938/3635

Show 3 more comments

3 answers

4


You already have the code ready, just change the document for the desired element, as in this example below:

function toggleFullScreen(id) {

  var div = document.getElementById(id);

  if ((document.fullScreenElement && document.fullScreenElement !== null) ||
    (!document.mozFullScreen && !document.webkitIsFullScreen)) {
    if (div.requestFullScreen) {
      div.requestFullScreen();
    } else if (div.mozRequestFullScreen) {
      div.mozRequestFullScreen();
    } else if (div.webkitRequestFullScreen) {
      div.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
    }
  } else {
    if (document.cancelFullScreen) {
      document.cancelFullScreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if (document.webkitCancelFullScreen) {
      document.webkitCancelFullScreen();
    }
  }
}
.red {
  background-color: red;
  height: 200px;
  width: 200px;
}
.blue {
  background-color: blue;
  height: 200px;
  width: 200px;
}
<div id="full">
  <input type="button" value="clique para alternar" onclick="toggleFullScreen('full')">

  <div class="red">
  </div>
</div>

<br/>
<div id="full2">
  <input type="button" value="clique para alternar" onclick="toggleFullScreen('full2')">

  <div class="blue">
  </div>
</div>

For reasons I don’t know, in Sopt’s Snnipet the code doesn’t work. But you can see how it works in Jsfiddle.

For more details, see this question.

  • 1

    Perfect man, just what I needed.

0

Hide the other elements you don’t want (display: None) so only your DIV is in full screen view.

  • This is an option that may work, but if there is a way to put only the div, I would find more "practical"

0

Follow the code below HTML:

<div id="DivFull">
  <canvas style="background-color:red;" />
</div>

<input type="button" value="clique para ativar tela cheia" id="btnFullScreen">

And in JAVASCRIPT:

$("#btnFullScreen").click(function(e){
    var el = document.getElementById("DivFull");

  // Supports most browsers and their versions.
  var requestMethod = el.requestFullScreen || el.webkitRequestFullScreen 
  || el.mozRequestFullScreen || el.msRequestFullScreen;

  if (requestMethod) {

    // Native 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}");
    }
  }
});

FOLLOW THE JSFIDDLE RUNNING:

EXAMPLE

  • It worked well to put the element on fullscreen, but and to return, through the same button? If I press F11 it goes back to normal, but I want to leave the option to go back to normal per button too

  • Include the boot inside the div

  • Already has a button that when being clicked calls the function, but is not leaving the fullscreen

  • In your own example if you use the back button, it does not work

  • Okay, I’m making an example here to solve this problem

  • Sorry to keep you waiting, I have a demand to make here now. Take a look at this library jquery it has the function to open and close the fullscreen https://github.com/private-face/jquery.fullscreen

  • No problems Danilo, but in this case I will use my current method and hide the other elements with css, is more practical than adding a library just for this

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.