zoukankan      html  css  js  c++  java
  • gulp.spritesmith修改px为rem单位

    移动端开发中,使用gulp.spritesmith进行小图sprite并生成样式,但由于spritesmith默认是以px为单位,所以就把插件的内容修改了下让生成rem单位并且能把background-size设置为雪碧图的宽高。

    1.首先修改gulp.spritesmith ode_modulesspritesheet-templateslibspritesheet-templates.js

    ['x', 'y', 'offset_x', 'offset_y', 'height', 'width', 'total_height', 'total_width'].forEach(function (key) {
        if (item[key] !== undefined) {
          px[key] = item[key]/64 + 'rem';
        }
      });

    修改的地方是item[key]/64+'rem';这句,我的是设置了640px宽度,所以这里除以64来转换得到rem值。

    2.修改gulp.spritesmith ode_modulesspritesheet-templateslib emplatescss.template.handlebars

    在模板页中加入生成background-size内容

    {{/block}}
    {{#block "sprites"}}
    .cicon {
        display: inline-block;
        background-size: {{spritesheet.px.width}} {{spritesheet.px.height}};
    }
    {{#each sprites}}
    {{{selector}}} {
      background-image: url({{{escaped_image}}});
      background-position: {{px.offset_x}} {{px.offset_y}};
       {{px.width}};
      height: {{px.height}};
    }
    {{/each}}
    {{/block}}

    主要添加了加粗的代码行。以上两点修改完成就可以把spritesmith生成的px转换成rem,增加background-size主要是因为px单位下图片背景位置跟大小默认就是雪碧图中的大小,所以转换成rem后需要进行修改。

    PS:rem单位下在不同设备中可能出现图片中出现了雪碧图中其他图的边边角角,所以这里需要设置图片合成的时候彼此之间有一定的间隙,这个只要是gulpfile中设置下padding:10即可。

    var spriteData = gulp.src(base_url+'_images/icons/*.+(jpeg|jpg|png)').pipe(spritesmith({
        imgName: 'icons_sprite.png',
        cssName: 'icons_sprite.css',
        cssFormat: 'css',
        padding: 10
      }));
  • 相关阅读:
    jQuery 工具函数
    cdh 5.13 centos6.9安装
    centos 6.9 NTP基准时间服务器配置
    cloudera cdh5.13.0 vmware 快速安装
    centos 7.3+nginx+jira(.bin)+mysql
    zabbix 3.2.6+centos 7 +nginx 1.12+ mysql 5.6+ Grafana +php 5.6
    centos 6.9 +nginx 配置GIT HTTPS服务器(证书采用自签名)
    好难啊 姿态解算 算是解决了
    stm32 iic读取mpu6050失败 改用串口
    stm32 延时函数 delay_ms 范围
  • 原文地址:https://www.cnblogs.com/zhouzone/p/5275197.html
Copyright © 2011-2022 走看看