window.innerHeight: 表示窗口内容区域(浏览器窗口)的高度,包含滚动条的。
window.innerWidth: 表示窗口内容区域(浏览器窗口)的宽度,包含滚动条的。
- var windowHeight = window.innerHeight
- console.log(windowHeight)
-
- var windowWidth = window.innerWidth
- console.log(windowWidth)
- window.onload = function () {
- console.log('页面已经加载完毕')
- }
- window.onscroll = function () {
- console.log('浏览器滚动了')
- }
- window.onscroll = function () {
- console.log(document.body.scrollTop)
- console.log(document.documentElement.scrollTop)
- }
- window.onscroll = function () {
- console.log(document.body.scrollLeft)
- console.log(document.documentElement.scrollLeft)
- }
alert() : 在浏览器中弹出一个警告框。
confirm() : 在浏览器中弹出一个选择框。
prompt() : 在浏览器中弹出一个输入框。
页面跳转
刷新页面
window.scrollTo(x,y)
window.scrollTo(options)
- for(let i = 0,len = window.localStorage.length;i <len ;i ++){
- let key = window.localStorage.key(i); //获取所有的key
- let value = window.localStorage.getItem(key); //获取所有的value
- }
- //byClassName的兼容
- function byClassName(obj,className){
- if(obj.getElementsByClassName){
- //支持
- return obj.getElementsByClassName(className);
- }else{
- //不支持
- //1. 获取所有节点对象
- var eles = obj.getElementsByTagName('*');
- //创建一个空数组
- var arr = [];
- //遍历所有对象
- for(var i = 0,len = eles.length;i < len;i ++){
- //检测
- if(eles[i].className === className){
- arr.push(eles[i]);
- }
- }
- return arr;
- }
- }
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- body{
- height: 5000px;
- }
- #box{
- width: 50px;
- height: 50px;
- background: red;
- color: yellow;
- position: fixed;
- right: 50px;
- bottom: 50px;
- display: none;
- }
- </style>
- </head>
- <body>
- <div id="box">顶部</div>
- <script>
- //获取页面元素
- var o_box = document.querySelector('#box');
- //1. 求滚动条到顶端的距离
- //2. 触发滚动事件 onscroll
- //添加事件
- window.onscroll = function(){
- // console.log('呵呵');
- // 求滚动条到顶端的距离
- // 除了低版本谷歌都支持
- // var scrollTop = document.documentElement.scrollTop;
- //低版本谷歌
- // var scrollTop = document.body.scrollTop;
- //兼容
- var scrollTop = Math.floor(document.documentElement.scrollTop || document.body.scrollTop);
- console.log(scrollTop);
- if(scrollTop >= 3000){
- o_box.style.display = 'block';
- }else{
- o_box.style.display = 'none';
- }
- }
- //给box添加一个点击事件
- o_box.onclick = function(){
- document.documentElement.scrollTop = document.body.scrollTop = 0;
- }
- </script>
- </body>
- </html>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- body{
- height: 5000px;
- }
- #box{
- width: 100%;
- height: 100px;
- background: red;
- position: absolute;
- top: 400px;
- }
- </style>
- </head>
- <body>
- <div id="box"></div>
- <script>
- //获取box元素
- var o_box = document.querySelector('#box');
- //添加事件
- window.onscroll = function(){
- //滚动条到顶端的距离
- var scrollTop = Math.floor(document.documentElement.scrollTop || document.body.srcollTop);
- //判断距离
- if(scrollTop >= 400){
- o_box.style.position = 'fixed';
- o_box.style.left = 0;
- o_box.style.top = 0;
- }else{
- o_box.style.position = 'absolute';
- o_box.style.top = 400 + 'px';
- }
- }
- </script>
- </body>
- </html>