npm install html2canvas -d
利用插件將dom元素轉成圖片,此時將content下載成圖片,里面包含了一張照片和定位在中間的span標簽,
只要在content元素內部,無論怎么寫樣式,都在該元素內,都會通過插件轉成圖片

1 <template> 2 <div class="home"> 3 <div class="content" ref="content"> 4 <img :src="imgUrl" alt=""> 5 <span>hahahah</span> 6 </div> 7 </div> 8 </template> 9 10 <script> 11 import html2canvas from 'html2canvas' 12 13 export default { 14 data() { 15 return { 16 imgUrl: require("../assets/20190111225623.png") 17 } 18 }, 19 mounted() { 20 this.$nextTick(() => { 21 html2canvas(this.$refs.content).then(resolve => { 22 let imgUrl = resolve.toDataURL('image/png'); //此時就得到了dom元素轉成了base64的圖片 23 console.log(imgUrl) 24 }) 25 }) 26 } 27 } 28 </script> 29 30 <style lang="less"> 31 .content { 32 position: relative; 33 img { 34 width: 100%; 35 height: 100%;37 display: block; 38 } 39 span { 40 position: absolute; 41 top: 0; 42 left: 50%; 43 transform: translate(-50%); 44 } 45 } 46 </style>
