关于如何在不使用jQuery的情况下获得div高度的任何想法?
我正在搜索Stack Overflow这个问题,似乎每个答案都指向jQuery .height().
我尝试了类似的东西myDiv.style.height,但它什么也没有返回,即使我的div有它width并且height设置在CSS中.
我目前正在使用selenium webdriver来解析Facebook用户朋友页面并从AJAX脚本中提取所有ID.但我需要向下滚动才能吸引所有朋友.如何在Selenium中向下滚动.我正在使用python.
我有一个帮助页面,help.php,我加载一个iframe里面main.php我怎样才能得到这个页面的高度,一旦它在iframe中加载?
我问这个是因为我无法将iframe的高度设置为100%或auto.这就是为什么我认为我需要使用javascript ..我正在使用jQuery
CSS:
body {
margin: 0;
padding: 0;
}
.container {
width: 900px;
height: 100%;
margin: 0 auto;
background: silver;
}
.help-div {
display: none;
width: 850px;
height: 100%;
position: absolute;
top: 100px;
background: orange;
}
#help-frame {
width: 100%;
height: auto;
margin:0;
padding:0;
}
Run Code Online (Sandbox Code Playgroud)
JS:
$(document).ready(function () {
$("a.open-help").click(function () {
$(".help-div").show();
return false;
})
})
Run Code Online (Sandbox Code Playgroud)
HTML:
<div class='container'>
<!-- -->
<div class='help-div'>
<p>This is a div with an iframe loading the help page</p>
<iframe id="help-frame" src="../help.php" …Run Code Online (Sandbox Code Playgroud) 我有这个我通过Id引用的元素:
let infiniteScrollElement = document.getElementById('th-infinite-scroll-tracker');
Run Code Online (Sandbox Code Playgroud)
当浏览器到达元素的底部时我需要监听.
怎么做到这一点?
有没有办法返回已设置为auto的元素的高度?当我调用$("#element")时,我得到0.高度();
这是jquery代码.所述img.height()返回0因此,最终结果是关闭的.
img.css({top: (img.parent().height() - img.height()) /2, left: (img.parent().outerWidth() - img.outerWidth()) / 2});
Run Code Online (Sandbox Code Playgroud)
HTML看起来像这样:
<div id="exploreImage" class="virtual-room-large" style="width: 288px; height: auto; top: 185px; left: 89px; ">
Run Code Online (Sandbox Code Playgroud) 获得元素高度的最佳方法是什么:
var myElement = document.querySelector('.some-class');
var height = myElement.getBoundingClientRect().height;
Run Code Online (Sandbox Code Playgroud)
要么
var myElement = document.querySelector('.some-class');
var height = myElement.offsetHeight;
Run Code Online (Sandbox Code Playgroud) 所以我的问题是,当我得到$(window).height()一个空文档时,它给出了正确的值,但是当我为 div ( .main-header)设置高度时,$(window).height()它的值加上接近 div 高度的值,
当我height为.main-headerdiv设置0时的这张图片
当我设置 700pxheight时.main-header
我试过 $(window).load() 和 $(document).ready() 并且都给出了相同的值https://jsfiddle.net/nev5onff/
$(window).load(function () {
var header = $('.main-header'),
windowH = $(window).height();
$('.test').text( windowH);
});Run Code Online (Sandbox Code Playgroud)
.main-header {
width: 100%;
height: 700px;
box-shadow: 0 1px 4px #888;
position: relative;
overflow: hidden;
}
.test {
position: fixed;
top: 0;
left: 0;
width: 100px;
float: left;
height: 100px;
background-color: #eee;
color: #000;
padding: 20px;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div …Run Code Online (Sandbox Code Playgroud)var div = document.getElementById('foo');
console.log(div);
console.log(div.scrollHeight);
Run Code Online (Sandbox Code Playgroud)
当我在控制台点击第一条log返回的div的DOM时,它的scrollHeight为x,而第二条log打印出y;x != y。
我想动画div的高度.这通常通过动画max-height属性在CSS中完成.
但是我需要在JS中这样做.div中填充了频繁更改的动态内容,因此未提前知道实际高度.
这是一个jsfiddle:https://jsfiddle.net/vpknxuk8/
var box = document.getElementById("box");
var button = document.getElementById("button");
var expanded = true;
button.addEventListener('click', function() {
if (expanded) {
box.style.maxHeight = 0;
expanded = false;
} else {
box.style.maxHeight = "";
expanded = true;
}
});Run Code Online (Sandbox Code Playgroud)
#box {
margin-top: 20px;
border: 1px solid black;
background-color: #4f88ff;
width: 200px;
transition: max-height 0.25s linear;
overflow: hidden;
}Run Code Online (Sandbox Code Playgroud)
<button id="button">Click</button>
<div id="box">
Hello World<br>
This is dynamic content<br>
The actual height won't be known ahead of time …Run Code Online (Sandbox Code Playgroud)计算scrollHeight滚动条底部距离其总底部(即页面的末尾)有多远的数学公式
是什么?因此,例如,当滚动条位于顶部时,我会说它底部与文档底部的距离为0%,并且当它完全滚动(垂直)时,它将是100%。
我的目标是计算底部和特定位置之间有多少像素,例如3%,相对于该底部上方的视口。同样,文档高度应该没有任何意义。如果相对于视口,3% 是 3%。
var P = 3 // in %
var totalHeight = document.documentElement.scrollHeight;
var viewportHeight = document.documentElement.clientHeight;
Run Code Online (Sandbox Code Playgroud)