Mouse Parallax 鼠标视差跟随,视觉差

网页中鼠标移动,元素或背景图片轻微移动特效

这种效果通常称为 Mouse Parallax(鼠标视差跟随),常用于 Banner、产品展示页、高端企业官网。

方案1:背景图片轻微移动(推荐)

HTML:

复制代码
复制代码
<div class="banner">
    <img src="banner.jpg" alt="">
</div>

CSS:

复制代码
复制代码
.banner{
    width:100%;
    height:600px;
    overflow:hidden;
    position:relative;
}

.banner img{
    width:110%;
    height:110%;
    object-fit:cover;
    position:absolute;
    left:50%;
    top:50%;
    transform:translate(-50%,-50%);
    transition:transform .15s ease-out;
}

JS:

复制代码
复制代码
const banner = document.querySelector('.banner');
const img = banner.querySelector('img');

banner.addEventListener('mousemove', function(e){

    const x = (e.clientX / window.innerWidth - 0.5) * 20;
    const y = (e.clientY / window.innerHeight - 0.5) * 20;

    img.style.transform =
        `translate(calc(-50% + ${x}px), calc(-50% + ${y}px))`;

});

效果:

  • 鼠标向右 → 图片轻微向右
  • 鼠标向左 → 图片轻微向左
  • 最大移动约 20px
  • 非常适合 Banner

方案2:元素跟随鼠标

HTML:

复制代码
复制代码
<div class="banner">
    <div class="floating"></div>
</div>

CSS:

复制代码
复制代码
.banner{
    height:600px;
    position:relative;
}

.floating{
    width:300px;
    height:300px;
    background:#0a84ff;
    border-radius:20px;

    position:absolute;
    left:50%;
    top:50%;

    transform:translate(-50%,-50%);
    transition:transform .2s ease-out;
}

JS:

复制代码
复制代码
const floating = document.querySelector('.floating');

document.addEventListener('mousemove', function(e){

    const x = (e.clientX - window.innerWidth/2) / 40;
    const y = (e.clientY - window.innerHeight/2) / 40;

    floating.style.transform =
        `translate(calc(-50% + ${x}px), calc(-50% + ${y}px))`;

});

方案3:多层视差(高级官网常用)

HTML:

复制代码
复制代码
<div class="banner">
    <img class="layer layer1" src="bg.png">
    <img class="layer layer2" src="machine.png">
    <img class="layer layer3" src="bottle.png">
</div>

JS:

复制代码
复制代码
const layers = document.querySelectorAll('.layer');

document.addEventListener('mousemove', e => {

    let x = e.clientX / window.innerWidth - 0.5;
    let y = e.clientY / window.innerHeight - 0.5;

    layers.forEach((layer,index)=>{

        let speed = (index + 1) * 10;

        layer.style.transform =
            `translate(${x * speed}px, ${y * speed}px)`;

    });

});

效果:

  • 背景层移动最慢
  • 产品层移动中等
  • 前景装饰层移动最快

类似:

4


方案4:性能最佳(推荐用于企业官网)

使用 requestAnimationFrame,动画更丝滑:

复制代码
复制代码
const banner = document.querySelector('.banner');

let mouseX = 0;
let mouseY = 0;

let currentX = 0;
let currentY = 0;

banner.addEventListener('mousemove', e => {

    mouseX = (e.clientX - window.innerWidth/2) * 0.02;
    mouseY = (e.clientY - window.innerHeight/2) * 0.02;

});

function animate(){

    currentX += (mouseX - currentX) * 0.08;
    currentY += (mouseY - currentY) * 0.08;

    banner.style.transform =
        `translate(${currentX}px, ${currentY}px)`;

    requestAnimationFrame(animate);

}

animate();

优点:

  • 不会频繁触发重排
  • CPU占用低
  • 比 mousemove + transition 更流畅

形成轻微 3D 视差效果,既高级又不会影响性能。

相关推荐
梅梅9666 天前
一款mipi转lvds的lcd调试(LT8912B)
单片机·计算机外设·电脑·显示器·pd芯片
归零鸟14 天前
笔记本内屏黑屏,系统却说 “显示器一切正常“:一次 Intel PSR2 导致的 eDP 排查实录
计算机外设·电脑
Htr_14 天前
Cortex 使用指南:开源 API 知识层
前端·数据库·人工智能·重构·计算机外设
Htr_15 天前
Loqua 使用指南:让思维不再因键盘而减速
计算机外设
legendary_16318 天前
PD‑SINK芯片在无协议后端负载中的工程应用
c语言·开发语言·人工智能·智能手机·计算机外设
whysoft18 天前
创意新用法-把老磁带转录成mp3-第2部分
linux·计算机外设
yyt36304584120 天前
一次鼠标交互触发两次 Vue flushJobs:把高频交互从 VDOM 里拆出来
前端·vue.js·计算机外设
励志不掉头发的内向程序员21 天前
【LibreCAD 2D架构】从鼠标点击到图形创建:RS_ActionDrawLine交互流程与状态机解析
开发语言·c++·qt·学习·系统架构·计算机外设·交互
白日做梦Q21 天前
Intel核显笔记本屏幕闪烁、忽明忽暗解决:关闭显示器节能
计算机外设
陈年菠萝包22 天前
计算机组成原理(1计算机基本组成)
计算机外设