文档滚动高度不起作用

发布于 2025-01-31 21:50:07 字数 1420 浏览 3 评论 0原文

我的目的是使用以下方式检索页面的完整滚动高度:

document.body.scrollHeight

但是我回来的只是视口的高度,而不是可滚动页面的完整高度。

我正在以一种在安装后调用的方法运行。它甚至在nextick函数中。我需要将图像随机放置在页面的完整高度上。这可能与Vue的反应性(?)有关,并且DOM无法快速更新以使我的方法获得完整的文档高度?

我可以在VUE中获得文档的滚动高度的不同方式是什么?


<script>
export default {
data() {
 return {
   images: [
     require("@/assets/img/icons/1.png"),
     require("@/assets/img/icons/2.png"),
   ],
   addedImage: [],
   imgTop: 150,
   imgLeft: 900,
   imgHeight: 64,
   imgWidth: 64,
   selectedImage: "",
   nextId: 100,
 };
},
mounted() {
 this.randomImage();
 this.$nextTick(() => {
   for (let i = 0; i < 40; i++) {
     this.randomImage();
     this.addImage();
     this.randomPosition();
   }
 });
},

methods: {
 randomImage() {
   const idx = Math.floor(Math.random() * this.images.length);
   this.selectedImage = this.images[idx];
 },
 randomPosition() {
   const randomPos = (twoSizes) => Math.round(Math.random() * twoSizes);
   this.imgTop = randomPos(document.body.scrollHeight - this.imgHeight);
   this.imgLeft = randomPos(window.innerWidth - this.imgWidth);
 },
 addImage() {
   this.addedImage.push({
     style: {
       top: `${this.imgTop}px`,
       left: `${this.imgLeft}px`,
       height: `${this.imgHeight}px`,
       width: `${this.imgWidth}px`,
     },
     src: this.selectedImage,
     id: this.nextId++,
   });

 },

},
};
</script>

I'm aiming to retrieve the full scrollable height of my page using:

document.body.scrollHeight

but all I get back is the height of the viewport and not the full height of the scrollable page.

I'm running this in a method that is called after mounted. It's even in a nexttick function. I need this to randomly place images over the full height of the page. It could be something to do with Vue's reactivity(?) and the DOM not updating quick enough for my method to get the full document height?

What is a different way I can get the scroll height of a document in Vue that will actually work?


<script>
export default {
data() {
 return {
   images: [
     require("@/assets/img/icons/1.png"),
     require("@/assets/img/icons/2.png"),
   ],
   addedImage: [],
   imgTop: 150,
   imgLeft: 900,
   imgHeight: 64,
   imgWidth: 64,
   selectedImage: "",
   nextId: 100,
 };
},
mounted() {
 this.randomImage();
 this.$nextTick(() => {
   for (let i = 0; i < 40; i++) {
     this.randomImage();
     this.addImage();
     this.randomPosition();
   }
 });
},

methods: {
 randomImage() {
   const idx = Math.floor(Math.random() * this.images.length);
   this.selectedImage = this.images[idx];
 },
 randomPosition() {
   const randomPos = (twoSizes) => Math.round(Math.random() * twoSizes);
   this.imgTop = randomPos(document.body.scrollHeight - this.imgHeight);
   this.imgLeft = randomPos(window.innerWidth - this.imgWidth);
 },
 addImage() {
   this.addedImage.push({
     style: {
       top: `${this.imgTop}px`,
       left: `${this.imgLeft}px`,
       height: `${this.imgHeight}px`,
       width: `${this.imgWidth}px`,
     },
     src: this.selectedImage,
     id: this.nextId++,
   });

 },

},
};
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文