Mobile Adapter General I will use REM to adapt, the approximate operation is in accordance with a certain size design, the final conversion of all PX values to REM, but some mobile phone background sprite (csssprite) will appear some error (1px or so), if the company requires not high pour also does not matter, But I can't bear it.
The method is to replace the REM value with the Background-position percent value (n m), we need the width of the element (W h), the width height of the sprite (K g), we need to display the icon's coordinates (x y), the formula is as follows:
1 n =-X/(W-K) * 100% 2 m =-y/(h-g) * 100%
After processing using the Scss method:
The width of the $spriteWidth sprite px//$spriteHeight The height of the sprite px//$iconWidth need to display the width of the icon px//$iconHeight need to display the height px//of the icon $iconX Icon's original x-coordinate//$iconY icon's original y-coordinate//@mixin bgposition ($spriteWidth, $spriteHeight, $iconWidth, $iconHeight, $iconX, $iconY ) { background-position: (($iconX/($spriteWidth-$iconWidth)) * 100% ($iconY/($spriteHeight-$iconHeight)) * 100% );}. icon2{ Width:0.74rem; Height:0.64rem; @include bgposition (1072, 442, 74, 64, 188, 5);} Further expansion//@mixin Bgpositionsamesprite ($iconWidth, $iconHeight, $iconX, $iconY) {//$spritewidth and $spriteheight are put into methods, This allows only 4 parameters to be used, and also avoids the late modification of the sprite chart size search and replace everywhere $spriteWidth: 1072; $spriteHeight: 442; @include bgposition ($spriteWidth, $spriteHeight, $iconWidth, $iconHeight, $iconX, $iconY); }. icon3{ Width:0.74rem; Height:0.64rem; @include Bgpositionsamesprite (74, 64, 188, 5);}
Mobile adaptation Sprite background positioning