js+css实现滚动曲线动画效果代码

代码语言:html

所属分类:加载滚动

代码描述:js+css实现滚动曲线动画效果代码

代码标签: js css 滚动 曲线 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">

  
  
  
<style>
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --white: #fff;
  --gray: #31302f;
  --black: #000000;
  --blue-light: #6caef7;
  --blue-dark: #446482;
}

body {
  display: flex;
  justify-content: center;

  color: var(--white);
  background: var(--black);
}

main {
  width: 100%;
  max-width: 1000px;

  display: grid;
  grid-template-columns: 1fr 160px 2fr;
  grid-template-rows: repeat(6, 400px);
}

svg {
  width: 100%;
  height: 100%;

  grid-column: 2;
  grid-row: 1 / -1;
}

.gridSquare {
  display: flex;
  align-items: center;
}

.col1 {
  grid-column: 1;
  justify-content: flex-end;
}

.col3 {
  grid-column: 3;
}

#bottomStraightLine {
  stroke: var(--gray);
}

#topStraightLine {
  /* why gradient can't be used for stroke: https://stackoverflow.com/questions/21638169/svg-line-with-gradient-stroke-wont-display-if-vertical-or-horizontal */
  stroke: var(--blue-dark);
  stroke-width: 2px;
}

#curveLine {
  stroke-width: 2px;
  fill: none;
}

#topStraightLineHead {
  stroke: var(--blue-light);
  stroke-width: 2px;
}
</style>

  
</head>


  <body>
  <main>
    <div class="gridSquare col1"></div>
    <div class="gridSquare col3"></div>
    <div class="gridSquare col1"></div>
    <div class="gridSquare col3 target">target</div>
    <div class="gridSquare col1"></div>
    <div class="gridSquare col3 target">target</div>
    <div class="gridSquare col1 target">target</div>
    <div class="gridSquare col3"></div>
    <div class="gridSquare col1"></div>
    <div class="gridSquare col3"></div>
    <div class="gridSquare col1"></div>
    <div class="gridSquare col3"></div>
  </main>

  
      <script >
const config = {
  // fraction of window height
  bodyClipPathHeightFraction: 0.6,
  headClipPathHeight: 64 };


const elems = {
  main: document.querySelector("main"),
  targets: document.querySelectorAll(".target") };


const ids = {
  gradient: "myGradient",
  bottomStraightLine: "bottomStraightLine",
  topStraightLine: "topStraightLine",
  curveLine: "curveLine",
  topStraightLineHead: "topStraightLineHead",
  clipPathBody: "clipPathBody",
  clipPathBodyRect: "clipPathBodyRect",
  clipPathHead: "clipPathHead",
  clipPathHeadRect: "clipPathHeadRect" };


let headClipStartY;

function setY(elem, y) {
  elem.setAttribute("y", y);
}

function moveBodyRect(scrollY) {
  setY(elems.clipPathBodyRect, scrollY);
}

function moveHeadRect(scrollY) {
  const newY = headClipStartY + scrollY;

  setY(elems.clipPathHeadRect, newY);
}

function rAFThrottle(callback) {
  let requestID;

  return function (...args) {
    const context = this;

    cancelAnimationFrame(requestID);

    requestID = requestAnimationFrame(() => {
      callback.call(context, ...args);
    });
  };
}

function getCurveCoords(x, y, isLeft) {
  if (isLeft) {
    return `
        L ${x} ${y - 120}
        c 0 60 -60 60 -60 120
        c 0 60 60 60 60 120
    `;
  }

  return `
        L ${x} ${y - 120}
        c 0 60 60 60 60 120
        c 0 60 -60 60 -60 120
    `;
}

function createCurveLine(x, height, targetsData) {
  const path = `
    <path
        id="${ids.curveLine}"
        d="
            M ${x} 0 
            ${targetsData.
  map(({ y, isLeft }) => getCurveCoords(x, y, isLeft)).
  join(" ")}
            L ${x} .........完整代码请登录后点击上方下载按钮下载查看

网友评论0