购物车按钮

2023-11-08

先看效果:
在这里插入图片描述
再看代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>购物车按钮展示</title>
  <link href="https://fonts.googleapis.com/css?family=Inter:400,500,600,700&amp;display=swap" type="text/css" rel="stylesheet">
  <script src="https://unpkg.co/gsap@3/dist/gsap.min.js"></script>
  <script src="https://assets.codepen.io/16327/Physics2DPlugin3.min.js"></script>
  <script src="https://assets.codepen.io/16327/MorphSVGPlugin3.min.js"></script>

  <style>
    .add-to-cart {
      --color: #000;
      --background: #fff;
      --drop: #38E739;
      --background-s: 1;
      --text-o: 1;
      --text-x: 10px;
      --bottle-x: -40%;
      --bottle-y: -60px;
      --bottle-s: .4;
      --bottle-r: 0deg;
      --bottle-o: 0;
      --cart-x: -57px;
      --cart-y: -2px;
      --cart-r: 0deg;
      --cart-s: 0.8;
      --check-y: 0px;
      --check-s: 0;
      --check-offset: 8.5px;
      -webkit-tap-highlight-color: transparent;
      -webkit-appearance: none;
      outline: none;
      background: none;
      border: none;
      padding: 12px 0;
      margin: 0;
      width: 142px;
      color: var(--color);
      cursor: pointer;
      position: relative;
      text-align: center;
      font: inherit;
    }
    .add-to-cart.clipped {
      -webkit-clip-path: polygon(0 -80px, 100% -80px, 100% 80px, 0 80px);
      clip-path: polygon(0 -80px, 100% -80px, 100% 80px, 0 80px);
    }
    .add-to-cart .background,
    .add-to-cart .cart,
    .add-to-cart .check {
      position: absolute;
      pointer-events: none;
    }
    .add-to-cart .background {
      left: 0;
      top: -4px;
      right: 0;
      bottom: 0;
      display: block;
      fill: var(--background);
      transform: scale(var(--background-s)) translateZ(0);
    }
    .add-to-cart span {
      position: relative;
      z-index: 1;
      line-height: 18px;
      display: block;
      font-size: 14px;
      font-weight: 500;
      opacity: var(--text-o);
      transform: translateX(var(--text-x)) translateZ(0);
    }
    .add-to-cart .drop {
      position: absolute;
      z-index: 1;
      left: 70px;
      top: 8px;
      width: 4px;
      height: 4px;
      border-radius: 50%;
      background: var(--drop);
    }
    .add-to-cart .cart {
      z-index: 2;
      bottom: 11px;
      left: calc(50% - 2px);
      transform-origin: 10px 17px;
      transform: translate(var(--cart-x), var(--cart-y)) scale(var(--cart-s)) rotate(var(--cart-r)) translateZ(0);
    }
    .add-to-cart .cart svg {
      display: block;
      width: 24px;
      height: 19px;
      stroke: var(--color);
      stroke-linecap: round;
      stroke-linejoin: round;
      fill: none;
      position: relative;
      z-index: 1;
      transform: translateZ(0);
    }
    .add-to-cart .cart img {
      display: block;
      position: absolute;
      bottom: 7px;
      left: 50%;
      opacity: var(--bottle-o);
      transform-origin: 50% 100%;
      transform: translate(var(--bottle-x), var(--bottle-y)) scale(var(--bottle-s)) rotate(var(--bottle-r)) translateZ(0);
    }
    .add-to-cart .check {
      bottom: 0;
      left: 50%;
      padding: 2px;
      background: var(--background);
      border-radius: 50%;
      transform: translate(-50%, var(--check-y)) scale(var(--check-s)) translateZ(0);
    }
    .add-to-cart .check svg {
      display: block;
      width: 10px;
      height: 10px;
      fill: none;
      stroke-width: 1.5;
      stroke-linecap: round;
      stroke-linejoin: round;
      stroke: var(--color);
      stroke-dasharray: 8.5px;
      stroke-dashoffset: var(--check-offset);
    }

    html {
      box-sizing: border-box;
      -webkit-font-smoothing: antialiased;
    }

    * {
      box-sizing: inherit;
    }
    *:before, *:after {
      box-sizing: inherit;
    }

    body {
      min-height: 100vh;
      display: flex;
      font-family: "Inter", Arial;
      justify-content: center;
      align-items: center;
      background: #000;
    }
    body .dribbble {
      position: fixed;
      display: block;
      right: 20px;
      bottom: 20px;
    }
    body .dribbble img {
      display: block;
      height: 28px;
    }
    body .twitter {
      position: fixed;
      display: block;
      right: 64px;
      bottom: 14px;
    }
    body .twitter svg {
      width: 32px;
      height: 32px;
      fill: #1da1f2;
    }
  </style>
