@livelybone/rem-init
v3.3.0
Published
rem, pixel, viewport, for adapting various interfaces
Maintainers
Readme
@livelybone/rem-init
pkg.module supported, which means that you can apply tree-shaking in you project
rem, pixel, viewport, for adapting various interfaces
Intro
pxwithremconversion depends on the font-size ofhtmltag, when the font-size ofhtmlis625%,1remis equal to100px
You can use
remeverywhere, include font-size.
This solution isn't compatible with ie8
If your client is pc, the function will set
1remto be100px
If your client is mobile, the function will set
1remto be(100 * (window.devicePixelRatio || 1))px
For adapting various interfaces, you can use
vwvhvminvmaxsimultaneously
repository
https://github.com/livelybone/rem-init.git
Demo
http://github.com/livelybone/rem-init#readme
Installation
npm i -S @livelybone/rem-initGlobal name
RemInit
Usage
import RemInit from '@livelybone/rem-init';Use in html, see what your can use in CDN: unpkg
<-- use what you want -->
<script src="https://unpkg.com/@livelybone/rem-init/lib/umd/<--module-->.js"></script>Params
| Name | Type | DefaultValue | Description |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ------------ |
| options | Object<{ pageNoScale: Boolean, pageScalable: Boolean, pageScaleMiddleware: Function, pageScaleMaxFactor: Number }> | { pageNoScale: false, pageScalable: false, pageScaleMiddleware: null, pageScaleMaxFactor: 1 } | |
pageNoScale: When it is equal totrue, the tool will force to set theinitial-scalevalue of viewport to 1
pageScalable: When it is equal totrue, the tool will remove theuser-scalablefield of viewport, which means that you can scale your pages on the range depends onminimum-scaleandmaximum-scale
pageScaleMiddleware:(fontScale: Number, isMobile: Boolean) => pageScaleYou can rewrite the pageScale value which is default to be1 / fontScaleon your own will by this function. It only works whenpageNoScaleis not equal totrue
pageScaleMaxFactor: It only works whenpageScalableis equal totrue. When pageScaleMaxFactor == false, the tool will remove themaximum-scalefield of viewport, otherwise, the function will set themaximum-scalevalue toMath.max(1, +options.pageScaleMaxFactor || 0) * initialScale
Example
// assume
window.devicePixelRatio = 2
window.isMobile = true
// results
RemInit({pageNoScale: true}) // => viewport: width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no
RemInit({pageScalable: true, pageScaleMaxFactor: true}) // => viewport: width=device-width, initial-scale=0.5, minimum-scale=0.5
RemInit({pageScalable: true, pageScaleMaxFactor: 1}) // => viewport: width=device-width, initial-scale=0.5, minimum-scale=0.5, maximum-scale=0.5
RemInit({pageScalable: true, pageScaleMaxFactor: 0.5}) // => viewport: width=device-width, initial-scale=0.5, minimum-scale=0.5, maximum-scale=0.5
RemInit({pageScalable: true, pageScaleMaxFactor: 4}) // => viewport: width=device-width, initial-scale=0.5, minimum-scale=0.5, maximum-scale=2
RemInit({pageScaleMaxFactor: 4}) // => viewport: width=device-width, initial-scale=0.5, minimum-scale=0.5, maximum-scale=0.5, user-scalable=no
RemInit({pageScaleMiddleware: (fontScale, isMobile) => (isMobile ? 1 / fontScale / fontScale : 1)}) // => viewport: width=device-width, initial-scale=0.25, minimum-scale=0.25, maximum-scale=0.25
RemInit({pageScalable: true, pageScaleMaxFactor: 4, pageScaleMiddleware: (fontScale, isMobile) => (isMobile ? 1 / fontScale / fontScale : 1)}) // => viewport: width=device-width, initial-scale=0.25, minimum-scale=0.25, maximum-scale=1Extra
The function will add
isMobilefield towindowifisMobileis not exist inwindow
The function will add
rootSizefield towindow, you can use it to convertremwithpx
var rootSize = {
value: Number, // means: 1rem = [value]px
unit: 'px/rem',
rem2px: Function,
px2rem: Function,
}