<script>
export default {
data() {
return {
eleIndex: 10,
windowHeight: 0,
scrollHeight: 0,
};
},
mounted() {
this.windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
window.onscroll = () => {
const obj = this.GetRect(`#hideMusicVar${this.eleIndex}`);
if (obj.bottom <= 0 || obj.top >= this.windowHeight) {
this.pauseAudio();
}
};
this.scrollHeight = $('#letterList').offset().height;
document.querySelector('#letterList').onscroll = () => {
const scrollTop = $('#letterList').scrollTop();
const obj = this.GetEleRect(`#hideMusicVar${this.eleIndex}`);
if (obj.bottom <= scrollTop || obj.top >= scrollTop + this.scrollHeight) {
this.pauseAudio();
}
};
},
methods: {
GetRect(ele) {
ele = document.querySelector(ele);
const rect = ele.getBoundingClientRect();
const top = document.documentElement.clientTop ? document.documentElement.clientTop : 0;
const left = document.documentElement.clientLeft ? document.documentElement.clientLeft : 0;
return {
top: rect.top - top,
bottom: rect.bottom - top,
left: rect.left - left,
right: rect.right - left
};
},
GetEleRect(ele) {
const eleTop = $(ele).position().top;
const eleHeight = $(ele).offset().height;
const eleLeft = $(ele).position().left;
const eleWidth = $(ele).offset().width;
return {
top: eleTop,
bottom: eleTop + eleHeight,
left: eleLeft,
right: eleLeft + eleWidth,
};
},
},
};
</script>