javascript每日一练(五)——BOM
一、BOM打开,关闭窗口
window.open(); window.close();
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
window.onload = function()
{
var oBtn = document.getElementById('btn1');
var oBtn2 = document.getElementById('btn2');
oBtn.onclick = function(){
var oNewWin = window.open('about:blank');
oNewWin.document.write(123);
};
oBtn2.onclick = function(){
window.close();
};
};
</script>
</head>
<body style="height:2000px;">
<button id="btn1">open</button><button id="btn2">close</button>
</body>
</html>
二、BOM常用属性
window.navigator.userAgent; window.loaction;
三、BOM侧边栏随窗口滚动(广告浮窗)
可视区高/宽度: document.documentElement.clientHeight/clientWidth;
滚动条滚动距离: document.documentElement.scrollTop || document.body.scrollTop;
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1{ width:100px; height:100px; background:red; position:absolute; right:0; top:0;}
</style>
<script>
window.onload = window.onresize = window.onscroll = function(){
var oDiv = document.getElementById('div1');
var iScrollTop = document.body.scrollTop || document.documentElement.scrollTop; //获取滚动条距离顶部高度
var iH = document.documentElement.clientHeight; //获取可视区高度
oDiv.style.top = iScrollTop + (iH - oDiv.offsetHeight) / 2 + 'px';
};
</script>
</head>
<body style="height:2000px;">
<div id="div1"></div>
</body>
</html>
四、BOM回到顶部
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
button{ position:fixed; right:0; bottom:0;}
</style>
<script>
window.onload = function(){
var oBtn = document.getElementById('btn1');
var timer = null;
var bSys = true;
window.onscroll = function(){
if(!bSys){
clearInterval(timer);
}
bSys = false;
};
oBtn.onclick = function(){
timer = setInterval(function(){
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
var iSpeed = Math.floor((-scrollTop / 8));
if(scrollTop == 0){
clearInterval(timer);
}
bSys = true;
document.documentElement.scrollTop = document.body.scrollTop = scrollTop + iSpeed;
}, 30);
};
};
</script>
</head>
<body style="height:2000px;">
<button id="btn1">回到顶部</button>
</body>
</html>
javascript每日一练(五)——BOM的更多相关文章
- javascript每日一练(一)——javascript基础
一.javascript的组成 ECMAScript DOM BOM 二.变量类型 常见类型有:number, string, boolean, undefined, object, function ...
- javascript每日一练—运动
1.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...
- javascript每日一练(十四)——弹性运动
一.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...
- javascript每日一练(十三)——运动实例
一.图片放大缩小 <!doctype html> <html> <head> <meta charset="utf-8"> < ...
- javascript每日一练(十二)——运动框架
运动框架 可以实现多物体任意值运动 例子: <!doctype html> <html> <head> <meta charset="utf-8&q ...
- javascript每日一练(十一)——多物体运动
一.多物体运动 需要注意:每个运动物体的定时器作为物体的属性独立出来互不影响,属性与运动对象绑定,不能公用: 例子1: <!doctype html> <html> <h ...
- javascript每日一练(十)——运动二:缓冲运动
一.缓冲运动 实现原理:(目标距离-当前距离) / 基数 = 速度(运动距离越大速度越小,运动距离和速度成反比) (500 - oDiv.offsetLeft) / 7 = iSpeed; 需要注意: ...
- javascript每日一练(九)——运动一:匀速运动
一.js的运动 匀速运动 清除定时器 开启定时器 运动是否完成:a.运动完成,清除定时器:b.运动未完成继续 匀速运动停止条件:距离足够近 Math.abs(当然距离-目标距离) < 最小运动 ...
- javascript每日一练(八)——事件三:默认行为
一.阻止默认行为 return false; 自定义右键菜单 <!doctype html> <html> <head> <meta charset=&quo ...
随机推荐
- 我的ubuntu
题外话:不知不觉也已经大三,最近思考了很多.在腾讯网看到了对李嘉诚的一篇专访,感触颇深. 想起来我从第一次接触ubuntu到现在也有一年了,记得第一个版本还是12.04,不过很快就换成了12.10,在 ...
- javascript 检测密码强度 美化版
模仿美团的美化 <!DOCTYPE> <head runat="server"> <title></title> <link ...
- QR Code 码
一.QR Code码 由日本Denso公司于1994年9月研制的一种矩阵二维码符号,它除具有一维条码及其它二维条码所有的信息容量大.可靠性高.可表示汉字及图象多种文字信息.保密防伪性强等优点外,还具有 ...
- java学习之网络编程之echo程序
服务端的实现 package com.gh.echo; import java.io.*; import java.net.*; /** * echo服务器程序 * 实现 不断接收字符串 ,然后返回一 ...
- android错误之android.content.res.Resources$NotFoundException:
错误:android.content.res.Resources$NotFoundException: String resource ID #0x1 原因:一般发生在参数 int resId 错误, ...
- swift优秀学习博客
http://www.00red.com/ http://www.cnblogs.com/kenshincui/ 优秀的某博客,包含大量iOS的全面的总结 https://github.com/Co ...
- iOS签发者无效
IOS开发证书全部变成无效,如下图 打包提示错误 解决方法: 1. 下载https://developer.apple.com/certificationauthority/AppleWWDRCA.c ...
- c语言 ,回调函数[个人理解]
回调函数:把需要调用的方法的指针pCallBackFuncX作为参数传递给一个函数UsrFunction,以便该UsrFunction函数在处理相似事件的时候可以灵活的使用不同的方法. 以在fla ...
- (Problem 7)10001st prime
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. ...
- ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解
原文 ASP.NET MVC 5 学习教程:Details 和 Delete 方法详解 在教程的这一部分,我们将研究一下自动生成的 Details 和Delete 方法. Details 方法 打开M ...