图片动画无效! Vue.js

时间:2019-03-25 11:13:58

标签: javascript animation vue.js setinterval

问题是,当我在此顶部:${this.imgTop++}px 上使用设定的时间间隔!

const vue = require("@/assets/images/vue.png");
const bootstrap = require("@/assets/images/bootstrap.png");
const bulma = require("@/assets/images/bulma.png");

export default {
  name: "randImg",
  data() {
    return {
      images: [
        vue,
        bootstrap,
        bulma
      ],
      addedImage: [],
      imgTop: -100,
      imgLeft: -100,
      imgHeight: 64,
      imgWidth: 64,
      changeInterval: 750,
      selectedImage: ''
    }
  },
  created() {
    const randomImg = func => setInterval(func, this.changeInterval);
    randomImg(this.randomImage);
    randomImg(this.addImage);
    randomImg(this.randomPosition);
    setInterval(this.moveImage,50);
  },
  methods: {
    randomImage() {
      const idx = Math.floor(Math.random() * this.images.length);
      this.selectedImage = this.images[idx];
    },
    randomPosition() {
      const randomPos = twoSizes => Math.round(Math.random() * twoSizes);
      this.imgTop = randomPos(window.innerHeight - this.imgHeight);
      this.imgLeft = randomPos(window.innerWidth - this.imgWidth);
    },
    moveRandomImage() {
      const randomImg = func => setInterval(func, this.changeInterval);
      randomImg(this.moveImage);
      this.randomImage();
    },
    addImage() {
      this.addedImage.push({
        style: {
          top: `${this.imgTop}px`,
          left: `${this.imgLeft}px`,
          height: `${this.imgHeight}px`,
          width: `${this.imgWidth}px`
        },
        src: this.selectedImage
      });
    },
    moveImage() {
      this.addedImage.style = {
        style: {
          top: `${this.imgTop++}px`
        }
      }
    }
  }
}

不明白为什么,但是这种动画无效 基本上我会解释它是如何工作的: 我每秒都要从 addedImage:[] 添加具有新位置的新图像,并且我希望每张照片都下来(top ++)

此外,这是一个模板:

<div class="randImg">
    <img class="image" :style="image.style"
         :src="image.src"
         v-for="image in addedImage">
</div>

2 个答案:

答案 0 :(得分:0)

我猜想,可以通过在addedImage数组上添加观察者来实现所需的功能,就像每次添加新的图像元素时,都可以更改其style.top

但是您不应该使用嵌套的setIntervals。我与您this answer分享,解释原因。


我认为一种更简单,更轻便的解决方案是使用CSS,例如创建@keyframe转换规则:

.drop {
  animation: dropDown 1.5s ease-in forwards;
}

@for $i from 1 to 10 {
  .drop:nth-child(#{$i}) {
    animation-delay: $i * 1.5s;
    animation-duration: -1.5s;
  }
}

@keyframes dropDown {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(600px);
    opacity: 0;
  }
}

这里是live example

答案 1 :(得分:0)

r = requests.get(url, headers=headers) r2 = r.json() seq = json.dumps(r2) print(seq['remotecontrol_id']) 是图像的数组,而不是图像。因此,当您执行this.addedImage时,会向数组中添加this.addedImage.style属性,而不是向数组中的图像添加属性。

将moveImage更改为:

style