Jinuss's blog Jinuss's blog
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《Git》
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • 学习
  • 实用技巧
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

东流

前端可视化
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript高级程序设计》
    • 《Vue》
    • 《React》
    • 《Git》
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • 学习
  • 实用技巧
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • HTML

  • CSS

    • CSS教程和技巧收藏
    • flex布局语法
    • flex布局案例-基础
    • flex布局案例-骰子
    • flex布局案例-圣杯布局
    • flex布局案例-网格布局
    • flex布局案例-输入框布局
    • CSS3之transition过渡
    • CSS3之animation动画
    • CSS自定义设置滚动条样式
    • 「布局技巧」图片未加载前自动撑开元素高度
    • 文字在一行或多行时超出显示省略号
    • 从box-sizing属性入手,了解盒子模型
    • 水平垂直居中的几种方式-案例
    • 如何根据系统主题自动响应CSS深色模式
    • CSS-function汇总
    • 网格布局中的动画
    • CSS属性background-position-y实现动画
    • CSS实现github右上角图标
    • CSS3实现购物车动画效果
    • css-functions-属性函数
    • css-functions-图形函数
    • CSS3换装达人原理
    • iconfont图标字体库介绍
    • css关键字currentColor
    • css伪类选择器系列一
    • css伪类选择器系列二
    • 动态修改iconfont图标配色
    • 使用CSS3实现loading效果
    • CSS滚动驱动动画
    • CSS实现图片3D立体效果
    • iconfont中svg图片颜色动态修改第二弹
  • 浏览器

  • 页面
  • CSS
东流
2024-05-16

CSS属性background-position-y实现动画

background-position-y属性用于设置初始状态时背景图片在垂直方向的位置,这个位置相对于通过background-origin定义的背景层原点进行定位,详见MDN 文档 (opens new window)。

今天要将的是如何利用background-position-y属性实现简单的动画。

效果如下

class animate {
  constructor(dom) {
    this.element = dom;
    this.timer_over = null;
    this.timer_leave = null;
    this.period = 30;
    this.step = 128;
    this.imgLength = -3228;
    this.init();
  }
  init() {
    this.element.addEventListener("mouseover", () => {
      this.over();
    });

    this.element.addEventListener("mouseleave", () => {
      this.leave();
    });
  }
  over() {
    if (this.timer_leave) {
      clearTimeout(this.timer_leave);
    }

    this.timer_over = setInterval(
      ((step) => {
        const positionYValue = this.getPositionY();
        if (positionYValue > this.imgLength) {
          this.element.style.backgroundPositionY = `${
            positionYValue - this.step
          }px`;
        }
      }).bind(this),
      this.period
    );
  }
  leave() {
    if (this.timer_over) {
      clearTimeout(this.timer_over);
    }

    this.timer_leave = setInterval(
      (() => {
        const positionYValue = this.getPositionY();
        if (positionYValue != 0) {
          this.element.style.backgroundPositionY = `${
            positionYValue + this.step
          }px`;
        }
      }).bind(this),
      this.period
    );
  }
  getPositionY() {
    const styles = window.getComputedStyle(this.element);
    const backgroundPositionY = styles.getPropertyValue(
      "background-position-y"
    );
    const positionYValue = parseFloat(backgroundPositionY);
    return positionYValue;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

示例中定义了一个animate类,通过addEventListener监听鼠标的mousemove和mouseleave,从而改变background-position-y的值;每项的背景图都是一个长图,长度为 128px * n, 步长step设为 128px,从视觉效果上就形成了动画

编辑 (opens new window)
上次更新: 2024/05/24, 03:31:26
网格布局中的动画
CSS实现github右上角图标

← 网格布局中的动画 CSS实现github右上角图标→

最近更新
01
GeoJSON
05-08
02
Circle
04-15
03
CircleMarker
04-15
更多文章>
Theme by Vdoing | Copyright © 2024-2025 东流 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式