From 2796e6fcee624180f4471ff58e09e49a6aa75e82 Mon Sep 17 00:00:00 2001
From: Ardis Lu Try adjusting the height:const elem = document.querySelector('.dynamic-line-clamp');
const lh = Number(getComputedStyle(elem, '::after').getPropertyValue('height').slice(0, -2));
new ResizeObserver(() => {
- const lines = Math.round(elem.clientHeight / lh);
+ const lines = Math.floor(elem.clientHeight / lh);
elem.style.setProperty('--lines', lines);
}).observe(elem);line-height whenever the paragraph is resized. To do so, update ResizeObserver:
new ResizeObserver(() => {
- const lines = Math.round(elem.clientHeight / lh);
+ const lines = Math.floor(elem.clientHeight / lh);
elem.style.setProperty('--lines', lines);
elem.style.setProperty('height', `${lines * lh}px`);
}).observe(elem);
@@ -294,7 +294,7 @@ new ResizeObserver(() => {
}
You can update the observer to cancel if another event is fired within 200 milliseconds:
new ResizeObserver(debounce(() => {
- const lines = Math.round(elem.clientHeight / lh);
+ const lines = Math.floor(elem.clientHeight / lh);
elem.style.setProperty('--lines', lines);
elem.style.setProperty('height', `${lines * lh}px`);
}, 200)).observe(elem);
@@ -324,7 +324,7 @@ new ResizeObserver(() => {
const elem = document.querySelector('.dynamic-line-clamp');
const lh = Number(getComputedStyle(elem, '::after').getPropertyValue('height').slice(0, -2));
new ResizeObserver(debounce(() => {
- const lines = Math.round(elem.clientHeight / lh);
+ const lines = Math.floor(elem.clientHeight / lh);
elem.style.setProperty('--lines', lines);
elem.style.setProperty('height', `${lines * lh}px`);
}, 200)).observe(elem);
@@ -361,14 +361,14 @@ new ResizeObserver(debounce(() => {
const elem = document.querySelector('#basic-observer');
const lh = Number(getComputedStyle(elem, '::after').getPropertyValue('height').slice(0, -2));
new ResizeObserver(() => {
- const lines = Math.round(elem.clientHeight / lh);
+ const lines = Math.floor(elem.clientHeight / lh);
elem.style.setProperty('--lines', lines);
}).observe(elem);
const elem2 = document.querySelector('#full-observer');
const lh2 = Number(getComputedStyle(elem2, '::after').getPropertyValue('height').slice(0, -2));
new ResizeObserver(debounce(() => {
- const lines = Math.round(elem2.clientHeight / lh2);
+ const lines = Math.floor(elem2.clientHeight / lh2);
elem2.style.setProperty('--lines', lines);
elem2.style.setProperty('height', `${lines * lh2}px`);
}, 200)).observe(elem2);
--
2.47.3