如何让div全屏显示?

发布于 2024-11-30 16:21:19 字数 351 浏览 2 评论 0原文

我正在使用 Flot 来绘制我的一些数据,我认为这会很棒单击按钮后使该图全屏显示(占据显示器上的全部空间)。目前,我的div如下:

<div id="placeholder" style="width:800px;height:600px"></div>

当然,style属性仅用于测试。在实际设计过程中,我会将其移至 CSS 中。无论如何,我可以让这个 div 全屏显示并仍然保留所有事件处理吗?

I am using Flot to graph some of my data and I was thinking it would be great to make this graph appear fullscreen (occupy full space on the monitor) upon clicking on a button. Currently, my div is as follows:

<div id="placeholder" style="width:800px;height:600px"></div>

Of course, the style attribute is only for testing. I will move this to CSS after during the actual design. Is there anyway I could make this div fullscreen and still preserve all event handling?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

枕梦 2024-12-07 16:21:19

您可以使用 HTML5 Fullscreen API 来实现此目的(这是我认为最合适的方式)。

全屏必须通过用户事件(单击、按键)触发,否则将无法工作。

这是一个按钮,单击后会使 div 全屏显示。在全屏模式下,单击按钮将退出全屏模式。

$('#toggle_fullscreen').on('click', function(){
  // if already full screen; exit
  // else go fullscreen
  if (document.fullscreenElement) {
    document.exitFullscreen();
  } else {
    $('#container').get(0).requestFullscreen();
  }
});
#container{
  border:1px solid red;
  border-radius: .5em;
  padding:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <p>
    <a href="#" id="toggle_fullscreen">Toggle Fullscreen</a>
  </p>
  I will be fullscreen, yay!
</div>

另请注意,Chrome 的全屏 API 不适用于非安全页面。请参阅 https请访问://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins了解更多详情。

另一件需要注意的是 :fullscreen CSS 选择器。您可以将其附加到任何 css 选择器,以便在该元素全屏显示时应用规则:

#container:fullscreen {
    width: 100vw;
    height: 100vh;
    }

You can use HTML5 Fullscreen API for this (which is the most suitable way i think).

The fullscreen has to be triggered via a user event (click, keypress) otherwise it won't work.

Here is a button which makes the div fullscreen on click. And in fullscreen mode, the button click will exit fullscreen mode.

$('#toggle_fullscreen').on('click', function(){
  // if already full screen; exit
  // else go fullscreen
  if (document.fullscreenElement) {
    document.exitFullscreen();
  } else {
    $('#container').get(0).requestFullscreen();
  }
});
#container{
  border:1px solid red;
  border-radius: .5em;
  padding:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <p>
    <a href="#" id="toggle_fullscreen">Toggle Fullscreen</a>
  </p>
  I will be fullscreen, yay!
</div>

Please also note that Fullscreen API for Chrome does not work in non-secure pages. See https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins for more details.

Another thing to note is the :fullscreen CSS selector. You can append this to any css selector so that the rules will be applied when that element is fullscreen:

#container:fullscreen {
    width: 100vw;
    height: 100vh;
    }
追风人 2024-12-07 16:21:19

当您说“全屏”时,您是指计算机的全屏,还是占用浏览器中的整个空间?

您不能强制用户进入全屏 F11;但是,您可以使用以下 CSS 使您的 div 全屏显示。

div {width: 100%; height: 100%;}

这当然会假设您的 div 是 标记的子级。否则,除了上述代码之外,您还需要添加以下内容。

div {position: absolute; top: 0; left: 0;}

When you say "full-screen", do you mean like full-screen for the computer, or for taking up the entire space in the browser?

You can't force the user into full-screen F11; however, you can make your div full screen by using the following CSS

div {width: 100%; height: 100%;}

This will of course assume your div is child of the <body> tag. Otherwise, you'd need to add the following in addition to the above code.

div {position: absolute; top: 0; left: 0;}
万水千山粽是情ミ 2024-12-07 16:21:19

CSS方式:

#foo {
   position: absolute;
   top: 0;
   right: 0;
   bottom: 0;
   left: 0;
}

JS方式:

$(function() {
    function abso() {
        $('#foo').css({
            position: 'absolute',
            width: $(window).width(),
            height: $(window).height()
        });
    }

    $(window).resize(function() {
        abso();         
    });

    abso();
});

CSS way:

#foo {
   position: absolute;
   top: 0;
   right: 0;
   bottom: 0;
   left: 0;
}

JS way:

$(function() {
    function abso() {
        $('#foo').css({
            position: 'absolute',
            width: $(window).width(),
            height: $(window).height()
        });
    }

    $(window).resize(function() {
        abso();         
    });

    abso();
});
傲鸠 2024-12-07 16:21:19

对于全屏浏览器渲染区域,有一个所有现代浏览器都支持的简单解决方案。

div#placeholder {
    height: 100vh;
}

唯一值得注意的例外是 Android 4.3 以下 - 但 ofc 仅在系统浏览器/webview 元素中(Chrome 工作正常)。

浏览器支持图表:http://caniuse.com/viewport-units

对于显示器的全屏,请使用 HTML5 全屏应用程序编程接口

For fullscreen of browser rendering area there is a simple solution supported by all modern browsers.

div#placeholder {
    height: 100vh;
}

The only notable exception is the Android below 4.3 - but ofc only in the system browser/webview element (Chrome works ok).

Browser support chart: http://caniuse.com/viewport-units

For fullscreen of monitor please use HTML5 Fullscreen API

好久不见√ 2024-12-07 16:21:19
.widget-HomePageSlider .slider-loader-hide {
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    z-index: 10000;
    background: white;
}
.widget-HomePageSlider .slider-loader-hide {
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    z-index: 10000;
    background: white;
}
白云不回头 2024-12-07 16:21:19

一样使用 FullScreen API

function toggleFullscreen() {
  let elem = document.querySelector('#demo-video');

  if (!document.fullscreenElement) {
    elem.requestFullscreen().catch(err => {
      alert(`Error attempting to enable full-screen mode: ${err.message} (${err.name})`);
    });
  } else {
    document.exitFullscreen();
  }
}

可以像这个Demo

const elem = document.querySelector('#park-pic');

elem.addEventListener("click", function(e) {
  toggleFullScreen();
}, false);

function toggleFullScreen() {

  if (!document.fullscreenElement) {
    elem.requestFullscreen().catch(err => {
      alert(`Error attempting to enable full-screen mode: ${err.message} (${err.name})`);
    });
  } else {
    document.exitFullscreen();
  }
}
#container{
  border:1px solid #aaa;
  padding:10px;
}
#park-pic {
  width: 100%;
  max-height: 70vh;
}
<div id="container">
  <p>
    <a href="#" id="toggle-fullscreen">Toggle Fullscreen</a>
  </p>
  <img id="park-pic"
      src="https://storage.coverr.co/posters/Skate-park"></video>
</div>

PS:现在使用 screenfull.js 。一个简单的包装器,用于 JavaScript 全屏 API 的跨浏览器使用。

Can use FullScreen API like this

function toggleFullscreen() {
  let elem = document.querySelector('#demo-video');

  if (!document.fullscreenElement) {
    elem.requestFullscreen().catch(err => {
      alert(`Error attempting to enable full-screen mode: ${err.message} (${err.name})`);
    });
  } else {
    document.exitFullscreen();
  }
}

Demo

const elem = document.querySelector('#park-pic');

elem.addEventListener("click", function(e) {
  toggleFullScreen();
}, false);

function toggleFullScreen() {

  if (!document.fullscreenElement) {
    elem.requestFullscreen().catch(err => {
      alert(`Error attempting to enable full-screen mode: ${err.message} (${err.name})`);
    });
  } else {
    document.exitFullscreen();
  }
}
#container{
  border:1px solid #aaa;
  padding:10px;
}
#park-pic {
  width: 100%;
  max-height: 70vh;
}
<div id="container">
  <p>
    <a href="#" id="toggle-fullscreen">Toggle Fullscreen</a>
  </p>
  <img id="park-pic"
      src="https://storage.coverr.co/posters/Skate-park"></video>
</div>

P.S: Using screenfull.js nowadays. A simple wrapper for cross-browser usage of the JavaScript Fullscreen API.

丢了幸福的猪 2024-12-07 16:21:19

这是最简单的一个。

#divid {
   position: fixed;
   top: 0;
   right: 0;
   bottom: 0;
   left: 0;
}

This is the simplest one.

#divid {
   position: fixed;
   top: 0;
   right: 0;
   bottom: 0;
   left: 0;
}
尐籹人 2024-12-07 16:21:19

你可以尝试这个..

<div id="placeholder" style="width:auto;height:auto"></div>

宽度和高度取决于你的flot或图表..

希望你想要这个...

或者

通过点击,你可以通过jquery使用它

$("#placeholder").css("width", $(window).width());
$("#placeholder").css("height", $(window).height());

u can try this..

<div id="placeholder" style="width:auto;height:auto"></div>

width and height depends on your flot or graph..

hope u want this...

or

By clicking, u can use this by jquery

$("#placeholder").css("width", $(window).width());
$("#placeholder").css("height", $(window).height());
仄言 2024-12-07 16:21:19

如果您想将其显示在浏览器的可见区域(可滚动区域)之外,请使用文档高度。

CSS 部分

#foo {
    position:absolute;
    top:0;
    left:0;
}

JQuery 部分

$(document).ready(function() {
   $('#foo').css({
       width: $(document).width(),
       height: $(document).height()
   });
});

Use document height if you want to show it beyond the visible area of browser(scrollable area).

CSS Portion

#foo {
    position:absolute;
    top:0;
    left:0;
}

JQuery Portion

$(document).ready(function() {
   $('#foo').css({
       width: $(document).width(),
       height: $(document).height()
   });
});
忆离笙 2024-12-07 16:21:19
<div id="placeholder" style="position:absolute; top:0; right:0; bottom:0; left:0;"></div>
<div id="placeholder" style="position:absolute; top:0; right:0; bottom:0; left:0;"></div>
洒一地阳光 2024-12-07 16:21:19

有了 Bootstrap 5.0,现在这变得非常简单。只需在全屏元素上打开和关闭这些类即可。

w-100 h-100 position-absolute top-0 start-0 bg-white

With Bootstrap 5.0 this is incredibly easy now. Just toggle these classes on and off the full screen element.

w-100 h-100 position-absolute top-0 start-0 bg-white
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文