Mobile adaptation Sprite background positioning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.