滚动到页面顶部
export const scrollToTop = () => {
const height = document.documentElement.scrollTop || document.body.scrollTop;
if (height > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, height - height / 8);
}
}
滚动到页面底部
export const scrollToBottom = () => {
window.scrollTo(0, document.documentElement.clientHeight);
}
滚动到指定元素区域
export const smoothScroll = (element) => {
document.querySelector(element).scrollIntoView({
behavior: 'smooth'
});
};
获取可视窗口高度
export const getClientHeight = () => {
let clientHeight = 0;
if (document.body.clientHeight && document.documentElement.clientHeight) {
clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
}
else {
clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
}
return clientHeight;
}
获取可视窗口宽度
export const getPageViewWidth = () => {
return (document.compatMode == "BackCompat" ? document.body : document.documentElement).clientWidth;
}
打开浏览器全屏
export const toFullScreen = () => {
let element = document.body;
if (element.requestFullscreen) {
element.requestFullscreen()
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen()
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen()
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullScreen()
}
}
退出浏览器全屏
export const exitFullscreen = () => {
if (document.exitFullscreen) {
document.exitFullscreen()
} else if (document.msExitFullscreen) {
document.msExitFullscreen()
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen()
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen()
}
}
以上是编程学习网小编为您介绍的“JavaScript开发小技巧之各种浏览器操作”的全面内容,想了解更多关于 vuejs 内容,请继续关注编程基础学习网。
沃梦达教程
本文标题为:JavaScript开发小技巧之各种浏览器操作


猜你喜欢
- 进一步理解CSS编程中的块级元素和行内元素 2024-01-03
- Javascript脚本获取form和input内容的方法(两种方法) 2023-07-10
- css实现0.5像素的边框的示例代码 2024-02-21
- Ajax如何传输Json和xml数据 2023-01-21
- css中clearfix清除浮动的用法及其原理示例介绍 2024-02-07
- IE6常见bug附解决方法 2024-02-06
- 深入理解JavaScript系列(22):S.O.L.I.D五大原则之依赖倒置原则DIP详解 2024-01-17
- 浅谈React Router关于history的那些事 2024-01-17
- Ajax跨域问题及解决方案(jsonp,cors) 2023-02-23
- 细说CSS中margin属性的使用 2023-12-14