vue3实现下载滚动页面的全部内容

npm install html2canvas
<template>
  <div ref="contentToCapture" class="scrollable-content">
    <!-- Your scrollable content here -->
    <div v-for="item in items" :key="item.id" class="item">
      {{ item.content }}
    </div>
  </div>
  <button @click="captureAndDownload">Download as Image</button>
</template>

<script>
import html2canvas from 'html2canvas';

export default {
  data() {
    return {
      items: [
        { id: 1, content: 'Item 1' },
        { id: 2, content: 'Item 2' },
        { id: 3, content: 'Item 3' },
        // Add more items as needed
      ]
    };
  },
  methods: {
    async captureAndDownload() {
      const element = this.$refs.contentToCapture;
      
      // Ensure the element is fully visible and not clipped
      element.style.height = 'auto';
      element.style.overflow = 'visible';

      const canvas = await html2canvas(element, {
        useCORS: true,
        scale: 2, // Increase the scale for better quality
        windowWidth: document.documentElement.scrollWidth,
        windowHeight: document.documentElement.scrollHeight,
      });

      const image = canvas.toDataURL('image/png');
      const link = document.createElement('a');
      link.href = image;
      link.download = 'captured-content.png';
      link.click();

      // Restore the original styles
      element.style.height = '';
      element.style.overflow = '';
    }
  }
};
</script>

<style>
.scrollable-content {
  width: 100%;
  max-height: 400px;
  overflow-y: scroll;
  border: 1px solid #ccc;
}

.item {
  padding: 10px;
  border-bottom: 1px solid #ddd;
}
</style>

我的代码参考了上图 

实际代码如下图所示:

相关推荐

  1. vue3SeamlessScroll实现列表无限循环滚动

    2024-07-09 21:56:04       19 阅读
  2. vue3-seamless-scroll实现循环滚动

    2024-07-09 21:56:04       22 阅读
  3. [Vue3]-router实现基本页面跳转

    2024-07-09 21:56:04       48 阅读
  4. vue页面滚动条,打开新页面页面不置顶问题

    2024-07-09 21:56:04       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 21:56:04       50 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 21:56:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 21:56:04       43 阅读
  4. Python语言-面向对象

    2024-07-09 21:56:04       54 阅读

热门阅读

  1. Kafka 典型问题与排查以及相关优化

    2024-07-09 21:56:04       19 阅读
  2. SQL中字符串类型中char和varchar之间的区别是什么

    2024-07-09 21:56:04       19 阅读
  3. 【框架】ABP(ASP.NET Boilerplate Project)

    2024-07-09 21:56:04       19 阅读
  4. SQL Server集成服务(SSIS):数据集成的瑞士军刀

    2024-07-09 21:56:04       19 阅读
  5. LVS+Keepalived群集

    2024-07-09 21:56:04       17 阅读
  6. 精准控制:Eureka服务续约间隔配置全指南

    2024-07-09 21:56:04       23 阅读
  7. 部署LVS-DR群集

    2024-07-09 21:56:04       24 阅读
  8. WordPress禁止用户注册某些用户名

    2024-07-09 21:56:04       22 阅读