obj.offsetHeight与obj.style.height的区别

本文探讨了在引入W3C支持的HTML文档中,使用DOM操作时JS中对象属性的不同表现,包括offsetHeight与style.height的区别,以及如何正确获取元素的实际高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当我们在jsp头部加了W3C的支持后,有些以前的JS会出现取不到值的情况

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

一、obj.offsetHeight与obj.style.height的区别

       我们知道 offsetTop 可以获得 HTML 元素距离上方或外层元素的位置,style.top 也是可以的,二者的区别是:


       1、offsetTop 返回的是数字,而 style.top 返回的是字符串,除了数字外还带有单位:px。

       2、offsetTop 只读,而 style.top 可读写。

       3、如果没有给 HTML 元素指定过 top 样式,则 style.top 返回的是空字符串。

       offsetLeft 与 style.left、offsetWidth 与 style.width、offsetHeight 与 style.height 也是同样道理。

 
    <div style="width:100px;height:100px;background-color:#FF0000;overflow:hidden;border:2px solid #00ff00;" id="p"></div> 
    <script type="text/javascript"> 
    var p = document.getElementById("p"); 
    window.status = "height is:" + p.style.height + " - offsetHeight is:" + p.offsetHeight; 
    </script> 

****如果给某个DIV没有指定height,那么你试图读取style.height的时候会读到空,而offsetHeight可以读取div的是实际高度。


