2020-7-13BOM对象
- window对象常用属性
| 描述 | |
|---|---|
| history | 包含了用户已浏览的 URL 的信息 |
| location | 包含关于当前 URL 的信息 |
| document | 代表给定浏览器窗口中的 HTML 文档 |
| event | 代表事件状态,如事件发生的元素,键盘状态,鼠标位置和鼠标按钮状态 |
示例1:location和history
分别定义4个页面,代码基本相同
<h1>4</h1> <select id="loc" onchange="forward()"> <option value="history1.html">history1</option> <option value="history2.html">history2</option> <option value="history3.html">history3</option> <option value="history4.html" selected="true">history4</option> </select> a <a href="javascript:history.go(1)">前进</a> a <a href="javascript:history.go(-1)">后退</a><!--使用这种方式调用js函数--> <hr > <img src="img/girls/zly4.png" > <script type="text/javascript" src="js/myjs.js"></script>
function forward(){
let loc=document.getElementById("loc");
window.location=loc.value; //使用location来重新设定访问地址信息
}
2.window常用函数
| 名称 | 描述 |
|---|---|
| alert | 显示自定义消息的对话框 |
| confirm | 显示确认对话框 |
| prompt | 显示输入对话框 |
| setTimeout | 经过指定毫秒值后计算表达式,并返回该事件对象 |
| setInterval | 每过一定时间间隔调用一次指定函数 |
| close |
let bool=confirm("你比豹子跑得快吗");
if(bool){
alert("你比禽兽还禽兽");
}else{
alert("你禽兽不如");
}
let i=0;
let duty;
function move(){
i++;
let img=document.getElementById("img");
img.src="imgs/eat/eat_"+i+".jpg";
if(i>39){
i=0;
}
duty=setTimeout("move()",100);
}
function stop(){
clearTimeout(duty);
}
<input type="button" value="动画" onclick="move()">
<input type="button" value="停止" onclick="stop()">
<img src="imgs/eat/eat_0.jpg" id="img">
let time=7200,h,m,s;
function showTime(){
let h1=document.getElementById("h1");
h=parseInt(time/60/60);
m=parseInt(time%3600/60);
s=time%60;
h1.innerHTML=h+":"+m+":"+s;
time--;
}
setInterval("showTime()",10);
鼠标坐标:e.clientX , e.clientY
图片位置:img.offsetLeft , img.offsetTop
方法一:在body中插入图片
<img src="../imgs/1.png" style="width:100px; position:absolute;left:0px;top:0px;" id="it"/>
js代码:
let x=0,y=0;
function move(e){
e = e.event || window.event;
let x1 = e.clientX;
let y1 = e.clientY;
let img = document.getElementById("it");
let a = setInterval(function setInt(){
if(x==x1 && y==y1){
clearTimeout(a);
}else if(x<x1){
x++;
}else if (x>x1){
x--;
}
if(y<y1){
y++;
}else if (y>y1){
y--;
}
img.style.left = x + "px";
img.style.top = y + "px";
}
,0.1);
}
document.onclick = move;
方法二:在css中使用transition属性;
img{
transition: 2s;
}
js代码:
let x=0,y=0;
function move(e){
e = e.event || window.event;
x = e.clientX;
y = e.clientY;
let img = document.getElementById("it");
img.style.left = x + "px";
img.style.top = y + "px";
}
document.onclick = move;

浙公网安备 33010602011771号