1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| let contentHeight = 0 const content = document.getElementById('tv-content') document.addEventListener('scroll', () => { if (content.clientHeight !== contentHeight) { findTop5Hot() contentHeight = content.clientHeight } })
let set = new Set() const findTop5Hot = () => { const count = document.getElementsByClassName('tv-card-social-item__count'); const elWithCount = [...count].map(el => { const num = el.textContent return {el, num: Number(num)} }); const sorted = elWithCount.sort((a, b) => b.num - a.num) for (const el of set) { el.parentElement.removeChild(el) console.log(el) } set = new Set() for (let i = 0; i < 5; i++) { const item = sorted[i]
if (item) { item.el.style = 'background-color:red;color:white;font-size:16px;font-weight:bold;' insertButton(item.el, item.num, i) } } }
const insertButton = (el, num, i) => { const btn = document.createElement('button') btn.addEventListener('click', () => { el && el.scrollIntoView({block: "center", inline: "center"}) }) btn.textContent = `指标热度:${num}` btn.style = ` position: fixed; top: ${i * 32}px; right: 0px; z-index: 200000; color: #000; font-weight: bold; font-size: 18px; ` set.add(btn) document.body.append(btn) }
(() => { insertButton() })()
|