<template> <div> <virtual-scroll :data="obj.content" :item-size="chartHeight" :visible-count="visibleChartCount" > <template v-slot="{ item, index }"> <div class="chartsList"> <echarts-one-component v-if="item.type === '1'" :obj="item" v-show="isShow" :ref="'child' + index" /> <echarts-two-component v-else-if="item.type === '2'" :obj="item" v-show="isShow" :ref="'child' + index" /> <TableComponent v-else :obj="item" v-show="isShow" :ref="'table' + index" /> </div> </template> </virtual-scroll> </div> </template> <script> const echarts = require('echarts') import EchartsOneComponent from './echartsOneComponent' import EchartsTwoComponent from './echartsTwoComponent' import TableComponent from './tableComponent' import JSZip from 'jszip' import { downLoadPPT, fastApi } from '@/api/ai/aiDashboard' import VirtualScroll from 'vue-virtual-scroll-list' export default { components: { EchartsOneComponent, TableComponent, EchartsTwoComponent, VirtualScroll }, name: 'dashboardOneComponent', props: { obj: { type: Object } }, watch: { obj: { handler(newVal) { //只有当DONE后才渲染 if (newVal.finish === true) { const refObj = this.$refs this.isShow = true for (var i = 0; i < this.obj.content.length; i++) { if (refObj['child' + i]) { refObj['child' + i][0].init(this.obj.content[i]) } if (refObj['table' + i]) { refObj['table' + i][0].init(this.obj.content[i]) } } this.$emit('scrollDown') } }, immediate: true, deep: true } }, data() { return { isShow: false, isDownload:false, chartHeight: 300, // 图表高度,根据实际情况调整 visibleChartCount: 5 // 可视区域显示的图表数量,根据屏幕大小调整 } }, created() { }, mounted() { }, methods: { //生成PPTZIP暂时注释 async download() { if (this.isDownload){ return } this.isDownload = true const hide = this.$message.loading('正在下载,大概需要1-2分钟,请耐心等待', 0) try { const zip = new JSZip() const charts = [] // 所有图表实例 const analyse = [] // 所有分析数据 const refObj = this.$refs for (var i = 0; i < this.obj.content.length; i++) { if (this.obj.content[i].echatsName === '新能源') { this.obj.content[i].echatsName = '私家车新能源' } else if (this.obj.content[i].echatsName === '燃油车') { this.obj.content[i].echatsName = '私家车燃油车' } else if (this.obj.content[i].echatsName === '拓展') { this.obj.content[i].echatsName = '业务品质监控扩展(计划)' } if (refObj['child' + i]) { charts.push( { obj:refObj['child' + i][0].getEcharts(), name: refObj['child' + i][0].getEchartTitle() + '@' + this.obj.content[i].echatsName, type: 'echats' }) analyse.push( { question: JSON.stringify(this.obj.content[i]), name: refObj['child' + i][0].getEchartTitle(), direction: this.obj.content[i].type === '1' ? '{direction:若是对分值的分析,则主要分析1分10分数据,以及1分10分各个月份的变化。若是对违章次数的分析,则主要分析0违章-99+空违章占比趋势以及各个月份的变化。}' : '{direction:分析LRPLR之间的趋势,若是二者差异大,则指出二者差异大的区间,若是二者整体相似,则输出LRPLR基本靠近,符合预期。}' }) }else{ const tableImage = refObj['table' + i][0].exportImage() charts.push({ obj:tableImage, name: refObj['table' + i][0].getTableTitle() + '@' + this.obj.content[i].echatsName, type: 'table' }) analyse.push({ question: JSON.stringify(this.obj.content[i]), name: refObj['table' + i][0].getTableTitle(), direction: '{direction:若是对高档位(高违章高占比等)的分析,则不对北京机构进行分析输出,需要分析出占比明显偏高偏少的数据,以及整体的变化趋势。若是对未开启占比进行分析,则要看一个时间趋势,机构之间没有相对性,看月份的变化,比如有没有新开的、新关的、逐月增加或减少的。}' }) } } const formData = new FormData() // 对接工作流 await fastApi(analyse).then(res => { if (res.state === 'success') { // 获取分析数据数组 const analyseData = res.data // 转换为json文件,交给后端进行解析,用于整合进ppt // const rawJsonBlob = new Blob([JSON.stringify(analyseData)], { type: 'application/json' }) // console.log(rawJsonBlob) // formData.append('analysisData', rawJsonBlob, 'analysis_data.json') for (let i = 0; i < charts.length; i++) { if (charts[i].type === 'table') { zip.file(charts[i].name + '.png', charts[i].obj.split(',')[1], { base64: true }) } else { const imgData = charts[i].obj.getDataURL({ type: 'png', pixelRatio: 2, backgroundColor: '#fff' }) //formData.append('file[]', imgData) zip.file(charts[i].name + '.png', imgData.split(',')[1], { base64: true }) } } zip.generateAsync({ type: 'blob' }).then(content => { formData.append('file', content, 'images.zip') downLoadPPT(formData) .then(res => { const blob = new Blob([res], { type: res.type }) const downloadElement = document.createElement('a') const href = window.URL.createObjectURL(blob) //创建下载的链接 downloadElement.href = href downloadElement.download = 'test.pptx' document.body.appendChild(downloadElement) downloadElement.click() //点击下载 document.body.removeChild(downloadElement) //下载完成移除元素 window.URL.revokeObjectURL(href) //释放blob对象 this.$message.success('下载成功') }).catch((error) => { }) }) } else { console.error('请求失败:', res.message) this.errorMessage = '分析请求失败,请重试' } }).catch(error => { console.error('网络错误:', error) this.errorMessage = '网络请求异常,请检查连接' }) // setTimeout(() => { // downLoadPPT(formData) // .then(res => { // const blob = new Blob([res], {type: res.type}) // const downloadElement = document.createElement('a') // const href = window.URL.createObjectURL(blob) //创建下载的链接 // downloadElement.href = href // downloadElement.download = 'test.pptx' // document.body.appendChild(downloadElement) // downloadElement.click() //点击下载 // document.body.removeChild(downloadElement) //下载完成移除元素 // window.URL.revokeObjectURL(href) //释放blob对象 // this.$message.success('下载成功') // }).catch((error) => { // }) // }, 15000) } catch (err) { this.$message.warn('下载PPT异常请稍后再试') } finally { this.isDownload = false this.$message.destroy(hide) } } /*download() { const zip = new JSZip() const charts = [] // 所有图表实例 const refObj = this.$refs for (var i = 0; i < this.obj.content.length; i++) { if (refObj['child' + i]) { charts.push(refObj['child' + i][0].getEcharts()) } } const pdf = new jsPDF('landscape', 'pt', 'a4') // 横向A4纸‌:ml-citation{ref="2,7" data="citationList"} const pageHeight = pdf.internal.pageSize.getHeight() let currentY = 0 // 当前绘制高度 for (let i = 0; i < charts.length; i++) { const imgData = charts[i].getDataURL({type: 'png', pixelRatio: 2,backgroundColor: '#fff'}) const imgProps = pdf.getImageProperties(imgData) const imgWidth = pdf.internal.pageSize.getWidth() const imgHeight = (imgProps.height * imgWidth) / imgProps.width // 分页判断 if (currentY + imgHeight > pageHeight) { pdf.addPage() currentY = 0 } pdf.addImage(imgData, 'PNG', 0, currentY, imgWidth, imgHeight) currentY += imgHeight + 20 //zip.file(`chart${i + 1}.png`, imgData.split(',')[1],{ base64: true }) } pdf.save('chart.pdf') //pdf.save('chart.pdf') /!*zip.generateAsync({type: 'blob'}).then(content => { saveAs(content, 'charts.zip') })*!/ }*/ } } </script> <style scoped> .chartsList{ display: flex; justify-content: center; text-align: center; margin-bottom: 25px; height: 300px;/* 根据实际高度设置 */ overflow: hidden; } </style>
最新发布
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值