网格地平线

布局区块HTML/CSS已复制 0 次

八十年代蒸汽波风格的整屏背景:一颗落日压在透视网格的地平线上,网格向观者推进,扫描线可选。太阳半径、黑线粗细、地平线高度、网格密度、推进周期都可调;黑线与扫描线默认都为 0,即干净的现代版,拉起来才回到复古质感。网格主色与冷色点缀各自独立取色。鼠标移动时整个场景轻微转头、太阳反向位移。给的是去掉调参台的独立版:一整页 HTML/CSS,参数集中在脚本开头的 CONFIG。

组件预览改右侧参数,这里和下方代码同步更新

组件演示加载中

演示在 iframe 里独立运行(1280×720 虚拟视口,等比缩放 40%)。滚动类交互要在框内滚动才看得到。

复制代码

代码跟着右侧参数走:你调的每个值都已经写进代码

<div class="scene" id="scene">
  <div class="sky"></div>
  <div class="sun" id="sun"></div>
  <div class="horizon" id="hzline"></div>
  <div class="ground-fill"></div>
  <div class="refl"></div>
  <div class="floor" id="floor"></div>
  <div class="skyglow"></div>
  <div class="scan" id="scan"></div>
  <div class="grain"></div>
  <div class="vignette"></div>
  <div class="content" id="content">
    <div class="eyebrow">Background · No.84</div>
    <h1 id="heading">网格地平线</h1>
    <p class="lede" id="lede">太阳落进网格里,时间开始往前走。</p>
    <div class="actions">
      <a class="btn btn-solid" href="#start">开始使用</a>
      <a class="btn btn-ghost" href="#more">看实现</a>
    </div>
  </div>
</div>
<div class="credit" id="credit"><a href="https://uibits.cn" target="_blank" rel="noopener"><span class="mk">UI Bits</span><span class="hair"></span><span>uibits.cn</span><span>↗</span></a><span>组件来自 UI Bits 组件库</span></div>

<script>
/* 参数集中在这里,改数字 / 颜色即生效 */
var CONFIG={
  sun:210,
  sunLine:0,
  hz:58,
  pink:'#FF2E88',
  cyan:'#22E8FF',
  grid:64,
  speed:7,
  scan:0,
  mouse:true,
  content:true,
  credit:true,
  heading:'网格地平线',
  lede:'太阳落进网格里,时间开始往前走。'
};
var S=Object.assign({},CONFIG);
var el=function(id){return document.getElementById(id)};
var cs=document.documentElement.style;
var paused=false;
/* 指针:整个场景做极轻的转头,太阳反向位移一点,做出前后层次 */
var tmx=innerWidth*.5,tmy=innerHeight*.5,mx=tmx,my=tmy,scene=el('scene'),sun=el('sun');
window.addEventListener('mousemove',function(e){tmx=e.clientX;tmy=e.clientY});
window.addEventListener('mouseleave',function(){tmx=innerWidth*.5;tmy=innerHeight*.5});
function follow(){
  mx+=(tmx-mx)*.05; my+=(tmy-my)*.05;
  if(S.mouse){
    var px=(mx/innerWidth-.5)*2, py=(my/innerHeight-.5)*2;
    scene.style.transform='perspective(1600px) rotateY('+(px*3.4).toFixed(2)+'deg) rotateX('+(-py*2.2).toFixed(2)+'deg) scale(1.1)';
    sun.style.transform='translate(-50%,-50%) translate3d('+(px*-34).toFixed(1)+'px,'+(py*-20).toFixed(1)+'px,0)';
  }
  requestAnimationFrame(follow);
}
follow();

function apply(){
  cs.setProperty('--sun',S.sun+'px');
  cs.setProperty('--hz',S.hz);
  cs.setProperty('--pink',S.pink);
  cs.setProperty('--cyan',S.cyan);
  var h=String(S.cyan).replace('#','');
  if(h.length===3)h=h[0]+h[0]+h[1]+h[1]+h[2]+h[2];
  var ci=parseInt(h,16);
  if(!isNaN(ci))cs.setProperty('--cyan-rgb',((ci>>16)&255)+','+((ci>>8)&255)+','+(ci&255));
  cs.setProperty('--gx',S.grid+'px');
  cs.setProperty('--gy',Math.round(S.grid*.68)+'px');
  cs.setProperty('--sp',S.speed);
  cs.setProperty('--sunLine',(+S.sunLine)+'px');
  sun.classList.toggle('noSunLine',+S.sunLine===0);
  cs.setProperty('--scan-a',(+S.scan/100*.62).toFixed(3));
  el('content').style.display=S.content?'flex':'none';
  el('credit').style.display=S.credit?'flex':'none';
  el('heading').textContent=S.heading;
  el('lede').textContent=S.lede;
}

apply();
if(matchMedia('(prefers-reduced-motion:reduce)').matches)document.body.classList.add('paused');
</script>

速览

类别
布局
层级
区块
框架
HTML/CSS
复制
0 次

安装

复制「HTML/CSS」与「样式 CSS」两份代码即可,无第三方依赖;可调项集中在脚本开头的 CONFIG。

能力

  • 支持深色
  • 带动效
  • 响应式

自定义参数

9 项默认

其它参数

尚未归类的可调项
210
0
58
64
7
0

使用提示

  1. 先在上方调参数看效果 —— 下方「复制代码」 会跟着你调的值一起变,复制走的就是眼前这一版。
  2. 「HTML/CSS」是结构与文案,「样式 CSS」是它用到的规则(含变量与响应式, 只挑了这个组件用到的部分)。两份一起贴。
  3. 代码里的素材是本站绝对地址,换成自己的图只需改 src。

相似组件

按标签重合度推荐,不是运营手配的榜单。

色涡 截图

色涡

布局 · 区块

墨蚀 截图

墨蚀

布局 · 区块

极光帷幔 截图

极光帷幔

布局 · 区块

流光绸缎 截图

流光绸缎

布局 · 区块