</head>
<body>
<button class="add-to-cart">
  <div class="cart">
    <svg viewBox="0 0 24 19">
      <path d="M11.25 17C11.25 17.6904 10.6904 18.25 10 18.25C9.30964 18.25 8.75 17.6904 8.75 17C8.75 16.3096 9.30964 15.75 10 15.75C10.6904 15.75 11.25 16.3096 11.25 17Z" stroke-width="1.5 "/>
      <path d="M19.25 17C19.25 17.6904 18.6904 18.25 18 18.25C17.3096 18.25 16.75 17.6904 16.75 17C16.75 16.3096 17.3096 15.75 18 15.75C18.6904 15.75 19.25 16.3096 19.25 17Z" stroke-width="1.5 "/>
      <path d="M1 1H5L5.91304 4M5.91304 4L8.06853 11.0823C8.32483 11.9245 9.10161 12.5 9.98188 12.5H18.585C19.4329 12.5 20.1887 11.9653 20.471 11.1656L23 4H5.91304Z" stroke-width="2" />
    </svg>
    <img srcset="https://assets.codepen.io/165585/alge.png 1x, https://assets.codepen.io/165585/alge@2x.png 2x" />
  </div>
  <span>Add to cart</span>
  <div class="check">
    <svg viewBox="0 0 10 10">
      <path d="M2 5L4 7L8 3" />
    </svg>
  </div>
  <svg class="background" viewBox="0 0 142 46">
    <path d="M0 19C0 10.7157 6.71573 4 15 4H41.4599C53.6032 4 62.4844 4 72.5547 4C82.6251 4 91.5063 4 103.65 4H137C139.761 4 142 6.23858 142 9V31C142 39.2843 135.284 46 127 46H5C2.23858 46 0 43.7614 0 41V19Z" />
  </svg>
</button>


<a class="dribbble" href="https://dribbble.com/ai" target="_blank"><img src="https://cdn.dribbble.com/assets/dribbble-ball-mark-2bd45f09c2fb58dbbfb44766d5d1d07c5a12972d602ef8b32204d28fa3dda554.svg" alt=""></a>
</body>

