zoukankan      html  css  js  c++  java
  • Vue-cli 2在webpack内使用雪碧图(响应式)

    先执行install

      cnpm install webpack-spritesmith

    文件位置

    buildwebpack.dev.conf.js

    添加内容:

    const SpritesmithPlugin = require('webpack-spritesmith');

    找到  

    文件内  plugins:

    我是生成less文件后,单位rem实现雪碧图的响应式适配,用的是阿里方案页面内需引用flexbile.min.js(参考阿里手淘适配)

    目录结构:

    添加如下内容:

    文件位置:

    buildwebpack.dev.conf.js

    plugins: 内添加如下内容
        new SpritesmithPlugin({
            // 目标小图标
            src: {
                cwd: path.resolve(__dirname, '../static/images/icon'),
                glob: '*.png'
            },
            // 输出雪碧图文件及样式文件
            target: {
                image: path.resolve(__dirname, '../static/images/sprite.png'),
                css:[[path.resolve(__dirname, '../static/images/sprite.less'),{
                        format: 'function_based_template'
                    }]]
            },
            customTemplates: {
                'function_based_template': path.resolve(__dirname, '../my_handlebars_template.handlebars')
            },
            // 样式文件中调用雪碧图地址写法
            apiOptions: {
                cssImageRef: './sprite.png?v='+Date.parse(new Date())
            },
            spritesmithOptions: {
                algorithm: 'binary-tree'
            }
        })

     

    添加文件:my_handlebars_template.handlebars

    文件位置:项目根目录下:my_handlebars_template.handlebars

    文件内容:

    {{#block "sprites"}}
    {{#block "spritesheet"}}
    @img:url('{{{spritesheet.escaped_image}}}');
    @r:75rem;
    .icon{
       background-size: {{spritesheet.width}}/@r {{spritesheet.height}}/@r;
       background-repeat:no-repeat;
       display:inline-block;
    };
    {{/block}}
    {{#each sprites}}
    .icon-{{{strings.name}}} {
      background-image: @img;
      background-position: {{offset_x}}/@r {{offset_y}}/@r;
       {{width}}/@r;
      height: {{height}}/@r
    };
    {{/each}}
    {{/block}}

    命令行内运行:

        npm run dev  即可

    生成这2个文件引用对应的less文件即可

  • 相关阅读:
    hanlp在jdk11 maven8编译后在jdk8 报错
    Linux top命令输出到文件——持续输出某个进程的信息
    maven with depend
    解决mount时发生错误wrong fs type, bad option, bad superblock
    leetcode中 01背包问题相关汇总
    leetcode刷题总结901-950
    Xgboost如何处理缺失值/
    leetcode刷题总结851-900
    leetcode刷题总结801-850
    leetcode刷题总结751-800
  • 原文地址:https://www.cnblogs.com/NB-JDzhou/p/9182639.html
Copyright © 2011-2022 走看看