简体   繁体   English

滚动事件更改字体大小后文本闪烁

[英]Text flickering after font-size change by scroll event

I have this JavaScript code that changes font-size based on window.scrollY .我有这个基于window.scrollY更改font-size的 JavaScript 代码。

 var title = document.querySelector('.title') var titleFontSizePX = getComputedStyle(title).fontSize var titleFontSizePXInt = parseFloat(titleFontSizePX) var titleFontSizeVWInt = titleFontSizePXInt * (100 / window.innerWidth) var titleFontSizeVW = titleFontSizeVWInt + 'vw' title.style.fontSize = titleFontSizeVW function updateFontSize(event) { console.log('fire!', event.type, window.scrollY) var titleSizeMax = titleFontSizeVWInt - 0.02 * window.scrollY var titleSizeMin = titleFontSizeVWInt * 2 / 5 title.style.fontSize = Math.max(titleSizeMin, titleSizeMax) + 'vw' } var events = ['scroll', 'touchmove'] events.forEach(event => document.addEventListener(event, updateFontSize))
 body { background: #111; padding: 40vh 10vw 10vw; font-family: 'Rubik'; color: white; } h1 { font-size: 12vw; line-height: 1em; font-weight: 700; } p { font-size: 1.4rem; line-height: 1.6em; font-weight: 400; margin-top: 2em; max-width: 50%; }
 <h1 class="title"> This is a big<br> hero copy to<br> say a couple<br> words about<br> this website. </h1> <p>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin.</p> <p>The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. "What's happened to me?" he thought. It wasn't a dream. His room, a proper human room although a little too small, lay peacefully between its four familiar walls.</p> <p>A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer.</p>

Everything seems to be working fine, except that sometimes the scroll event keeps firing multiple times, causing the text to flick in a loop.一切似乎都运行良好,只是有时滚动事件会多次触发,导致文本循环轻弹。 I've tested different debugging methods, like debouncing, throttling, setting line-height manually, etc, but none worked.我测试了不同的调试方法,例如去抖动、节流、手动设置line-height高等,但都没有奏效。

Also, you can type in console window.scrollTop(0, number) to cause the flickering effect.此外,您可以在控制台window.scrollTop(0, number)键入以引起闪烁效果。 Try different numbers.尝试不同的数字。 At some point it'll happen.在某个时候它会发生。

Don't know for sure what is causing it, so some help would be appreciated.不知道是什么原因造成的,所以一些帮助将不胜感激。

Here's a video: https://youtu.be/Kiwwcabmqcc这是一个视频: https : //youtu.be/Kiwwcabmqcc

And a Codepen: https://codepen.io/podrivo/pen/zebWmW还有一个 Codepen: https ://codepen.io/podrivo/pen/zebWmW

Thanks!谢谢!

The solution is to set the last scroll position to be different from the actual scroll position. 解决方案是将最后一个滚动位置设置为与实际滚动位置不同。 So the JavaScript is: 所以JavaScript是:

var title               = document.querySelector('.title')
var titleFontSizePX     = getComputedStyle(title).fontSize
var titleFontSizePXInt  = parseInt(titleFontSizePX, 10)

var titleFontSizeVWInt  = titleFontSizePXInt * (100 / window.innerWidth)
var titleFontSizeVW     = titleFontSizeVWInt + 'vw'
title.style.fontSize    = titleFontSizeVW

var lastScroll          = 0

function updateFontSize(event) {
  if (lastScroll != window.scrollY) {
    var titleSizeMax      = titleFontSizeVWInt - 0.02 * window.scrollY
    var titleSizeMin      = titleFontSizeVWInt * 2 / 5
    title.style.fontSize  = Math.max(titleSizeMin, titleSizeMax) + 'vw'
  }
  lastScroll = window.scrollY
}

var events = ['scroll', 'touchmove']
events.forEach(event => document.addEventListener(event, updateFontSize))

我也遇到了同样的问题,你解决了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM