js效果 整理

整理中。。。javascript

 
一、js获取页面及元素高度、宽度

其余参考文献:http://www.open-open.com/lib/view/open1420120422531.htmlcss

js:html

网页可见区域宽: document.body.clientWidth;(不含滚动条)
网页可见区域高: document.body.clientHeight;(不含滚动条)
网页可见区域宽: document.body.offsetWidth;(包括边线的宽);
网页可见区域高: document.body.offsetHeight;(包括边线的宽);
网页正文全文宽: document.body.scrollWidth;
网页正文全文高: document.body.scrollHeight;
网页被卷去的高(ff):document.body.scrollTop;
网页被卷去的高(ie): document.documentElement.scrollTop;
网页被卷去的左: document.body.scrollLeft;
网页正文部分上: window.screenTop;
网页正文部分左: window.screenLeft;
某个元素的宽度: obj.offsetWidth;
某个元素的高度: obj.offsetHeight;
某个元素的上边界到body最顶部的距离: obj.offsetTop;(在元素的包含元素不含滚动条的状况下)
某个元素的左边界到body最左边的距离: obj.offsetLeft;(在元素的包含元素不含滚动条的状况下)
返回当前元素的上边界到它的包含元素的上边界的偏移量: obj.offsetTop;(在元素的包含元素含滚动条的状况下)
返回当前元素的左边界到它的包含元素的左边界的偏移量: obj.offsetLeft;(在元素的包含元素含滚动条的状况下)java

屏幕分辨率的高: window.screen.height
屏幕分辨率的宽: window.screen.width
屏幕可用工做区高度: window.screen.availHeight
屏幕可用工做区宽度: window.screen.availWidthjquery

HTML精肯定位:scrollLeft,scrollWidth,clientWidth,offsetWidth
scrollHeight: 获取对象的滚动高度。
scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离
scrollWidth:获取对象的滚动宽度
offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度
offsetLeft:获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置
offsetTop:获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置
event.clientX 相对文档的水平座标
event.clientY 相对文档的垂直座标
event.offsetX 相对容器的水平坐标
event.offsetY 相对容器的垂直坐标
document.documentElement.scrollTop 垂直方向滚动的值
event.clientX+document.documentElement.scrollTop 相对文档的水平座标+垂直方向滚动的量css3

jquery:web

获取浏览器显示区域(可视区域)的高度 : $(window).height();
获取浏览器显示区域(可视区域)的宽度 : $(window).width();
获取页面的文档高度:$(document).height();
获取页面的文档宽度 :$(document).width();
浏览器当前窗口文档body的高度: $(document.body).height();
浏览器当前窗口文档body的宽度: $(document.body).width();
获取滚动条到顶部的垂直高度 (即网页被卷上去的高度) :$(document).scrollTop();
获取滚动条到左边的垂直宽度 :$(document).scrollLeft();
获取或设置元素的宽度:$(obj).width();
获取或设置元素的高度:$(obj).height();segmentfault

获取或设置元素的宽度:$(obj).innerWidth(); (height + padding)
获取或设置元素的高度:$(obj).innerHeight(); (height + padding)浏览器

获取或设置元素的宽度:$(obj).outerWidth(); (height + padding + border)
获取或设置元素的高度:$(obj).outerHeight(); (height + padding + border)app

获取或设置元素的宽度:$(obj).outerWidth(true); (height + padding + border + margin)
获取或设置元素的高度:$(obj).outerHeight(true); (height + padding + border + margin)
某个元素的上边界到body最顶部的距离:obj.offset().top;(在元素的包含元素不含滚动条的状况下)
某个元素的左边界到body最左边的距离:obj.offset().left;(在元素的包含元素不含滚动条的状况下)
返回当前元素的上边界到它的包含元素的上边界的偏移量:obj.offset().top(在元素的包含元素含滚动条的状况下)
返回当前元素的左边界到它的包含元素的左边界的偏移量:obj.offset().left(在元素的包含元素含滚动条的状况下)

 
二、选中改变样式,其余默认样式
 
<dl class="use_class">
<dd>我的</dd>
<dd>机构</dd>
</dl>

