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 ...
随机推荐
- LNMP下基于端口的虚拟主机配置
1.在/usr/local/nginx/conf/nginx.conf文件的的最后一个"}"前加上 include vhost/*.conf; 2.在/usr/local/ngin ...
- Flask对请求的处理
由http://www.cnblogs.com/steinliber/p/5133386.html 中可得服务器会把environ和start_response发送给Flask的实例app,返回的是a ...
- [LeetCode]题解(python):019-Remove Nth Node From End of List
题目来源: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 题意分析: 这道题是给定一个链表,删除倒数第n个节点.提醒, ...
- Judge loop in directed graph
1 深度优先方法 首先需要更改矩阵初始化函数init_graph() 然后我们需要初始化vist标记数组 深度优先访问图,然后根据是否存在back edge判断是否存在环路 算法如下: #includ ...
- word2vec代码解释
以前看的国外的一篇文章,用代码解释word2vec训练过程,觉得写的不错,转过来了 原文链接 http://nbviewer.jupyter.org/github/dolaameng/tutorial ...
- IM-即时通讯技术概述
IM-即时通讯技术概述 简述 即时通讯技术(IM)支持用户在线实时交谈.如果要发送一条信息,用户需要打开一个小窗口,以便让用户及其朋友在其中输入信息并让交谈双方都看到交谈的内容.大多数常用的即时通讯发 ...
- C 语言中的变量为什么不能以数字打头
C 语言中的变量为什么不能以数字打头? C 语言中的变量为什么不能以数字打头? 不要告诉我编译原理书上有.我暂时看不懂. 除了下面的解释外, “假如变量名允许以数字开头的话,那么语法分析器在解析一个全 ...
- 【Web】java date 到 Oracle date 精确到时分秒
有两种方法: java.util.Date startTime=new Date("2014/01/01 23:00:00"); 1.new Timestamp(startTime ...
- ios常见加密解密方法
在其他平台中经常会计算MD5值,在iOS平台中也提供了该方法,首先需要导入头文件 #import <CommonCrypto/CommonDigest.h> 方法CC_MD5可以获取MD5 ...
- [转]组合数取模 Lucas定理
对于C(n, m) mod p.这里的n,m,p(p为素数)都很大的情况.就不能再用C(n, m) = C(n - 1,m) + C(n - 1, m - 1)的公式递推了. 这里用到Lusac定理 ...