一、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的更多相关文章

  1. javascript每日一练(一)——javascript基础

    一.javascript的组成 ECMAScript DOM BOM 二.变量类型 常见类型有:number, string, boolean, undefined, object, function ...

  2. javascript每日一练—运动

    1.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...

  3. javascript每日一练(十四)——弹性运动

    一.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...

  4. javascript每日一练(十三)——运动实例

    一.图片放大缩小 <!doctype html> <html> <head> <meta charset="utf-8"> < ...

  5. javascript每日一练(十二)——运动框架

    运动框架 可以实现多物体任意值运动 例子: <!doctype html> <html> <head> <meta charset="utf-8&q ...

  6. javascript每日一练(十一)——多物体运动

    一.多物体运动 需要注意:每个运动物体的定时器作为物体的属性独立出来互不影响,属性与运动对象绑定,不能公用: 例子1: <!doctype html> <html> <h ...

  7. javascript每日一练(十)——运动二:缓冲运动

    一.缓冲运动 实现原理:(目标距离-当前距离) / 基数 = 速度(运动距离越大速度越小,运动距离和速度成反比) (500 - oDiv.offsetLeft) / 7 = iSpeed; 需要注意: ...

  8. javascript每日一练(九)——运动一:匀速运动

    一.js的运动 匀速运动 清除定时器 开启定时器 运动是否完成:a.运动完成,清除定时器:b.运动未完成继续 匀速运动停止条件:距离足够近  Math.abs(当然距离-目标距离) < 最小运动 ...

  9. javascript每日一练(八)——事件三:默认行为

    一.阻止默认行为 return false; 自定义右键菜单 <!doctype html> <html> <head> <meta charset=&quo ...

随机推荐

  1. django template出错

    解决方法一: 先导入settings >>> from django.conf import settings >>> settings.configure() & ...

  2. myeclipse部署时An internal error occurred 错误的几种情况

    myecplise上将工程部署到应用下时,经常出现 An internal error occurred during: "Add Deployment". java.lang.N ...

  3. viminfo: 无效的启动字符

    当自己进入一个用户,使用vi打开一个文件时,出现以下情况: [gexd@localhost ~]$ vi test.c E575: viminfo: 无效的启动字符 位于行: int main() . ...

  4. (step4.2.1) hdu 1372(Knight Moves——BFS)

    解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...

  5. LA-3135 - Argus(优先队列)

    3135 - Argus A data stream is a real-time, continuous, ordered sequence of items. Some examples incl ...

  6. 获取系统的emoji表情定制键盘

    首先 ,想要获取系统的表情,要首先知道表情对应的UTF8 的编码方式,我将其中一部分的图片展示出来 ,然后用UIButton 排列,iOS 7后又增加了300多个表情符号,这些都可以百度查到,现在上代 ...

  7. 关于C++ 的eof

    #include<iostream> #include<fstream> #define MAXSIZE 1000 using namespace std; int main( ...

  8. java:转换时间格式为String

    SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Date curDate = new ...

  9. Codeforces 479D - Long Jumps

    479D - Long Jumps, 480B - Long Jumps It , or . If we can already measure both x and y, output . Then ...

  10. Ajax辅助方法

    目前为止,我们已经考察了如何编写客户端JavaScript代码,以发送并接受服务器的数据.然而,在使用ASP.NET MVC时,还有另一种方法可用来执行Ajax调用,这就是Ajax辅助方法. Ajax ...