如何计算工具栏,地址栏和其他导航工具的高度(以像素为单位)? [英] How do i calculate the height of toolbars, address bars and other navigation tools in pixels?
本文介绍了如何计算工具栏,地址栏和其他导航工具的高度(以像素为单位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上我需要知道y轴距离屏幕左上角有多少像素,直到我到达实际的Web窗口。有没有人有任何想法......?
Basically i need to know how many pixels the y axis is from the top left of the screen until i reach the actual web window. Does anyone have any ideas...?
推荐答案
我不知道如何返回外部组件的高度,但是我从 http://www.alexandre-gomes.com/?p=115 并将其调整为还返回滚动条高度。
I don't know how you can return the height of external component, but I took this from http://www.alexandre-gomes.com/?p=115 and adapted it to also return scroll bar height.
- Chrome
- FF 9+
- IE9 (thank you Pax0r)
- Opera (thank you Pax0r)
如果有人可以在其他浏览器中测试它给我反馈,我会相应地修改这个答案。
If anyone can test it in other browsers and give me feedback, I will modify this answer accordingly.
jQuery.getScrollBarSize = function() {
var inner = $('<p></p>').css({
'width':'100%',
'height':'100%'
});
var outer = $('<div></div>').css({
'position':'absolute',
'width':'100px',
'height':'100px',
'top':'0',
'left':'0',
'visibility':'hidden',
'overflow':'hidden'
}).append(inner);
$(document.body).append(outer);
var w1 = inner.width(), h1 = inner.height();
outer.css('overflow','scroll');
var w2 = inner.width(), h2 = inner.height();
if (w1 == w2 && outer[0].clientWidth) {
w2 = outer[0].clientWidth;
}
if (h1 == h2 && outer[0].clientHeight) {
h2 = outer[0].clientHeight;
}
outer.detach();
return [(w1 - w2),(h1 - h2)];
};
alert( $.getScrollBarSize() ); // in Chrome = [15,15] in FF = [16,16]
没有JQuery
Without JQuery
function getScrollBarSize () {
var inner = document.createElement('p');
inner.style.width = "100%";
inner.style.height = "100%";
var outer = document.createElement('div');
outer.style.position = "absolute";
outer.style.top = "0px";
outer.style.left = "0px";
outer.style.visibility = "hidden";
outer.style.width = "100px";
outer.style.height = "100px";
outer.style.overflow = "hidden";
outer.appendChild (inner);
document.body.appendChild (outer);
var w1 = inner.offsetWidth;
var h1 = inner.offsetHeight;
outer.style.overflow = 'scroll';
var w2 = inner.offsetWidth;
var h2 = inner.offsetHeight;
if (w1 == w2) w2 = outer.clientWidth;
if (h1 == h2) h2 = outer.clientHeight;
document.body.removeChild (outer);
return [(w1 - w2),(h1 - h2)];
};
这篇关于如何计算工具栏,地址栏和其他导航工具的高度(以像素为单位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文