咱們來實現test中的onclick事件javascript
function justAtest()
{
var test= document.getElementById("test");
var test2=document.getElementById("test2")
var test3=document.getElementById("test3")
var test4=document.getElementById("test4");
alert(test4.style.height);
alert(test3.style.height);
alert(test2.style.height)
alert(test.style.height);
alert(document.body.style.height)
}css
height :其實Height高度跟其餘的高度有點不同,在javascript中它是屬於對象的style對象屬性中的一個成員,它的值是一個字符類型的,而另外三個高度的值是int類型的,它們是對象的屬性.所以這樣document.body.height就會提示undenifine,而必須寫成document.body.style.height
上面的腳本將依次彈出700px,550px,600px,500px,1000px.height是最簡單的了,沒必要去考慮是否有滾動條及邊框等.所以也不作多解釋了.
而後咱們將腳本換下html
function justAtest()
{
var test= document.getElementById("test");
var test2=document.getElementById("test2")
var test3=document.getElementById("test3")
var test4=document.getElementById("test4");
alert(test4.clientHeight);
alert(test3.clientHeight);
alert(test2.clientHeight)
alert(test.clientHeight);
alert(document.body.clientHeight)
}java
運行後火狐的結果爲:700,550,583,483,1000
IE的結果爲:700 ,550,583,483,1000
IE與火狐下的運行結果是一致的.下面來看下clientHeight的定義.
clientHeight:可見區域的寬度,不包括boder的寬度,若是區域內帶有滾動條,還應該減去橫向滾動條不可用的高度,正常的是17px,其實就是滾動條的可滾動的部分了,其實clientHeight與height的高度差很少,若是不帶滾動條的話他們的值都是同樣的,若是帶有滾動條的話就會比height值少17px;火狐與IE下均爲一致.
接着咱們來看scrollHeightexpress
function justAtest()
{
var test= document.getElementById("test");
var test2=document.getElementById("test2")
var test3=document.getElementById("test3")
var test4=document.getElementById("test4");
alert(test4.scrollHeight);
alert(test3.scrollHeight);
alert(test2.scrollHeight)
alert(test.scrollHeight);
alert(document.body.scrollHeight)
}瀏覽器
運行後火狐的結果爲:700,552,700,602,1002
IE的結果爲: 15, 15 , 700,602, 552
scrollHeight:這個屬性就比較麻煩了,由於它們在火狐跟IE下簡直差太多了..
在火狐下還很好理解,它其實就是滾動條可滾動的部分還要加上boder的高度還要加上橫向滾動條不可用的高度,與clientHeight比起來,多個border的高度跟橫向滾動條不可用的高度.
在IE中 scrollHeight確是指這個對象它所包含的對象的高度加上boder的高度和marging,若是它裏面沒有包含對象或者這個對象的高度值未設置,那麼它的值將爲15
最後咱們來看offsetHeightapp
function justAtest()
{
var test= document.getElementById("test");
var test2=document.getElementById("test2")
var test3=document.getElementById("test3")
var test4=document.getElementById("test4");
alert(test4.offsetHeight);
alert(test3.offsetHeight);
alert(test2.offsetHeight)
alert(test.offsetHeight);
alert(document.body.offsetHeight)
}測試
offsetHeight:
FF:700,552,602,502,1002
IE:700,552,602,502,1002
這個屬性好辦,由於在測試中IE跟火狐的結果是同樣的,均表示是自身的高度,若是有設置boder的話還應該加上boder的值,由於除了test4這個div外,其餘的div均有設置border=1px,因此552=550+2,其餘的均同樣.
嘿嘿,綜上所述,clientHeight與height的區別是若是有滾動條時應減去滾動條的17px不可用部分,offsetHeight與Height的區別是增長了boder的高度,ScrollHeihgt與Height的區別是火狐下與offsetHeight一致,IE下如上所述.
相信你瞭解了這個,對width,clientWidth,scrollWidth,offsetWidth已經不陌生了吧,只不過一個是長一個是寬的問題了.this
整合應用:只要引入js,支持自動到頂/底導航。spa
common_scroll.js
/**
* 回到頂部 auto scroll 頁面引入生效
*/
;
(function($) {
/**
* dialog配置
*/
$.auto_scroll = function(options) {
};
$.extend($.auto_scroll.prototype, {
defaults : {
classes : {
"scroll_fixed_" : "scroll_fixed_",
"ie_scroll_fixed_up" : "ie_scroll_fixed_up",
"ie_scroll_fixed_down" : "ie_scroll_fixed_down",
"x_scroll_goto_top" : "x_scroll_goto_top",
"x_scroll_goto_down" : "x_scroll_goto_down"
},
styles : {
scroll_fixed_ : {
'position' : 'fixed',
'width' : '30px',
'height' : '30px',
'cursor' : 'pointer',
'border-radius' : '2px'
},
ie_scroll_fixed_up : {
'_position' : 'absolute',
'_clear' : 'both',
'_top' : 'expression(eval(document.compatMode && document.compatMode=="CSS1Compat") ? documentElement.scrollTop +(documentElement.clientHeight-this.clientHeight) - 38 : document.body.scrollTop +(document.body.clientHeight-this.clientHeight) - 38)'
},
ie_scroll_fixed_down : {
'_position' : 'absolute',
'_clear' : 'both',
'_top' : 'expression(eval(document.compatMode && document.compatMode=="CSS1Compat") ? documentElement.scrollTop +(documentElement.clientHeight-this.clientHeight) - 8 : document.body.scrollTop +(document.body.clientHeight-this.clientHeight) - 8)'
},
x_scroll_goto_top : {
'border' : '1px solid #DDD',
'font' : '25px',
'color' : '#4e736e',
'line-height' : '30px',
'font-color' : '#DBF1EE',
'text-align' : 'center',
'right' : '8px',
'display':'none'
},
x_scroll_goto_down : {
'border' : '1px solid #DDD',
'font' : '25px',
'color' : '#4e736e',
'line-height' : '30px',
'font-color' : '#DBF1EE',
'text-align' : 'center',
'right' : '8px',
'display':'none'
}
}
}
});
})(jQuery);
var my_scroll_operator_object = {
/* scroll卷太高度 */
_getWindowScrollTop : function() {
var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
if (document.body) {
bodyScrollTop = document.body.scrollTop;
}
if (document.documentElement) {
documentScrollTop = document.documentElement.scrollTop;
}
scrollTop = (bodyScrollTop - documentScrollTop > 0)
? bodyScrollTop
: documentScrollTop;
return scrollTop;
},
/* 文檔的真實內容高度 */
_getWindowScrollHeight : function() {
var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
if (document.body) {
bodyScrollHeight = document.body.scrollHeight;
}
if (document.documentElement) {
documentScrollHeight = document.documentElement.scrollHeight;
}
scrollHeight = (bodyScrollHeight - documentScrollHeight > 0)
? bodyScrollHeight
: documentScrollHeight;
return scrollHeight;
},
/* 文檔窗口高度(含邊框) */
_getWindowOffsetHeight : function() {
var offsetHeight = 0, bodyOffsetHeight = 0, documentOffsetHeight = 0;
if (document.body) {
bodyOffsetHeight = document.body.offsetHeight;
}
if (document.documentElement) {
documentOffsetHeight = document.documentElement.offsetHeight;
}
offsetHeight = (bodyOffsetHeight - documentOffsetHeight > 0)
? bodyOffsetHeight
: documentOffsetHeight;
return offsetHeight;
},
/* 瀏覽器窗口可見高度 */
_getWindowHeight : function() {
var windowHeight = 0;
if (document.compatMode == "CSS1Compat") {
windowHeight = document.documentElement.clientHeight;
} else {
windowHeight = document.body.clientHeight;
}
return windowHeight;
},
/* y軸scroll是否存在 */
_getWindowScrollYexits : function() {
return (this._getWindowScrollHeight() > this._getWindowOffsetHeight())
? true
: false;
},
/* 數據類型 */
__getClass : function(object) {
return Object.prototype.toString.call(object)
.match(/^\[object\s(.*)\]$/)[1];
},
/* css對象轉style串 */
_getStyleStr : function(item) {
var styleStr = '';
if (this.__getClass(item) == 'Object') {
for (var p in item) {
var objVal = item[p];
styleStr += p + ":" + objVal + ";";
}
}
return styleStr;
},
/**
* 初始化回到頂部
*/
_initUpScroll : function() {
if (this._getWindowScrollYexits()) {
this._initUpScrollStyle();
this._initUpScrollBody();
}
},
_initUpScrollStyle : function() {
/* 加載樣式,暫時style中不支持 filter ,就往head中加style FIXEDME */
var defaults = $.auto_scroll.prototype.defaults;
var drag_style = '<style type="text/css">';
var headHtml = $("head").html();
if (!(headHtml && headHtml
.indexOf('.' + defaults.classes.scroll_fixed_) > -1)) {
drag_style += '.' + defaults.classes.scroll_fixed_ + '{';
drag_style += this._getStyleStr(defaults.styles.scroll_fixed_);
drag_style += '}';
}
headHtml = $("head").html();
if (!(headHtml && headHtml
.indexOf('.' + defaults.classes.ie_scroll_fixed_up) > -1)) {
drag_style += '.' + defaults.classes.ie_scroll_fixed_up + '{';
drag_style += this._getStyleStr(defaults.styles.ie_scroll_fixed_up);
drag_style += '}';
}
headHtml = $("head").html();
if (!(headHtml && headHtml
.indexOf('.' + defaults.classes.x_scroll_goto_top) > -1)) {
drag_style += '.' + defaults.classes.x_scroll_goto_top + '{';
drag_style += this._getStyleStr(defaults.styles.x_scroll_goto_top);
drag_style += '}';
}
drag_style += '</style>';
$("head").append(drag_style);
},
_initUpScrollBody : function() {
$("body")
.append('<div class="scroll_fixed_ ie_scroll_fixed_up x_scroll_goto_top" title="\u56de\u9876\u90e8">\u2191</div>');
$(window).scroll(function() {
var sctop = my_scroll_operator_object._getWindowScrollTop();
if (sctop > 50) {
$(".x_scroll_goto_top").fadeIn("middle");
} else {
$(".x_scroll_goto_top").fadeOut("middle");
}
});
/* 回到頂部 */
$('.x_scroll_goto_top').click(function() {
$('html, body').animate({
scrollTop : 0
}, 'middle');
});
$(".x_scroll_goto_top").mouseover(function(e) {
$(this).css({
"color" : "green",
"background" : "#e9e9e9",
"filter": "alpha(opacity=80)",
"-moz-opacity":"0.8",
"opacity": "0.8"
});
}).mouseout(function(e) {
$(this).css({
"color" : "#4e736e",
"background" : ""
});
});
},
/**
* 初始化回到底部
*/
_initDownScroll : function() {
if (this._getWindowScrollYexits()) {
this._initDownScrollStyle();
this._initDownScrollBody();
}
},
_initDownScrollStyle : function() {
/* 加載樣式,暫時style中不支持 filter ,就往head中加style FIXEDME */
var defaults = $.auto_scroll.prototype.defaults;
var drag_style = '<style type="text/css">'; var headHtml = $("head").html(); if (!(headHtml && headHtml .indexOf('.' + defaults.classes.scroll_fixed_) > -1)) { drag_style += '.' + defaults.classes.scroll_fixed_ + '{'; drag_style += this._getStyleStr(defaults.styles.scroll_fixed_); drag_style += '}'; } headHtml = $("head").html(); if (!(headHtml && headHtml .indexOf('.' + defaults.classes.ie_scroll_fixed_down) > -1)) { drag_style += '.' + defaults.classes.ie_scroll_fixed_down + '{'; drag_style += this._getStyleStr(defaults.styles.ie_scroll_fixed_down); drag_style += '}'; } headHtml = $("head").html(); if (!(headHtml && headHtml .indexOf('.' + defaults.classes.x_scroll_goto_down) > -1)) { drag_style += '.' + defaults.classes.x_scroll_goto_down + '{'; drag_style += this._getStyleStr(defaults.styles.x_scroll_goto_down); drag_style += '}'; } drag_style += '</style>'; $("head").append(drag_style); }, _initDownScrollBody : function() { $("body") .append('<div class="scroll_fixed_ ie_scroll_fixed_down x_scroll_goto_down" title="\u5230\u5e95\u90e8">\u2193</div>'); $(".x_scroll_goto_down").fadeIn("middle"); $(window).scroll(function() { var sctop = my_scroll_operator_object._getWindowScrollHeight() - (my_scroll_operator_object._getWindowScrollTop() + my_scroll_operator_object._getWindowHeight()); if (sctop < 50) { $(".x_scroll_goto_down").fadeOut("middle"); } else { $(".x_scroll_goto_down").fadeIn("middle"); } }); /* 回到頂部 */ var window_scrollHeight = my_scroll_operator_object._getWindowScrollHeight(); $('.x_scroll_goto_down').click(function() { $('html, body').animate({ scrollTop : window_scrollHeight }, 'middle'); }); $(".x_scroll_goto_down").mouseover(function(e) { $(this).css({ "color" : "green", "background" : "#e9e9e9", "filter": "alpha(opacity=80)", "-moz-opacity":"0.8", "opacity": "0.8" }); }).mouseout(function(e) { $(this).css({ "color" : "#4e736e", "background" : "" }); }); } } $(document).ready(function() { //等待數據 setTimeout(function(){ my_scroll_operator_object._initDownScroll(); my_scroll_operator_object._initUpScroll(); },2500); });