• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

CSS3、原生JS、Vue3.0技术各自实现序列帧动画效果

武飞扬头像
帅龍之龍
帮助1

还记得序列帧技术在2018年的时候很火的,那时做H5很吃香的...

那么在此记录一下用纯CSS3、原生JS、Vue3.0,这3种方式来设计几个例子。需要自己去找一下雪碧图哦,比如阿里云、腾讯云官网有很多雪碧图的~

1、使用纯CSS3方式实现

  1.  
    <template>
  2.  
    <div class="machine">
  3.  
    <div class="machine-box" :class="sign ? 'machine-running' : ''" @click="handleMachineRunningClick">
  4.  
    </div>
  5.  
    </div>
  6.  
    </template>
  7.  
     
  8.  
    <script>
  9.  
    import { ref } from 'vue';
  10.  
     
  11.  
    export default {
  12.  
    name: 'machineComponent',
  13.  
    setup() {
  14.  
     
  15.  
    let sign = ref(false);
  16.  
     
  17.  
    function handleMachineRunningClick() {
  18.  
    sign.value = true;
  19.  
    }
  20.  
     
  21.  
    return {sign, handleMachineRunningClick}
  22.  
    }
  23.  
    }
  24.  
    </script>
  25.  
     
  26.  
    <style scoped>
  27.  
    .machine {
  28.  
    width: 250px;
  29.  
    height: 250px;
  30.  
    margin: 100px auto 0 auto;
  31.  
    border-radius: 250px;
  32.  
    box-shadow: 0px 0px 5px #888;
  33.  
    }
  34.  
     
  35.  
    .machine .machine-box {
  36.  
    width: 64px;
  37.  
    height: 64px;
  38.  
    margin: 0 auto;
  39.  
    cursor: pointer;
  40.  
    background-position: 0 0;
  41.  
    background-size: 100%;
  42.  
    transform: translateY(93px);
  43.  
    background-image: url('./machine.png');
  44.  
    }
  45.  
     
  46.  
    .machine .machine-running {
  47.  
    animation: startingRun 1s steps(1, start) infinite;
  48.  
    }
  49.  
    </style>
  50.  
     
  51.  
    <style>
  52.  
    @keyframes startingRun {
  53.  
    0% {
  54.  
    background-position: 0 64px;
  55.  
    }
  56.  
    5% {
  57.  
    background-position: 0 128px;
  58.  
    }
  59.  
    10% {
  60.  
    background-position: 0 192px;
  61.  
    }
  62.  
    15% {
  63.  
    background-position: 0 256px;
  64.  
    }
  65.  
    20% {
  66.  
    background-position: 0 320px;
  67.  
    }
  68.  
    25% {
  69.  
    background-position: 0 384px;
  70.  
    }
  71.  
    30% {
  72.  
    background-position: 0 448px;
  73.  
    }
  74.  
    35% {
  75.  
    background-position: 0 512px;
  76.  
    }
  77.  
    40% {
  78.  
    background-position: 0 576px;
  79.  
    }
  80.  
    45% {
  81.  
    background-position: 0 640px;
  82.  
    }
  83.  
    50% {
  84.  
    background-position: 0 704px;
  85.  
    }
  86.  
    55% {
  87.  
    background-position: 0 768px;
  88.  
    }
  89.  
    60% {
  90.  
    background-position: 0 768px;
  91.  
    }
  92.  
    65% {
  93.  
    background-position: 0 832px;
  94.  
    }
  95.  
    70% {
  96.  
    background-position: 0 896px;
  97.  
    }
  98.  
    75% {
  99.  
    background-position: 0 960px;
  100.  
    }
  101.  
    80% {
  102.  
    background-position: 0 1024px;
  103.  
    }
  104.  
    85% {
  105.  
    background-position: 0 1088px;
  106.  
    }
  107.  
    90% {
  108.  
    background-position: 0 1152px;
  109.  
    }
  110.  
    95% {
  111.  
    background-position: 0 1216px;
  112.  
    }
  113.  
    100%{
  114.  
    background-position: 0 1280px;
  115.  
    }
  116.  
    }
  117.  
    </style>
学新通

效果:~

学新通

2、使用原生JS方式实现(在阿里云官网右键JS源码找到的^^)

  1.  
    <!DOCTYPE html>
  2.  
    <html>
  3.  
    <head>
  4.  
    <title>使用原生JS方式实现序列帧动画</title>
  5.  
    <style type="text/css">
  6.  
    .machine {
  7.  
    width: 250px;
  8.  
    height: 250px;
  9.  
    margin: 100px auto 0 auto;
  10.  
    border-radius: 250px;
  11.  
    box-shadow: 0px 0px 5px #888;
  12.  
    }
  13.  
     
  14.  
    .machine .machine-box {
  15.  
    width: 64px;
  16.  
    height: 64px;
  17.  
    margin: 0 auto;
  18.  
    cursor: pointer;
  19.  
    background-position: 0 0;
  20.  
    background-size: 100%;
  21.  
    transform: translateY(93px);
  22.  
    background-image: url('./player.png');
  23.  
    }
  24.  
    </style>
  25.  
    </head>
  26.  
     
  27.  
    <body>
  28.  
    <div class="machine">
  29.  
    <div class="machine-box"></div>
  30.  
    </div>
  31.  
    </body>
  32.  
     
  33.  
    <script type="text/javascript">
  34.  
     
  35.  
    let count = 0;
  36.  
    let enterTimer;
  37.  
    let leaveTimer;
  38.  
     
  39.  
    var o = document.getElementsByClassName("machine-box")[0];
  40.  
     
  41.  
    o.onmouSEO((Search Engine Optimization))ver = function() {
  42.  
    clearInterval(leaveTimer),
  43.  
    enterTimer = setInterval(function() {
  44.  
    if (count < 20 && 64 * count < 1344) {
  45.  
    count ;
  46.  
    document.getElementsByClassName("machine-box")[0].style.backgroundPositionY = -64 * count "px";
  47.  
    } else {
  48.  
    clearInterval(enterTimer)
  49.  
    }
  50.  
    }, 30);
  51.  
    };
  52.  
     
  53.  
    o.onmouSEO((Search Engine Optimization))ut = function() {
  54.  
    clearInterval(enterTimer),
  55.  
    leaveTimer = setInterval(function() {
  56.  
    if (count > 0 && 64 * count > 0) {
  57.  
    count--;
  58.  
    document.getElementsByClassName("machine-box")[0].style.backgroundPositionY = -64 * count "px"
  59.  
    } else {
  60.  
    clearInterval(leaveTimer)
  61.  
    }
  62.  
    }, 30);
  63.  
    };
  64.  
    </script>
  65.  
    </html>
学新通

效果:~

学新通

3、使用Vue3.0方式实现

  1.  
    <template>
  2.  
    <div class="machine">
  3.  
    <span>{{ offsetHeight }}</span>
  4.  
    <div
  5.  
    class="machine-box"
  6.  
    :style="'background-position: 0 ' offsetHeight 'px'"
  7.  
    @mouseenter.stop.prevent="handleMouseEnter"
  8.  
    @mouseleave.stop.prevent="handleMouseLeave">
  9.  
    </div>
  10.  
    </div>
  11.  
    </template>
  12.  
     
  13.  
    <script>
  14.  
    import { ref } from 'vue';
  15.  
     
  16.  
    export default {
  17.  
    name: 'machineComponent',
  18.  
    setup() {
  19.  
    // 响应式设置偏移高度初始化为0
  20.  
    let offsetHeight = ref(0);
  21.  
     
  22.  
    // 运算符初始化为加号,因为开始时的偏移高度为0,只能增加
  23.  
    let operator = 'add';
  24.  
     
  25.  
    // 鼠标进入事件定时器
  26.  
    let enterTimer;
  27.  
     
  28.  
    // 鼠标离开事件定时器
  29.  
    let leaveTimer;
  30.  
     
  31.  
    /**
  32.  
    * 增加背景图片的偏移高度
  33.  
    */
  34.  
    function loopComputeOffsetHeight() {
  35.  
    if (operator == 'add') {
  36.  
    offsetHeight.value = 64;
  37.  
    if (offsetHeight.value >= 1344) {
  38.  
    operator = 'reduce';
  39.  
    }
  40.  
    } else {
  41.  
    if (offsetHeight.value > 0) {
  42.  
    offsetHeight.value -= 64;
  43.  
    if (offsetHeight.value <= 0) {
  44.  
    operator = 'add';
  45.  
    }
  46.  
    }
  47.  
    }
  48.  
    console.log(operator, offsetHeight.value);
  49.  
    }
  50.  
     
  51.  
    /**
  52.  
    * 减少背景图片的偏移高度
  53.  
    */
  54.  
    function reduceOffsetHeight() {
  55.  
    if (offsetHeight.value > 0) {
  56.  
    offsetHeight.value -= 64;
  57.  
    if (offsetHeight.value <= 0) {
  58.  
    clearInterval(leaveTimer)
  59.  
    operator = 'add';
  60.  
    }
  61.  
    console.log(offsetHeight.value);
  62.  
    }
  63.  
    }
  64.  
     
  65.  
    /**
  66.  
    * 鼠标进入事件
  67.  
    */
  68.  
    function handleMouseEnter() {
  69.  
    clearInterval(leaveTimer);
  70.  
    enterTimer = setInterval(loopComputeOffsetHeight, 50);
  71.  
    }
  72.  
     
  73.  
    /**
  74.  
    * 鼠标离开事件
  75.  
    */
  76.  
    function handleMouseLeave() {
  77.  
    clearInterval(enterTimer);
  78.  
    leaveTimer = setInterval(reduceOffsetHeight, 50);
  79.  
    }
  80.  
     
  81.  
    return {offsetHeight, handleMouseEnter, handleMouseLeave}
  82.  
    }
  83.  
    }
  84.  
    </script>
  85.  
     
  86.  
    <style scoped>
  87.  
    .machine {
  88.  
    width: 250px;
  89.  
    height: 250px;
  90.  
    position: relative;
  91.  
    text-align: center;
  92.  
    margin: 100px auto 0 auto;
  93.  
    border-radius: 250px;
  94.  
    box-shadow: 0px 0px 5px #ddd;
  95.  
    }
  96.  
     
  97.  
    .machine span {
  98.  
    position: absolute;
  99.  
    position: absolute;
  100.  
    margin: auto;
  101.  
    left: 0;
  102.  
    right: 0;
  103.  
    top: 50px;
  104.  
    bottom: auto;
  105.  
    }
  106.  
     
  107.  
    .machine .machine-box {
  108.  
    width: 64px;
  109.  
    height: 64px;
  110.  
    margin: 0 auto;
  111.  
    background-position: 0 0;
  112.  
    background-size: 100%;
  113.  
    transform: translateY(93px);
  114.  
    background-image: url('./machine.png');
  115.  
    cursor: pointer;
  116.  
    }
  117.  
    </style>
学新通

效果:~

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgeheca
系列文章
更多 icon
同类精品
更多 icon
继续加载