<script type="text/javascript" src="../js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
// 用户类别选择,我的、机构
$(document).ready(function(){
$("dd").click(function(){
$(this).css("background","#46b0d5").siblings().css("background","#ccc");
});
</script>
三、JS判断浏览器是否支持某一个CSS3属性的方法

css3表现冲击最大的就是动画了,所以颇有必要去事先判断浏览器是否支持,写CSS3动画库就只有部分浏览器支持

function supportCss3(style) {
            var prefix = ['webkit', 'Moz', 'ms', 'o'],
                    i,
                    humpString = [],
                    htmlStyle = document.documentElement.style,
                    _toHumb = function (string) {
                        return string.replace(/-(\w)/g, function ($0, $1) {
                            return $1.toUpperCase();
                        });
                    };

            for (i in prefix)
                humpString.push(_toHumb(prefix[i] + '-' + style));

            humpString.push(_toHumb(style));

            for (i in humpString)
                if (humpString[i] in htmlStyle) return true;

            return false;
        }
alert(supportCss3('animation-play-state'));//使用方法
四、JS实现-DIV自动居中代码
window.onload = function(){
            function box(){
                //获取DIV为‘box’的盒子
                var oBox = document.getElementById('box');
                //获取元素自身的宽度
                var L1 = oBox.offsetWidth;
                //获取元素自身的高度
                var H1 = oBox.offsetHeight;
                //获取实际页面的left值。(页面宽度减去元素自身宽度/2)
                var Left = (document.documentElement.clientWidth-L1)/2;
                //获取实际页面的top值。(页面宽度减去元素自身高度/2)
                var top = (document.documentElement.clientHeight-H1)/2;
                oBox.style.left = Left+'px';
                oBox.style.top = top+'px';
            }
            box();
            //当浏览器页面发生改变时,DIV随着页面的改变居中。
            window.onresize = function(){
                box();
            }
        }
五、JS自动刷新页面

Javascript刷新页面的几种方法:

1 history.go(0) 
2 location.reload() 
3 location=location 
4 location.assign(location) 
5 document.execCommand('Refresh') 
6 window.navigate(location) 
7 location.replace(location) 
8 document.URL=location.href

例如:

function myrefresh()
                {
                    window.location.reload();
                }
        setTimeout('myrefresh()',1000); //指定1秒刷新一次

 

六、点击空白关闭弹窗的js写法推荐?
$(document).mouseup(function(e){
  var _con = $(' 目标区域 ');   // 设置目标区域
  if(!_con.is(e.target) && _con.has(e.target).length === 0){ // Mark 1
    some code...   // 功能代码
  }
});
/* Mark 1 的原理:
判断点击事件发生在区域外的条件是:
1. 点击事件的对象不是目标区域自己
2. 事件对象同时也不是目标区域的子元素
*/
七、js写一个弹出窗
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js实现一个弹出框</title>
<style type="text/css">
/*预先写好弹出窗的样式*/
#menu{height: 900px;}
#close{ 
    width:30px; 
    height:30px; 
    cursor:pointer; 
    position:absolute; 
    right:5px; 
    top:5px; 
    text-indent:-999em;
    background-color:blue;
    }
#mask{ 
    background-color:pink;
    opacity:0.5;
    filter: alpha(opacity=50); 
    position:absolute; 
    left:0;
    top:0;
    z-index:1;
    }
#login{ 
    position:fixed;
    z-index:2;
    }
.loginCon{ 
    position:relative; 
    width:670px;
    height:380px;
    /*background:url(img/loginBg.png) #2A2C2E center center no-repeat;*/
    background-color: #ccc;
    }
</style>
<script>
function openNew(){
    //获取页面的高度和宽度
    var sWidth=document.body.scrollWidth;
    var sHeight=document.body.scrollHeight;
    
    //获取页面的可视区域高度和宽度
    var wHeight=document.documentElement.clientHeight;
    
    var oMask=document.createElement("div");
        oMask.id="mask";
        oMask.style.height=sHeight+"px";
        oMask.style.width=sWidth+"px";
        document.body.appendChild(oMask);
    var oLogin=document.createElement("div");
        oLogin.id="login";
        oLogin.innerHTML="<div class='loginCon'><div id='close'>关闭</div></div>";
        document.body.appendChild(oLogin);
    
    //获取登录框的宽和高
    var dHeight=oLogin.offsetHeight;
    var dWidth=oLogin.offsetWidth;
        //设置登录框的left和top
        oLogin.style.left=sWidth/2-dWidth/2+"px";
        oLogin.style.top=wHeight/2-dHeight/2+"px";
    //点击关闭按钮
    var oClose=document.getElementById("close");
    
        //点击登录框之外的区域也能够关闭登录框
        oClose.onclick=oMask.onclick=function(){
                    document.body.removeChild(oLogin);
                    document.body.removeChild(oMask);
                    };
                    };
                    
    window.onload=function(){
            var oBtn=document.getElementById("btnLogin");
                //点击登陆按钮
                oBtn.onclick=function(){
                    openNew();
                    return false;
                    }
                
        }
</script>

</head>
<body>
<div id="menu">
    <div id="login-area">
      <button id="btnLogin">登陆</button>
    </div>
</div>
</body>
</html>
相关文章
相关标签/搜索