简体   繁体   English

如何获取隐藏溢出的div的高度?

[英]How to get height of a div with overflow hidden?

I have a div as you will see in the jsfiddle, and I need to get the height of entire div(visible+overflowed).I tried some JQuery code but I can only get the visible height.我有一个 div,你将在 jsfiddle 中看到,我需要获得整个 div 的高度(可见 + 溢出)。我尝试了一些 JQuery 代码,但我只能获得可见高度。 How can I solve this?我该如何解决这个问题?

 $(document).ready(function() { var scrolled = 0; $(".down").on("click", function() { scrolled = scrolled + 150; $("#container").animate({ scrollTop: scrolled }); }); $(".up").on("click", function() { scrolled = scrolled - 150; $("#container").animate({ scrollTop: scrolled }); }); });
 #container { border: solid 1px red; overflow-y: hidden; overflow-x: hidden; margin: auto; position: absolute; height: 400px; width: 300px; top: 0; bottom: 0; left: 0; right: 0; } .cent { text-align: center; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="up">UP</button> <div id="container" class="cent"> <img id="image1" src="http://lorempixel.com/100/50/sports/1/"> <img id="image2" src="http://lorempixel.com/100/50/fashion/1/"> <img id="image3_small" src="http://lorempixel.com/100/50/city/1/"> <img id="image4" src="http://lorempixel.com/100/50/sports/1/"> <img id="image5" src="http://lorempixel.com/100/50/fashion/1/"> <img id="image6" src="http://lorempixel.com/100/50/city/1/"> <img id="image1" src="http://lorempixel.com/100/50/sports/1/"> <img id="image2" src="http://lorempixel.com/100/50/fashion/1/"> <img id="image3_small" src="http://lorempixel.com/100/50/city/1/"> <img id="image4" src="http://lorempixel.com/100/50/sports/1/"> <img id="image5" src="http://lorempixel.com/100/50/fashion/1/"> <img id="image6" src="http://lorempixel.com/100/50/city/1/"> <img id="image1" src="http://lorempixel.com/100/50/sports/1/"> <img id="image2" src="http://lorempixel.com/100/50/fashion/1/"> <img id="image3_small" src="http://lorempixel.com/100/50/city/1/"> <img id="image4" src="http://lorempixel.com/100/50/sports/1/"> <img id="image5" src="http://lorempixel.com/100/50/fashion/1/"> <img id="image6" src="http://lorempixel.com/100/50/city/1/"> <img id="image1" src="http://lorempixel.com/100/50/sports/1/"> <img id="image2" src="http://lorempixel.com/100/50/fashion/1/"> <img id="image3_small" src="http://lorempixel.com/100/50/city/1/"> <img id="image4" src="http://lorempixel.com/100/50/sports/1/"> <img id="image5" src="http://lorempixel.com/100/50/fashion/1/"> <img id="image6" src="http://lorempixel.com/100/50/city/1/"> <img id="image1" src="http://lorempixel.com/100/50/sports/1/"> <img id="image2" src="http://lorempixel.com/100/50/fashion/1/"> <img id="image3_small" src="http://lorempixel.com/100/50/city/1/"> <img id="image4" src="http://lorempixel.com/100/50/sports/1/"> <img id="image5" src="http://lorempixel.com/100/50/fashion/1/"> <img id="image6" src="http://lorempixel.com/100/50/city/1/"> </div> <button class="down">Down</button>

Any advice is highly welcome.非常欢迎任何建议。 Thanks!谢谢!

Use scrollHeight instead ( a DOM element property ):使用scrollHeight代替( 一个 DOM 元素属性):

$(selector)[0].scrollHeight

so in your example:所以在你的例子中:

var height= $("#container")[0].scrollHeight;

or (as Vohuman points out):或(如 Vohuman 指出的):

var height= $("#container").prop("scrollHeight");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM