美文网首页
animejs 简单的scroll滚动demo

animejs 简单的scroll滚动demo

作者: zhao_ran | 来源:发表于2022-07-27 20:45 被阅读0次
<template>
  <div>
    <div class="anime">
      <div
        class="box"
        :id="'_' + item"
        @click="handelClick(item)"
        v-for="item in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]"
      >
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
import anime from "animejs/lib/anime.es.js";
export default {
  methods: {
    scrollTo(selector, offset, cb) {
      var demosEl = document.querySelector(".anime");
      var offset = offset || 0;
      var el = document.querySelector(selector);
      anime({
        targets: { scroll: demosEl.scrollTop },
        scroll: el.offsetTop - offset,
        duration: 500,
        easing: "easeInOutQuart",
        update: function (a) {
          demosEl.scrollTop = a.animations[0].currentValue;
        },
        complete: function () {
          if (cb) cb();
        },
      });
    },
    handelClick(item) {
      this.scrollTo("#_" + item, 0, () => {});
    },
  },
};
</script>

<style>
.anime {
  width: 200px;
  background: pink;
  overflow-y: scroll;
  height: 100vh;
  /* overflow-y: scroll; */
}
.box {
  height: 200px;
  border: 1px solid #eee;
}
</style>

相关文章

网友评论

      本文标题:animejs 简单的scroll滚动demo

      本文链接:https://www.haomeiwen.com/subject/llxvvrtx.html