1.BOM概述:
BOM(Browser Object Model):即浏览器对象模型
bom操作:windows location history
js里面的内置对象:Array String Math Date
2.window
window是浏览器环境中的一个全局的顶级对象,所有在浏览器环境中使用的对象都是window对象的子对象。它有非常多的属性和方法。全局变量是 window 对象的属性,全局函数是 window 对象的方法。
(1)window窗口的三种属性:
First:
console.log(window.screen.width); //屏幕的总宽度
console.log(window.screen.availWidth); //实际宽度
console.log(window.screen.height); //屏幕的总高度
console.log(window.screen.availHeight)
//实际高度(即不包括导航栏等上部分
Second:获取可视区域的宽和高(两种方式)
console.log(document.documentElement.clientWidth);
console.log(document.documentElement.clientHeight);
console.log(window.innerWidth);
console.log(window.innerHeight);
Third:获取到的也是可视区域的宽和高,但是包含浏览器的8像素
window.function (){
console.log(document.body.clientWidth);
console.log(document.body.clientHeight);
}
(2)window中移动端加载一个js文件,怎么判断设备加载不同的js脚本
var app=navigator.appVersion;
var she=["iPhone","Android","iPad"];
for(var index in she){
if(app.indexOf(she[index])!=-1)
{
var sc=document.createElement("script");
sc.src="1.yidong.js";
document.body.appendChild(sc);
}
}
(3)window对象中的两个计时器
setTimeout(); 延迟一段时间执行,只执行一次
setInterval(); 延迟一段时间执行 ,循环执行
var time;
var count=0;
showtime();
function showtime(){
count++;
console.log(count);
time=setTimeout("showtime()",1000);
} //函数的递归,可让一次性定时器多次执行
clearTimeout(time); 可清除定时器setTimeout();
clearInterval(time);可清除定时器 setInterval();
3.Math对象
(1)Math对象属性
属性 | 描述 |
PI | 返回圆周率(约为3.1415) |
E | 返回自然对数的底数(约为2.718) |
LN2(10) | 返回2(10)的自然对数 |
LOG2(10) | 返回以2(10)为底e的对数 |
方法 | 描述 |
abs(x) | 求绝对值 |
ceil() | 向上取整 |
floor() | 向下取整 |
round() | 四舍五入取整 |
min() | 取最小值的的方法 |
max() | 取最大值的的方法 |
random() | 产生0~1之间的随机数 |
pow() | 求幂 |
sin() | 求正弦 |
tan() | 求正切 |
4.Date对象
Date是日期和时间对象,可以获取时间,也可以设置时间
var time = new Date();
注:Date对象会自动把当前的时间和日期保存为其初始值。
Date对象的方法:
方法 | 描述 |
getFullYear() | 从 Date 对象以四位数字返回年份。 |
getYear() | 从1900到当前年份的间隔 |
getMonth() | 从 Date 对象返回月份 (0 ~ 11)。 |
getDay() | 从 Date 对象返回一个月中的某一天 (1 ~ 31)。 |
getHours() | 返回 Date 对象的小时 (0 ~ 23)。 |
getMinutes() | 返回 Date 对象的分钟 (0 ~ 59)。 |
getSeconds() | 返回 Date 对象的秒数 (0 ~ 59)。 |
getMilliseconds() | 返回 Date 对象的毫秒(0 ~ 999)。 |
getTime() | 返回 1970 年 1 月 1 日至今的毫秒数。 |
toDateString() | 把 Date 对象的日期部分转换为字符串。 |
toLocaleDateString() | 根据本地时间格式,把 Date 对象的日期部分转换为字符串。 |
toLocaleString() | 根据本地时间格式,把 Date 对象转换为字符串。 |
toLocaleTimeString() | 根据本地时间格式,把 Date 对象的时间部分转换为字符串。 |
toTimeString() | 把 Date 对象的时间部分转换为字符串。 |
setTime() | 以毫秒设置 Date 对象。 |
var b=document.getElementsByClassName("b");
var time=setInterval(function(){
var timego=new Date("2019 5-01 00:00:00");
var nowtime=new Date();
var timeres=timego.getTime()-nowtime.getTime();
var ms=timeres%1000;
var res="";
if(ms<10){
res="00"+ms;
}
else if(ms>=10&&ms<100){
res="0"+ms;
}
else{
res=ms;
}
var sec=parseInt(timeres/1000); //总秒数
var secth=sec%60; //剩余秒
var minth=parseInt(sec/60)%60; //剩余分钟数
var houth=parseInt(sec/(60*60))%24; // 剩余小时数
var dayth=parseInt(sec/(60*60*24))%31; //剩余天数
var month=parseInt(sec/(60*60*24*31))%12; //剩余月
var yearth=parseInt(sec/(60*60*24*31*12))%100;
console.log(yearth);
b[0].innerHTML=yearth+"年";
b[1].innerHTML=month+"月";
b[2].innerHTML=dayth+"天";
b[3].innerHTML=houth+"时";
b[4].innerHTML=minth+"分";
b[5].innerHTML=secth+"秒";
b[6].innerHTML=res+"毫秒";
},1)