JS实现全屏的四种写法

前端之家收集整理的这篇文章主要介绍了JS实现全屏的四种写法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JS或jQuery实现全屏

JS写法一

.html

.css

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

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写法二

.html

全屏展示和退出全屏

.js

显示之后的样式, 退出全屏后样式还在, 若要回到原来样式,需在退出全屏里把样式还原回去 (见写法三) */ 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写法三

.html

.js

显示之后的样式, 退出全屏后样式还在, 若要回到原来样式,需在退出全屏里把样式还原回去 */ 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写法四

.html

退出

.css

fullScreen.js

// 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 = $('

',{
'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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

原文链接:https://www.f2er.com/js/42975.html

猜你在找的JavaScript相关文章