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>
相關文章
相關標籤/搜索