function fullScreen() {
const el = document.documentElement
const rfs =
el.requestFullScreen ||
el.webkitRequestFullScreen ||
el.mozRequestFullScreen ||
el.msRequestFullscreen
if(typeof rfs != "undefined" && rfs) {
rfs.call(el)
}
}
fullScreen()
当您需要退出全屏时
为盐湖等地区用户提供了全套网页设计制作服务,及盐湖网站建设行业解决方案。主营业务为做网站、成都做网站、盐湖网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
function exitScreen() {
if (document.exitFullscreen) {
document.exitFullscreen()
}
else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen()
}
else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen()
}
else if (document.msExitFullscreen) {
document.msExitFullscreen()
}
if(typeof cfs != "undefined" && cfs) {
cfs.call(el)
}
}
exitScreen()
当您需要打印当前页面时
window.print()
当需要打印出当前页面,但需要修改当前布局时
print
当需要阻止用户刷新或关闭浏览器时,可以选择触发beforeunload事件,有些浏览器无法自定义文本内容
window.onbeforeunload = function(){
return 'Are you sure you want to leave the haorooms blog?';
};
当您需要录制当前屏幕并上传或下载屏幕录制时
const streamPromise = navigator.mediaDevices.getDisplayMedia()
streamPromise.then(stream => {
var recordedChunks = [];// recorded video data
var options = { mimeType: "video/webm; codecs=vp9" };// Set the encoding format
var mediaRecorder = new MediaRecorder(stream, options);// Initialize the MediaRecorder instance
mediaRecorder.ondataavailable = handleDataAvailable;// Set the callback when data is available (end of screen recording)
mediaRecorder.start();
// Video Fragmentation
function handleDataAvailable(event) {
if (event.data.size > 0) {
recordedChunks.push(event.data);// Add data, event.data is a BLOB object
download();// Encapsulate into a BLOB object and download
}
}
// file download
function download() {
var blob = new Blob(recordedChunks, {
type: "video/webm"
});
// Videos can be uploaded to the backend here
var url = URL.createObjectURL(blob);
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.download = "test.webm";
a.click();
window.URL.revokeObjectURL(url);
}
})
当您需要判断手机横屏或竖屏状态时
const streamPromise = navigator.mediaDevices.getDisplayMedia()
streamPromise.then(stream => {
var recordedChunks = [];// recorded video data
var options = { mimeType: "video/webm; codecs=vp9" };// Set the encoding format
var mediaRecorder = new MediaRecorder(stream, options);// Initialize the MediaRecorder instance
mediaRecorder.ondataavailable = handleDataAvailable;// Set the callback when data is available (end of screen recording)
mediaRecorder.start();
// Video Fragmentation
function handleDataAvailable(event) {
if (event.data.size > 0) {
recordedChunks.push(event.data);// Add data, event.data is a BLOB object
download();// Encapsulate into a BLOB object and download
}
}
// file download
function download() {
var blob = new Blob(recordedChunks, {
type: "video/webm"
});
// Videos can be uploaded to the backend here
var url = URL.createObjectURL(blob);
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.download = "test.webm";
a.click();
window.URL.revokeObjectURL(url);
}
})
当你需要为横竖屏设置不同的样式时
当需要监听tab显示和隐藏事件时
const {hidden, visibilityChange} = (() => {
let hidden, visibilityChange;
if (typeof document.hidden !== "undefined") {
// Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
return {
hidden,
visibilityChange
}
})();
const handleVisibilityChange = () => {
console.log("currently hidden", document[hidden]);
};
document.addEventListener(
visibilityChange,
handleVisibilityChange,
false
);
图片
当您从客户端获取图片但无法立即上传到服务器,但需要预览时
当图片较多时,需要预加载图片以避免白屏
const images = []
function preloader(args) {
for (let i = 0, len = args.length; i < len; i++) {
images[i] = new Image()
images[i].src = args[i]
}
}
preloader(['1.png', '2.jpg'])
JavaScript
当需要将一串字符串转换为JavaScript脚本时,该方法存在xss漏洞,请谨慎使用
const obj = eval('({ name: "jack" })')
// obj will be converted to object{ name: "jack" }
const v = eval('obj')
// v will become the variable obj
当需要编写递归函数时,声明了函数名,但是每次修改函数名时,总是忘记修改内部函数名。argument是函数的内部对象,包括传入函数的所有参数,arguments.callee代表函数名。
// This is a basic Fibonacci sequence
function fibonacci (n) {
const fn = arguments.callee
if (n <= 1) return 1
return fn(n - 1) + fn(n - 2)
}
DOM 元素
当需要判断某个dom元素当前是否出现在页面视图中时,可以尝试使用IntersectionObserver来判断。
Invisible
Invisible
Invisible
当你需要编辑一个dom元素时,让它像文本区域一样点击。
here can be edited
test
当开发过程中需要打印dom元素时,使用console.log往往只能打印出整个dom元素,而无法查看dom元素的内部属性。你可以尝试使用 console.dir
console.dir(document.body)
其他
当你在移动端开发时,你需要打开其他应用程序。还可以通过location.href赋值来操作以下方法
phone
android message
ios message
ios message
以上就是我今天想与你分享的全部内容,希望对你有所帮助,最后,感谢你的阅读,祝编程愉快!
名称栏目:18个高级工程师必须会的强大JavaScript技巧
网站链接:http://www.gawzjz.com/qtweb2/news33/16283.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联