首页 > 文章列表 > CSS鼠标跟随的原理

CSS鼠标跟随的原理

css
407 2022-08-06

说明

1、鼠标跟随就是元素会跟随着鼠标的移动,而作出相对应的运动。

2、为了能够监控当前鼠标的位置,我们只需在页面上铺上元素即可。

实例

<div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  ... // 100个
</div>
.g-container {
    position: relative;
    width: 100vw;
    height: 100vh;
}
 
.position {
    position: absolute;
    width: 10vw;
    height: 10vh;
}
 
@for $i from 0 through 100 {
    
    $x: $i % 10;
    $y: ($i - $x) / 10;
    
    .position:nth-child(#{$i + 1}) {
        top: #{$y * 10}vh;
        left: #{$x * 10}vw;
    }
 
    .position:nth-child(#{$i + 1}):hover {
        background: rgba(255, 155, 10, .5)
    }
}

本文教程操作环境:windows7系统、css3版,DELL G3电脑。