Javascript 如何找出用户的高度';s设备屏幕小于页面内容的高度?
我正在用PHP制作一个web应用程序,仅当用户需要滚动页面时,才想在页脚中放置“Back to top”元素,否则不会显示。如何验证用户浏览器屏幕的高度太小,无法在没有框架的情况下显示Javascript 如何找出用户的高度';s设备屏幕小于页面内容的高度?,javascript,html,css,Javascript,Html,Css,我正在用PHP制作一个web应用程序,仅当用户需要滚动页面时,才想在页脚中放置“Back to top”元素,否则不会显示。如何验证用户浏览器屏幕的高度太小,无法在没有框架的情况下显示标记 现在,页脚看起来像: <footer class="container pt-4 my-md-5 pt-md-5 border-top"> <p class="float-right"><a href="#">Back to top</a><
标记
现在,页脚看起来像:
<footer class="container pt-4 my-md-5 pt-md-5 border-top">
<p class="float-right"><a href="#">Back to top</a></p>
<p>© 2020 · <a href="#" target="_blank">Telegram</a> · <a href="#" target="_blank">GitHub</a></p>
</footer>
©2020年·
您需要像这样计算相对于窗口高度的内容高度
var contentHeight=document.getElementById("content-id").clientHeight
var contentRelativeHeight= contentHieght / window.innerHeight
然后检查是否低于或等于1(即内容高度小于或等于设备屏幕高度)隐藏回顶部按钮
<p id="goTopBtn" class="float-right"><a href="#">Back to top</a></p>
if(contentRelativeHeight基于Ahmed的答案。解决方案是:
window.addEventListener("DOMContentLoaded", () => {
let contentHeight = document.getElementsByTagName("body")[0].clientHeight;
let contentRelativeHeight = contentHeight / window.innerHeight;
if (contentRelativeHeight <= 1) {
document.getElementById("to-top-btn").style.display = "none";
}
});
window.addEventListener(“DOMContentLoaded”,()=>{
让contentHeight=document.getElementsByTagName(“正文”)[0].clientHeight;
让contentRelativeHight=contentHeight/window.innerHeight;
if(contentRelativeHeight)谢谢!这很有帮助,不过我改了很多:window.addEventListener(“DOMContentLoaded”,()=>{let contentHeight=document.getElementsByTagName(“body”)[0]。clientHeight;let contentRelativeHeight=contentHeight/window.innerHeight;if(contentRelativeHeight
window.addEventListener("DOMContentLoaded", () => {
let contentHeight = document.getElementsByTagName("body")[0].clientHeight;
let contentRelativeHeight = contentHeight / window.innerHeight;
if (contentRelativeHeight <= 1) {
document.getElementById("to-top-btn").style.display = "none";
}
});