JS實(shí)現(xiàn)全屏的四種寫(xiě)法
JS或jQuery實(shí)現(xiàn)全屏
JS寫(xiě)法一
.html
<div class="container" <button id="full-screen">全屏</button> <button id="exit-fullscreen">退出</button> </div>
.css
/* Basic element styles */
html {
color: #000;
background: paleturquoise;
min-height: 100%;
}
/* Structure */
.container {
text-align: center;
width: 500px;
min-height: 600px;
background: #fff;
border: 1px solid #ccc;
border-top: none;
margin: 20px auto;
padding: 20px;
border-radius: 10px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
box-shadow: 1px 1px 10px #000;
-moz-box-shadow: 1px 1px 10px #000;
-webkit-box-shadow: 1px 1px 5px #000;
}
button{
margin: 200px auto;
width: 100px;
height: 30px;
background-color: aliceblue;
}
/* Fullscreen */
html:-moz-full-screen {
background: blue;
}
html:-webkit-full-screen {
background: blue;
}
html:-ms-fullscreen {
background: blue;
width: 100%; /* needed to center contents in IE */
}
html:fullscreen {
background: blue;
}
.js
(function () {
var viewFullScreen = document.getElementById("full-screen");
if (viewFullScreen) {
viewFullScreen.addEventListener("click", function () {
var docElm = document.documentElement;
if (docElm.requestFullscreen) {
docElm.requestFullscreen();
}
else if (docElm.msRequestFullscreen) {
docElm.msRequestFullscreen();
}
else if (docElm.mozRequestFullScreen) {
docElm.mozRequestFullScreen();
}
else if (docElm.webkitRequestFullScreen) {
docElm.webkitRequestFullScreen();
}
}, false);
}
var cancelFullScreen = document.getElementById("exit-fullscreen");
if (cancelFullScreen) {
cancelFullScreen.addEventListener("click", function () {
if (document.exitFullscreen) {
document.exitFullscreen();
}
else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
}
else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
}
}, false);
}
var fullscreenState = document.getElementById("fullscreen-state");
if (fullscreenState) {
document.addEventListener("fullscreenchange", function () {
fullscreenState.innerHTML = (document.fullscreenElement)? "" : "not ";
}, false);
document.addEventListener("msfullscreenchange", function () {
fullscreenState.innerHTML = (document.msFullscreenElement)? "" : "not ";
}, false);
document.addEventListener("mozfullscreenchange", function () {
fullscreenState.innerHTML = (document.mozFullScreen)? "" : "not ";
}, false);
document.addEventListener("webkitfullscreenchange", function () {
fullscreenState.innerHTML = (document.webkitIsFullScreen)? "" : "not ";
}, false);
}
})();
JS寫(xiě)法二
.html
<div style="margin:0 auto;height:600px;width:700px;"> <button id="btn">全屏</button> <div id="content" style="margin:0 auto;height:500px;width:700px; background:#ccc;" > <h1>全屏展示和退出全屏</h1> </div> </div>
.js
document.getElementById("btn").onclick=function(){
var elem = document.getElementById("content");
requestFullScreen(elem);
/*
注意這里的樣式的設(shè)置表示全屏顯示之后的樣式,
退出全屏后樣式還在,
若要回到原來(lái)樣式,需在退出全屏里把樣式還原回去
(見(jiàn)寫(xiě)法三)
*/
elem.style.height = '800px';
elem.style.width = '1000px';
};
function requestFullScreen(element) {
var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || 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}");
}
}
}
JS寫(xiě)法三
.html
<div style="margin:0 auto;height:600px;width:700px;"> <div id="content" style="margin:0 auto;height:500px;width:700px; background:#ccc;" > <button id="btn">全屏</button> <h1>全屏展示和退出全屏</h1> <button id="btnn" >退出</button> </div> </div>
.js
document.getElementById("btn").onclick=function(){
var elem = document.getElementById("content");
requestFullScreen(elem);
/*
注意這里的樣式的設(shè)置表示全屏顯示之后的樣式,
退出全屏后樣式還在,
若要回到原來(lái)樣式,需在退出全屏里把樣式還原回去
*/
elem.style.height = '800px';
elem.style.width = '1000px';
};
document.getElementById("btnn").onclick=function () {
exitFullscreen();
};
/*
全屏顯示
*/
function requestFullScreen(element) {
var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
requestMethod.call(element);
};
/*
全屏退出
*/
function exitFullscreen() {
var elem = document;
var elemd = document.getElementById("content");
if (elem.webkitCancelFullScreen) {
elem.webkitCancelFullScreen();
} else if (elem.mozCancelFullScreen) {
elemd.mozCancelFullScreen();
} else if (elem.cancelFullScreen) {
elem.cancelFullScreen();
} else if (elem.exitFullscreen) {
elem.exitFullscreen();
} else {
//瀏覽器不支持全屏API或已被禁用
};
/*
退出全屏后樣式還原
*/
elemd.style.height = '500px';
elemd.style.width = '700px'
}
jQuery寫(xiě)法四
.html
<div id="cont" STYLE="width: 500px;height: 300px;background-color: aliceblue;margin: auto"> <button id="btn">全屏&退出</button> </div>
.css
.full{
position: fixed;
align-content: center;
/*top: 10px;*/
/*left: 10px;*/
/*
原來(lái)基礎(chǔ)的百分百
*/
width: 100%;
height: 100%;
overflow: auto;
}
fullScreen.js
(function ($) {
// Adding a new test to the jQuery support object
$.support.fullscreen = supportFullScreen();
// Creating the plugin
$.fn.fullScreen = function (props) {
if (!$.support.fullscreen || this.length != 1) {
// The plugin can be called only
// on one element at a time
return this;
}
if (fullScreenStatus()) {
// if we are already in fullscreen, exit
cancelFullScreen();
return this;
}
// You can potentially pas two arguments a color
// for the background and a callback function
var options = $.extend({
'background': '#111',
'callback': function () {}
}, props);
// This temporary div is the element that is
// actually going to be enlarged in full screen
var fs = $('<div>', {
'css': {
'overflow-y': 'auto',
'background': options.background,
'width': '100%',
'height': '100%',
'align': 'center'
}
});
var elem = this;
// You can use the .fullScreen class to
// apply styling to your element
elem.addClass('fullScreen');
// Inserting our element in the temporary
// div, after which we zoom it in fullscreen
fs.insertBefore(elem);
fs.append(elem);
requestFullScreen(fs.get(0));
fs.click(function (e) {
if (e.target == this) {
// If the black bar was clicked
cancelFullScreen();
}
});
elem.cancel = function () {
cancelFullScreen();
return elem;
};
onFullScreenEvent(function (fullScreen) {
if (!fullScreen) {
// We have exited full screen.
// Remove the class and destroy
// the temporary div
elem.removeClass('fullScreen').insertBefore(fs);
fs.remove();
}
// Calling the user supplied callback
options.callback(fullScreen);
});
return elem;
};
// These helper functions available only to our plugin scope.
function supportFullScreen() {
var doc = document.documentElement;
return ('requestFullscreen' in doc) ||
('mozRequestFullScreen' in doc && document.mozFullScreenEnabled) ||
('webkitRequestFullScreen' in doc);
}
function requestFullScreen(elem) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) {
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullScreen) {
elem.webkitRequestFullScreen();
}
}
function fullScreenStatus() {
return document.fullscreen ||
document.mozFullScreen ||
document.webkitIsFullScreen;
}
function cancelFullScreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
}
}
function onFullScreenEvent(callback) {
$(document).on("fullscreenchange mozfullscreenchange webkitfullscreenchange", function () {
// The full screen status is automatically
// passed to our callback as an argument.
callback(fullScreenStatus());
});
}
})(jQuery);
myJS.js
$(function () {
$("#btn").click(function () {
$("#cont").fullScreen();
})
});
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS前端開(kāi)發(fā)之exec()和match()的對(duì)比使用
match()方法可在字符串內(nèi)檢索指定的值,或找到一個(gè)或多個(gè)正則表達(dá)式的匹配,下面這篇文章主要給大家介紹了關(guān)于JS前端開(kāi)發(fā)之exec()和match()的對(duì)比使用的相關(guān)資料,需要的朋友可以參考下2022-09-09
iframe窗口高度自適應(yīng)的又一個(gè)巧妙實(shí)現(xiàn)思路
這篇文章主要介紹了實(shí)現(xiàn)iframe窗口高度自適應(yīng)的又一個(gè)巧妙思路,需要的朋友可以參考下2014-04-04
JavaScript 判斷對(duì)象中是否有某屬性的常用方法
判斷對(duì)象中是否有某屬性的常見(jiàn)方式總結(jié),不同的場(chǎng)景要使用不同的方式。這篇文章給大家介紹了JavaScript 判斷對(duì)象中是否有某屬性的常用方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-06-06
javascript實(shí)現(xiàn)數(shù)組中的內(nèi)容隨機(jī)輸出
本文實(shí)例講述了javaScript數(shù)組隨機(jī)排列實(shí)現(xiàn)隨機(jī)洗牌功能的方法。分享給大家供大家參考。2015-08-08
一個(gè)簡(jiǎn)單的JS鼠標(biāo)懸停特效具體方法
這個(gè)特效最終實(shí)現(xiàn)效果就是當(dāng)鼠標(biāo)移動(dòng)到鏈接上,文字會(huì)橫向移動(dòng)一定距離,貌似總有人喜歡這些花花草草。添加此效果方法很簡(jiǎn)單。2013-06-06

