代码之家  ›  专栏  ›  技术社区  ›  Zeth

无法使用“红色精灵之星”更改“最后精灵之星”

  •  -1
  • Zeth  · 技术社区  · 5 年前

    我在用 Gulp Spritesmith 产生一个巨大的长精灵,使滚动动画效果。

    我在跟踪 this tutorial . 在本教程中,有一个sass函数,它遍历图像:

    @for $i from 1 through $frame-count {
      .frame#{$i} {
        background-position: -(($i * $offset-val) * 2) + px 50%;
      }
    }
    

    我要做同样的事情,只有垂直,所以:

    @for $i from 1 through $frame-count {
      .frame#{$i} {
        background-position: 0px -(($i * $offset-val) * 2) + px;
      }
    }
    

    但是不管我做什么,我的输出精灵都使用 binary-tree -制作精灵的算法。

    如果我有16张图片,那么精灵看起来是这样的:

    1  2  7  13
    3  4  8  14
    5  6  9  15
    10 11 12 16
    

    我希望他们像:

    1
    2
    3
    4
    5
    6
    7
    ...
    ...   
    

    这是我的核心Gulp Spritesmith文件:

    var gulp = require('gulp');
    
    var spritesmith = require('gulp.spritesmith');
    
    gulp.task('default');
    
    gulp.task('sprite', function () {
      var spriteData = gulp.src('images/*.jpg')
      .pipe(spritesmith({
        imgName: 'sprite.jpg',
        cssName: 'sprite.css',
        algorithm: 'top-down'
      }));
      spriteData.img.pipe(gulp.dest('img'));
      spriteData.css.pipe(gulp.dest('css'));
    });
    

    在过去的一个小时里,我一直在摆弄这三条线:

      .pipe(spritesmith({
        imgName: 'sprite.jpg',
        cssName: 'sprite.css',
      }));
    

    …不管我做什么,然后我不能让输出文件改变(根本!)。甚至连名字都没有,替换 cssName: 'sprite.css', 具有 cssName: 'foobar.css', !

    我错过了什么?

    0 回复  |  直到 5 年前
        1
  •  1
  •   kavinraj    5 年前

    使用 spritesmith gulp.spritesmith

    其中sprite image有4个tile blockm图像,如下图等

    enter image description here

    示例代码:

    GulpFiel.js

    var gulp = require('gulp');
    var spritesmith = require('gulp.spritesmith');
    
    gulp.task('sprite', function () {
      var spriteData = gulp.src('app/images/*.jpg').pipe(spritesmith({
        imgName: 'doodle-sprite.jpg',
        cssName: 'sprite.scss',
        algorithmOpts: {
          sort: false
        },
        algorithm: 'top-down',
      }));
      return spriteData.pipe(gulp.dest('app/images/'));
    });
    

    我对sprite.scss的结果

    @mixin sprite-width($sprite) {
      width: nth($sprite, 5);
    }
    
    @mixin sprite-height($sprite) {
      height: nth($sprite, 6);
    }
    
    @mixin sprite-position($sprite) {
      $sprite-offset-x: nth($sprite, 3);
      $sprite-offset-y: nth($sprite, 4);
      background-position: $sprite-offset-x  $sprite-offset-y;
    }
    
    @mixin sprite-image($sprite) {
      $sprite-image: nth($sprite, 9);
      background-image: url(#{$sprite-image});
    }
    
    @mixin sprite($sprite) {
      @include sprite-image($sprite);
      @include sprite-position($sprite);
      @include sprite-width($sprite);
      @include sprite-height($sprite);
    }
    
    // The `sprites` mixin generates identical output to the CSS template
    //   but can be overridden inside of SCSS
    //
    // @include sprites($spritesheet-sprites);
    @mixin sprites($sprites) {
      @each $sprite in $sprites {
        $sprite-name: nth($sprite, 10);
        .#{$sprite-name} {
          @include sprite($sprite);
        }
      }
    }
    

    出来的图像

    enter image description here