透過過渡縮放文字以適應瀏覽器的大小
P粉459578805
P粉459578805 2023-09-08 11:11:22

我有一些文本,我想在將滑鼠懸停在按鈕上時將其大小調整為瀏覽器的 100%。如何使該文字適合瀏覽器/容器?

我知道使用 font size ,您可以執行類似 font-size:100vw 的操作,但是 font-size 的動畫很不穩定,所以我無法使用此方法。

http://jsfiddle.net/0n15mfyL/

#對於那些想知道抖動的人,當字體縮小時會很明顯:http://jsfiddle.net/rbk48upd/

P粉459578805
P粉459578805

全部回覆(1)
P粉729518806

我正在考慮 requestAnimationFrame()

#
const 
  hoverMe        = document.querySelector('#hover-me')
, animTextSizing = document.querySelector('#anim-text-sizing')
, txtSizeStart   = parseInt(window.getComputedStyle(animTextSizing).getPropertyValue('font-size'))
  ;
let sizeLimit = 0,  currentTxtSize = txtSizeStart
  ;
hoverMe.onmouseover =_=>
  {
  sizeLimit = document.body.clientWidth;
  animGrow();
  }
hoverMe.onmouseout =_=>
  {
  sizeLimit = txtSizeStart;
  currentTxtSize++
  animReduce();
  }
function animGrow()
  {
  animTextSizing.style['font-size'] = `${++currentTxtSize}px`;

  if ( animTextSizing.clientWidth < sizeLimit)
    requestAnimationFrame(animGrow);
  }
function animReduce()
  {
  animTextSizing.style['font-size'] = `${--currentTxtSize}px`;

  if ( currentTxtSize > sizeLimit)
    requestAnimationFrame(animReduce);
  }
* {
  margin  : 0;
  padding : 0;
}
#anim-text-sizing {
  font-size   : 30px;
  width       : fit-content;
  white-space : nowrap;
  background  : #aabfe5;
  }
#hover-me {
  position   : fixed;
  background : red;
  color      : white;
  top        : 50%;
  left       : 50%;
  padding    : .5rem;
  cursor  : zoom-in;
  }
  
Hover me
Welcome to this Page
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!