JavaScript——BOM(笔记)
文章目录
-
- BOM
-
- wiodow对象事件
- 窗口加载事件
- 定时器
-
- setTimeout()
- setInterval()
这个系统采用独特的开发策略。
JavaScript执行流程采用独特设计。
在该系统中使用位置对象进行操作。
借助navigator对象完成各项功能。
系统支持history记录功能。
提供offset系列辅助功能。
客户端系列功能一应俱全。
滚动系列操作流畅无阻。
动画效果令人惊艳。
系统具备高效的流量控制能力。
支持多种触控事件处理方式。
配备强大的本地存储功能。
BOM
wiodow对象事件
window.onload = function(){ // onload等页面加载完毕执行
alert('窗口加载事件'); // 阔以放到页面任何位置
}
window.addEventListener("load",function(){
alert('窗口加载事件'); // 与onload相同
})
document.addEventListener("DOMContentLoaded",function(){
alert('窗口加载事件'); // 主要的DOM 标签 加载完毕后执行 不包括图片,
})
窗口加载事件
window.onresize = function(){ // onresize 浏览器窗口发生改变时触发
console.log(window.innerWidth); // 当前浏览器屏幕的宽度
}
window.addEventListener('resize',function(){
console.log(window.innerWidth); // 当前浏览器屏幕的宽度
})
定时器
setTimeout()
window.setTimeout(调用函数, [延迟的毫秒数]); 触发一次 window 阔以省略
window.setTimeout(function(){
alert('调用1次'); // 处理函数
},2000) // 延迟时间
停止定时器
window.clearTimeout(timeoutID)(timeoutID标识符名)
var btn = document.querySelector("button");
var timer = setTimeout(function(){ // timer 是代表着这个定时器
alert('爆炸了');
},3000) // 延迟时间
btn.addEventListener("click",function(){
clearTimeout(timer); // 清除timer这个定时器
})
setInterval()
使用[时间间隔(以毫秒计)]作为参数来设置回调函数的时间轴。其首次运行是在指定的时间点后进行。随后每隔[时间间隔(以毫秒计)]就会再次被调用。
window.setInterval(function(){
alert('调用多次')
},2000) // 延迟时间
停止定时器
window.clearInterval(intervalID)(timeoutID标识符名)
var btn = document.querySelector("button");
var timer = window.setInterval(function(){ // timer 代表这个定时器
alert('调用多次')
},2000) // 延迟时间
btn.addEventListener("click",function(){
clearInterval(timer); // 清除timer这个定时器
})
this
- 在全局作用域或普通函数中,this指针指向全局对象window,而在此处,需要注意的是,当在定时器内部调用此方法时,this指针仍然指向window。
- 在方法调用过程中, this指针始终由被调用者所持有,因此,谁调用了这个方法决定了this指针的来源。
- 在构造函数内部,this指针总是初始化为所创建实例的引用。
JS执行机制
同步:依次进行烧水、切菜、炒菜
异步:当同步完成后立即启动异步流程,并且在完成烧水后立即开始切菜和炒菜
依次运行 中的同步操作。
将(作为回调函数的)异步作业加入作业队列中。
当 的所有同步操作完成后,在线程间调度机制作用下系统将依次从队列中获取相应的异步作业。这些被读取到的作业在完成等待状态后会立即进入 并立即开始执行。
console.log(1);
document.onclick = function(){
console.log('click');
}
console.log(2);
setTimeout(function(){
console.log(3);
},3000)
// 先执行同步 1 , 2 在将点击事件和定时器提交给异步任务 当点击了就输出 click 3秒后输出 3
// 1 2 click 3 或 1 2 3 click
location对象
网络协议
HTTP/1.1协议
| 方法 | 说明 |
|---|---|
| location.href | 获取或设置整个URL |
| location.host | 返回主机(域名) |
| location.port | 返回端口号无为空串 |
| location.pathname | 返回路径 |
| location.search | 返回参数 |
| location.hash | 返回片段 |
<form action="JavaScript.html"> <!-- 在另一页面实现表单跳转功能 -->
<input type="text" name="username" />
<input type='submit'/>
</form>
var div = document.querySelector("div");
var str = location.search.substr(1); // 获取另一页面的参数 ?username=luokong 截取字符串
var arr = str.split('='); // 通过 = 将字符串分隔成数组
console.log(str); // username=luokong
console.log(arr); // [username,luokong]
div.innerHTML = arr[1] + '欢迎你的登录' // 修改div的内容
| 方法 | 说明 |
|---|---|
| location.assign() | 和href一样,跳转页面 |
| location.replace() | 替换当前页面,不记录历史,不能后退页面 |
| location.reload() | 重新加载,相当于f5,参数为true强制刷新 CTRL+f5 刷新服务器的缓存 |
var btn = document.querySelector('button');
btn.addEventListener("click",function(){
// location.assign('http://www.baidu.com'); // 实现跳转功能
// location.replace('http://www.baidu.com'); // 实现跳转不记录历史
location.reload(); // 刷新页面
})
navigator对象
navigator.userAgent:判断用户那个终端打开页面,实现跳转
if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
window.location.href = ""; //手机
} else {
window.location.href = ""; //电脑
}
history 对象
| 方法 | 说明 |
|---|---|
| history.back() | 后退功能 |
| history.forward() | 前进功能 |
| go(参数) | 前进后退功能 参数如果是1前进,-1后退 |
offset系列
- 计算指定元素与具有定位属性的父容器的距离坐标值
- 查询当前元素的宽度信息,并考虑包含边框区域的影响
- 仅根据具有定位属性的父亲容器进行计算,默认不基于整个页面进行计算
| 属性 | 说明 |
|---|---|
| element.offsetParent | 返回带有定位的父级元素 |
| element.offsetTop | 上方的偏移量(元素距父元素的上边距) |
| element.offsetLeft | 左边框的偏移量(元素距父元素的左边距) |
| element.offsetWidth | 返回自身包括padding,边框,内容区域的宽度 |
| element.offsetHeight | 返回自身包括padding,边框,内容区域的高度 |
.father {
width: 400px;
height: 200px;
background-color: pink;
position: absolute; /* 父元素代定位 */
}
.son {
width: 200px;
height: 100px;
margin-top: 50px;
margin-left: 50px;
border: 5px solid red;
background-color: blue;
}
<div class="father">
<div class="son"></div>
</div>
var son = document.querySelector('.son');
console.log(son.offsetParent); // 父元素有定位返回父元素 没有返回body
console.log(son.offsetTop); // 元素距父元素的上边距 50
console.log(son.offsetLeft); // 50
console.log(son.offsetWidth); // 210 包括边框
console.log(son.offsetHeight); // 110
| offset | style |
|---|
| offset 可以得到任意样式表中的样式值
loffset 系列获得的数值是没有单位的
loffsetWidth 包含padding+border+width
loffsetWidth 等属性是只读属性,只能获取不能赋值
所以,我们想要获取元素大小位置,用offset更合适| lstyle 只能得到行内样式表中的样式值
lstyle.width 获得的是带有单位的字符串
lstyle.width 获得不包含padding和border 的值
lstyle.width 是可读写属性,可以获取也可以赋值
所以,我们想要给元素更改值,则需要用style改变 |
client系列
| 属性 | 说明 |
|---|---|
| element.clientTop | 返回元素上边框的大小 |
| element.clientLeft | 返回元素左边框的大小 |
| element.clientWidth | 返回自身包括padding,内容区域的宽度,不包含边框 |
| element.clientHeight | 返回自身包括padding,内容区域的高度,不包含边框 |
.box {
width: 200px;
height: 100px;
background-color: pink;
border-left: 5px solid red;
border-top: 10px solid red;
}
<div class="box"></div>
<script type="text/javascript">
var box = document.querySelector('.box');
// client 与 offset 类似 区别在于不带边框
console.log(box.clientTop); // 获取上边框
console.log(box.clientLeft); // 获取左边框
console.log(box.clientWidth); // 获取宽度不带边框
console.log(box.clientHeight); // 获取高度不带边框
</script>
scroll系列
| 属性 | 说明 |
|---|---|
| element.scrollTop | 返回被卷上去的距离,不带单位 |
| element.scrollLeft | 返回被卷左侧的距离,不带单位 |
| element.scrollWidth | 返回自身的宽度不包含边框 |
| element.scrollHeight | 返回自身的高度不包含边框 |
<textarea class="box">
我是内容我是内容我是内容我是内容
我是内容我是内容我是内容我是内容
我是内容我是内容我是内容我是内容
我是内容我是内容我是内容我是内容
</textarea>
<script type="text/javascript">
var box = document.querySelector('.box');
console.log(box.scrollHeight); // 返回文字自身的宽度
console.log(box.scrollWidth); // 返回文字自身的高度
box.addEventListener('scroll',function(){ // scroll 滚动事件
console.log(box.scrollTop); // 被卷上去的距离
})
</script>
页面滚动距离:window.pageYoffset获得 或 window.pagXoffset
document.addEventListener('scroll',function(){
console.log(window.pageYOffset); // 页面距顶部的距离
})
立即执行函数
(function(){
alert("666666")
})() // 立即执行
(function(){
alert('888888')
}())
动画
1.获取位置
2.加1个移动距离
3.利用定时器不断重复
4.加个结束条件
5.需要定位
function animate(obj,times,main){//对象,距离,回调函数
clearInterval(obj.timer);//清除原先的定时器
obj.timer = setInterval(function(){
// (目标值 - 现在的位置) / 10 每次的步长
// 100 - 0 / 10 = 10
// 100 - 10 / 10 = 9
var step = (times - obj.offsetLeft) / 10;//缓动
step = step > 0 ? Math.ceil(step) : Math.floor(step);//取整
if(obj.offsetLeft == times){ // 当目标值 = 现在的位置 结束
clearInterval(obj.timer);
if(main){
main();//结束时调用回调函数
}
}
obj.style.left = obj.offsetLeft + step +'px';
//均速 //缓动递减
},30);
}
节流阀
// 开始为true执行前关闭false 结束后为true
var t = null;
$(function(){
$(document).on('mousemove',function(e){
//鼠标移动时改变位置
if(t){ // 有定时器就不执行
return
}
console.log('1s')
t = setTimeout(function(){
$("#img").css("left",e.pageX + 'px')
$("#img").css("top",e.pageY + 'px');
t = null;// 执行完开启节流
},50)
})
})
touch事件
移动端触屏事件
| 事件 | 说明 |
|---|---|
| touchstart | 手值触摸到时触发 |
| touchmove | 手指在滑动时触发 |
| touchend | 手指移开时触发 |
本地存储
sessionStorage:生命周期为关闭浏览器
| 方法 | 说明 |
|---|---|
| window.sessionStorage.setItem(‘key’,value) | 存储 |
| window.sessionStorage.getItem(‘key’) | 获取 |
| window.sessionStorage.removeItem(‘key’) | 删除 |
| window.sessionStorage.clear() | 清除 |
<input type="text">
<button class="set">存储数据</button>
<button class="get">获取数据</button>
<button class="remove">删除数据</button>
<button class="del">清空所有数据</button>
<script>
console.log(localStorage.getItem('username'));
var ipt = document.querySelector('input');
var set = document.querySelector('.set');
var get = document.querySelector('.get');
var remove = document.querySelector('.remove');
var del = document.querySelector('.del');
set.addEventListener('click', function() {
// 当我们点击了之后,就可以把表单里面的值存储起来
var val = ipt.value;
sessionStorage.setItem('uname', val);
sessionStorage.setItem('pwd', val);
});
get.addEventListener('click', function() {
// 当我们点击了之后,就可以把表单里面的值获取过来
console.log(sessionStorage.getItem('uname'));
});
remove.addEventListener('click', function() {
sessionStorage.removeItem('uname');
});
del.addEventListener('click', function() {
// 当我们点击了之后,清除所有的
sessionStorage.clear();
});
</script>
localStorage:生命周期永久存在
| 方法 | 说明 |
|---|---|
| window.localStorage.setItem(‘key’,value) | 存储 |
| window.localStorage.getItem(‘key’) | 获取 |
<input type="text">
<button class="set">存储数据</button>
<button class="get">获取数据</button>
<button class="remove">删除数据</button>
<button class="del">清空所有数据</button>
<script>
var ipt = document.querySelector('input');
var set = document.querySelector('.set');
var get = document.querySelector('.get');
var remove = document.querySelector('.remove');
var del = document.querySelector('.del');
set.addEventListener('click', function() {
var val = ipt.value;
localStorage.setItem('username', val);
})
get.addEventListener('click', function() {
console.log(localStorage.getItem('username'));
})
remove.addEventListener('click', function() {
localStorage.removeItem('username');
})
del.addEventListener('click', function() {
localStorage.clear();
})
</script>