<script>
  const { to, registerPlugin, set, timeline } = gsap

  gsap.registerPlugin(MorphSVGPlugin, Physics2DPlugin)

  document.querySelectorAll('.add-to-cart').forEach(button => {
    let background = button.querySelector('.background path')
    button.addEventListener('pointerdown', e => {
      if(button.classList.contains('active')) {
        return
      }
      to(button, {
        '--background-s': .97,
        duration: .1
      })
    })
    button.addEventListener('click', e => {
      e.preventDefault()
      if(button.classList.contains('active')) {
        return
      }
      button.classList.add('active')
      to(button, {
        keyframes: [{
          '--background-s': .97,
          duration: .1
        }, {
          '--background-s': 1,
          delay: .1,
          duration: .8,
          ease: 'elastic.out(1, .6)'
        }]
      })
      to(button, {
        '--text-x': '16px',
        '--text-o': 0,
        duration: .2
      })
      to(button, {
        keyframes: [{
          '--cart-x': '-12px',
          '--cart-s': 1,
          duration: .25
        }, {
          '--bottle-s': 1,
          '--bottle-o': 1,
          duration: .15,
          onStart() {
            to(button, {
              duration: .4,
              keyframes: [{
                '--bottle-r': '-8deg'
              }, {
                '--bottle-r': '8deg'
              }, {
                '--bottle-r': '0deg'
              }]
            })
          }
        }, {
          '--bottle-y': '0px',
          duration: .3,
          delay: .15,
          onStart() {
            to(background, {
              keyframes: [{
                morphSVG: 'M0 19C0 10.7157 6.71573 4 15 4H41.4599C53.6032 4 62.4844 12 72.5547 12C82.6251 12 91.5063 4 103.65 4H137C139.761 4 142 6.23858 142 9V31C142 39.2843 135.284 46 127 46H5C2.23858 46 0 43.7614 0 41V19Z',
                duration: .1,
                delay: .18
              }, {
                morphSVG: 'M0 19C0 10.7157 6.71573 4 15 4H41.4599C53.6032 4 62.4844 4 72.5547 4C82.6251 4 91.5063 4 103.65 4H137C139.761 4 142 6.23858 142 9V31C142 39.2843 135.284 46 127 46H5C2.23858 46 0 43.7614 0 41V19Z',
                duration: .8,
                ease: 'elastic.out(1, .6)'
              }]
            })
            to(button, {
              '--bottle-s': .5,
              duration: .15
            })
          }
        }, {
          '--cart-y': '3px',
          duration: .1,
          onStart() {
            to(button, {
              keyframes: [{
                '--check-y': '24px',
                '--check-s': 1,
                duration: .25
              }, {
                '--check-offset': '0px',
                duration: .25
              }]
            })
          }
        }, {
          '--cart-y': '0px',
          duration: .2
        }, {
          '--cart-x': '-6px',
          '--bottle-r': '12deg',
          '--bottle-x': '-25%',
          duration: .15
        }, {
          '--cart-x': '-16px',
          '--bottle-r': '-12deg',
          '--bottle-x': '-50%',
          duration: .2,
          onStart() {
            drops(button, 5, -130, -100);
          },
        }, {
          '--cart-x': '92px',
          '--cart-r': '-20deg',
          duration: .4,
          onStart() {
            button.classList.add('clipped')
          },
          onComplete() {
            set(button, {
              '--cart-x': '-120px',
              '--cart-s': .8,
              '--cart-y': '-2px',
              '--bottle-o': 0,
              '--text-x': '2px'
            })
          }
        }, {
          '--cart-x': '-57px',
          '--cart-r': '0deg',
          '--check-s': 0,
          duration: .3,
          delay: .1,
          clearProps: true,
          onStart() {
            to(button, {
              '--text-x': '10px',
              '--text-o': 1,
              duration: .2,
              delay: .1
            })
          },
          onComplete() {
            button.classList.remove('active', 'clipped')
          }
        }]
      })
    })
  })

  function drops(parent, quantity, minAngle, maxAngle) {
    for(let i = quantity - 1; i >= 0; i--) {
      let angle = gsap.utils.random(minAngle, maxAngle),
              velocity = gsap.utils.random(60, 80)

      let div = document.createElement('div')
      div.classList.add('drop')

      parent.appendChild(div);

      set(div, {
        opacity: 1,
        scale: 0,
      });
      timeline({
        onComplete() {
          div.remove();
        }
      }).to(div, {
        duration: .4,
        scale: gsap.utils.random(.5, .7)
      }, 0).to(div, {
        duration: 1,
        physics2D: {
          angle: angle,
          velocity: velocity,
          gravity: 80
        }
      }, 0).to(div, {
        duration: .3,
        opacity: 0
      }, .3);
    }
  }

</script>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

