滚动到页面顶部
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开发小技巧之各种浏览器操作
基础教程推荐
猜你喜欢
- JavaScript canvas复刻苹果发布会环形进度条 2022-08-30
- 举例详解CSS的z-index属性的使用 2023-12-09
- apply和call方法定义及apply和call方法的区别 2023-12-15
- 关于 css:JavaScript 选择器的类前缀? 2022-09-21
- Bootstrap导航条可点击和鼠标悬停显示下拉菜单 2023-12-20
- Vue 中对计算属性的一点理解 针对get set方法 2023-10-08
- 不使用hover外部CSS样式实现hover鼠标悬停改变样式 2023-12-21
- css 行级元素在多浏览器下的宽度问题 与解决方法 2024-03-09
- 从 ExtJs JsonStore 访问 piggybag 属性,有可能吗? 2022-09-15
- 基于 Immutable.js 实现撤销重做功能的实例代码 2024-02-11
