项目作者: alexcambose

项目描述 :
Small library for fitting text perfectly into it's container.
高级语言: JavaScript
项目地址: git://github.com/alexcambose/fit-fontsize.git
创建时间: 2018-04-14T20:22:31Z
项目社区:https://github.com/alexcambose/fit-fontsize

开源协议:MIT License

下载


fit-fontsize

Small library for fitting text perfectly into it’s container.

Demo

Install

Install using npm

  1. npm install -S fit-fontsize

Install using bower

  1. bower install fit-fontsize

Usage

Html only

  1. <div style="height: 200px; width: 200px; border: 1px solid black;">
  2. <span fit-fontsize>Lorem ipsum dolor sit amet</span>
  3. </div>

Css like properties can be added to the attribute

  1. <div style="height: 200px; width: 200px; border: 1px solid black;">
  2. <span fit-fontsize="type: height; ifParentBigger: false">Lorem ipsum dolor sit amet</span>
  3. </div>

Javascript

  1. <div style="height: 200px; width: 200px; border: 1px solid black;">
  2. <span id="element">Lorem ipsum dolor sit amet</span>
  3. </div>
  4. <script>
  5. fit.apply(document.getElementById('element'));
  6. </script>

Fit only with the width of the parent and also watch for changes.

  1. <div style="height: 200px; width: 200px; border: 1px solid black;">
  2. <span id="element">Lorem ipsum dolor sit amet</span>
  3. </div>
  4. <script>
  5. fit.watch(document.getElementById('element'), { type: 'width' });
  6. </script>

Each time #element changes it will recalculate it’s font-size in order to fit.

API

Fit

Kind: global class

new Fit(attribute)

Param Type Default Description
attribute string ‘fit-fontsize’ Attribute that will be used to select the element that sould be fitted

fit.apply(element, options)

Resize

Kind: instance method of Fit

Param Type Default Description
element object The DOM element that needs the font to be resized so that it will fit it’s container
options object Resizing options
[options.type] ‘both’ \ ‘width’ \ ‘height’ ‘both’ Type of the resize.
[options.ifParentBigger] boolean true Resize text bigger until it fits perfectly
[options.ifParentSmaller] boolean true Resize text smaller until it fits perfectly

Example

  1. fit.apply(document.getElementById('elem'), { type: 'width' });

fit.watch(element, options) ⇒ number

Calls apply each time the element changes

Kind: instance method of Fit
Returns: number - Id of the watcher
See: #apply

Param Type Description
element object Same as the apply method
options object Same as the apply method

Example

  1. fit.watch(document.getElementById('elem'), { type: 'height' }); // 0

fit.stop(index)

Stops a watcher

Kind: instance method of Fit

Param Type Description
index number Id of the watcher

Example

  1. fit.stop(0);