购物车按钮 的相关文章

  • 将 SVG 路径转换为绝对命令

    给定一个 SVG Path 元素 如何将所有路径命令转换为绝对坐标 例如 转换此路径
  • html 图像 src 调用 javaScript 变量

    这是我的代码 我想问 我怎样才能做到这一点 img src img apple 我一直在尝试使用 call 函数和 document onload 但它根本不起作用 有人可以救我吗 我假设你只是想用 javascript 更新图像 src
  • 由于 MIME 类型而导致资源被阻止?

    多年来我已经成功地包含动态创建的 javascript 文件 这是一个例子 https granadainfo com sups php locs 95 https granadainfo com sups php locs 95 正如你所
  • 个人 Tumblr 帖子上的 Javascript

    我知道您可以编辑在 tumblr 博客上呈现所有帖子博客主页的 html AngularJS 但是 有什么办法可以添加自定义到各个帖子 我想在逐个帖子的基础上做一些 javascript 的东西 但似乎无法找到可以编辑代码的位置 或者 如果
  • 如何用 JavaScript 修复图像透视变形和旋转?

    我有一些用手机拍摄的图像 有没有可以拉直纸张照片并将其压平的 JavaScript 库 例如 我想创建一个矩形图像 该图像没有任何失真 换句话说我想知道如何用 JavaScript 修复透视变形和旋转 例如 我发现下面的示例图像来自this
  • vuejs 模板和 asp.net 部分视图,好的做法吗?

    我在网站中使用 Vue js 并将模板添加到 html 代码中 并将 js 代码添加到单个 js 文件中 所以我不想使用 vue Vuefy Browserfy 方法 而是稍后捆绑并缩小我的 js 文件 由于我必须使用 Asp Net MV
  • 使用 IE9、10、11 的 CSS 将比例打印到 50% 等百分比

    Zoom css 属性不适用于 IE9 10 11 观察到打印预览 UI 令人不安 默认比例为 缩小以适合 当我将此比例从 缩小 更改为适合 50 时 页面显示正常 打印预览 任何人都可以帮助我如何使用 CSS 代码将比例设置为 50 为页
  • 节点 sass 无效 CSS

    node sass 是否支持 use 由于我收到此错误 SassError t family fonts 之后的 CSS 无效 预期的表达式 例如 1px 粗体 为 roboto 这是 Nav scss 的代码 nav width 100
  • 只保留 A-Z 0-9 并使用 javascript 从字符串中删除其他字符

    我正在尝试验证字符串以使它们成为有效的网址 我只需要保留 A Z 0 9 并使用以下命令从字符串中删除其他字符javascript or jquery 例如 贝儿餐厅 我需要将其转换为 百丽餐厅 所以字符被删除 只保留 A Z a z 0
  • IE localStorage 事件失火

    在 Internet Explorer 9 和 10 中 localStorage 实现意外地触发事件 这里有很棒的线索 Chrome 的 localStorage 实现存在错误 https stackoverflow com questi
  • 如何绕过Access-Control-Allow-Origin?

    我正在一个平台上对我自己的服务器进行ajax调用 他们设置了阻止这些ajax调用的平台 但我需要它从我的服务器获取数据以显示从我的服务器数据库检索到的数据 我的 ajax 脚本正在运行 它可以将数据发送到我的服务器的 php 脚本以允许其处
  • 在 Chrome 开发者工具中禁用调试器语句

    我正在尝试对恶意 JavaScript 进行逆向工程 当我最初加载侧面时 会注入 JS 代码 其中包括 debugger 语句并将断点注入我的 chrome 开发人员控制台 通过stackoverflow阅读 禁用所有断点does not帮
  • JavaScript 错误:MVC2 视图中的条件编译已关闭

    我试图在 MVC2 视图页面中单击时调用 JavaScript 函数 a href Select a JavaScript 函数 function SelectBenefit id code alert id alert code 这里 b
  • 此版本的 CLI 仅与 Angular 版本 5.0.0 或更高版本兼容错误

    我已经有 Angular 项目在 4 版本中运行 在安装新项目时 不幸的是我安装了 6 版本的 Angular cli 在以 4 版本运行的旧项目中运行 ngserve 命令时 这会引发错误 您的全局 Angular CLI 版本大于本地版
  • 如何将项目插入到特定索引处的空数组中?

    我想将一个项目插入到空数组的指定索引中 我看到有 Array prototype splice 方法 但是 如果我在空数组上使用 splice 它只会添加项目来结束数组 如下所示 var a a splice 3 0 item 3 cons
  • 什么时候可以使用Javascript,什么时候不可以?

    不使用太多 javascript jquery 是个好习惯吗 我们应该尽可能避免它 为了良好的可访问性 吗 什么时候可以使用 JavaScript 什么时候不能在网页设计和开发中使用 JavaScript 在什么场景 什么条件下 Updat
  • 表单 CSS:根据选中/未选中状态设置单选框的父级(标签)样式

    所以我有一个表格 表格中提出的大多数问题都是使用无线电输入 我要和
  • 如何将数据推送到嵌套对象

    如何将另一个元素推入variables来自以下对象的属性 var request name Name id 3 rules name Rule name tags tagId 1 variables variable var1 matchT
  • 深层嵌套 Flexbox 布局是否会导致性能问题?

    我一直在开发一个 ReactJS 项目 其中我使用 Flexbox 布局创建了大部分组件 由于使用 React 我们可以拥有深度嵌套的组件 因此我的布局具有嵌套的 Flexbox 布局 现在我的问题是 这对性能有任何问题吗 在一个页面上 有
  • 如何从 Cloud Functions for Firebase 文件夹读取证书文件

    我正在尝试读取 certs 文件夹下的文件 如下所示 functions certs idp public cert perm 这是我用来读取文件的代码 fs readFileSync path join dirname certs idp

随机推荐