我读过很多文章,并且一直在尝试为剪切路径设置动画。此时,我只能让它显示Firefox中的clip-path,并且它始终将路径固定为0,0。

这是示例代码-> http://jsfiddle.net/justintense/4torLok9/1/

我使用一个简单的嵌入式SVG作为路径:

    <clipPath id="clipping">

        <polygon fill="#FFFFFF" points="40,35 40,15 20,35   "/>
        <polygon fill="#FFFFFF" points="0,35 20,35 0,15     "/>

        <circle id="white_overlay_9_" fill="#FFFFFF" cx="20" cy="18.77" r="7.393"/>
    </clipPath>

我不确定我尝试做的事情是否有可能,所以我只是在寻找指针,这样我才能获得这首单曲。

编辑:

我正在尝试重新创建类似于此站点的行为--------> http://uppymama.com/

最佳答案

定义clipPath:

您不需要在clipPath中包含三个元素,可以将其简化为单个path元素。

<path d="M0,0 L40,0 L20,15z M20,5 m-5,0 a5,5 0 1,0 10,0 a5,5 0 1,0 -10,0z" />

clipPath应用于div:

为了应用clip-path,如果需要maximum browser support,则可以通过divsvg元素将svg导入到foreignObject元素中。然后,将clip-path应用于foreignObject

如果您通过CSS应用clip-path,则这是browser support

动画clipPath:

现在,为了使指针在菜单项的中间对齐,您将需要JavaScript进行一些计算并为clipPathtransform属性设置动画。
  • 当页面加载时,计算从左窗角到第一个菜单项的距离,并将clip-path移动到该坐标。
  • 调整窗口大小时,再次计算距离并移动指针。
  • 单击菜单项时,使用setTimeout()和同步代码计算距离并为指针设置动画,直到到达正确的位置。



  • site的精确副本:

    正如您在该站点上看到的那样,指针未居中。

    我的解决方案将始终根据菜单项的宽度将指针居中。

    上的CodePen演示

    var items = document.getElementsByClassName('menu-item');
    var clipPath = document.getElementById('clip-path');
    var lastActive = items[0];
    for (i = 0; i < items.length; i++) {
      items[i].addEventListener('click', function() {
        var x = this.getBoundingClientRect().left + (this.offsetWidth / 2) - 20;
        animStart(x);
        lastActive = this;
      })
    }
    
    function animStart(end) {
      for (start = 0; start <= end; start += 0.1) {
        anim(start);
      }
    }
    
    function anim(start) {
      setTimeout(function() {
        clipPath.setAttribute('transform', 'translate(' + start + ', 0)');
      }, 1 * start);
    }
    
    function align() {
      var x = lastActive.getBoundingClientRect().left + (lastActive.offsetWidth / 2) - 20;
      animStart(x);
    }
    
    function resize() {
      var x = lastActive.getBoundingClientRect().left + (lastActive.offsetWidth / 2) - 20;
      clipPath.setAttribute('transform', 'translate(' + x + ', 0)');
    }
    window.onresize = resize;
    window.onload = align;
    body {
      background: #222222;
      margin: 0;
    }
    .nav-container {
      top: 20px;
      position: relative;
      width: 100%;
      height: 50px;
      background: repeating-linear-gradient(90deg, palegoldenrod 5px, palegreen 10px, palegreen 15px, paleturquoise 15px, paleturquoise 20px, plum 20px, plum 25px);
    }
    .nav {
      width: 100%;
      height: 50px;
      margin: 0 auto;
      text-align: center;
    }
    .menu-item {
      display: inline-block;
      width: 70px;
      padding: 17px 20px;
      color: #222222;
      font-size: 14px;
    }
    .menu-item:hover {
      color: seagreen;
      cursor: pointer;
    }
    #bottom {
      width: 100%;
      height: 35px;
      background: repeating-linear-gradient(90deg, palegoldenrod 5px, palegreen 10px, palegreen 15px, paleturquoise 15px, paleturquoise 20px, plum 20px, plum 25px);
    }
    #clip-path {
      transition: 0.5s transform;
    }
    <body>
      <div class="nav-container">
        <div class="nav">
          <div id="menu-item-1" class="menu-item">Home</div>
          <div id="menu-item-2" class="menu-item">About</div>
          <div id="menu-item-3" class="menu-item">Services</div>
          <div id="menu-item-4" class="menu-item">Locations</div>
          <div id="menu-item-5" class="menu-item">Contact Us</div>
        </div>
        <svg style="position: relative; top: -1px;" class="svg-defs" height="35" width="100%">
          <defs>
            <clipPath id="clipping">
              <path id="clip-path" transform="translate(0,0)" d="M0,0 L40,0 L20,15z M20,6 m-5,0 a5,5 0 1,0 10,0 a5,5 0 1,0 -10,0z" />
            </clipPath>
          </defs>
          <foreignObject x="0" y="0" clip-path="url(#clipping)" height="35" width="100%">
            <div id="bottom"></div>
          </foreignObject>
        </svg>
      </div>
    </body>



    更好的是:

    保留指针的最后一个位置。

    上的CodePen演示

    var items = document.getElementsByClassName('menu-item');
    var clipPath = document.getElementById('clip-path');
    var last = items[0];
    for (i = 0; i < items.length; i++) {
      items[i].addEventListener('click', function() {
        var x = this.getBoundingClientRect().left + (this.offsetWidth / 2) - 20;
        var currentX = clipPath.getAttribute('transform').replace('translate(', '');
        currentX = parseInt(currentX.replace(', 0)', ''), 10);
        animStart(currentX, x);
        last = this;
      })
    }
    
    function animStart(begin, end) {
      if (begin < end) {
        for (start = begin; start <= end; start += 0.1) {
          anim(start);
        }
      } else {
        var c = end;
        for (start = begin; end <= start; start -= 0.1) {
          animD(start, c);
          c += 0.1
        }
      }
    }
    
    function anim(start) {
      setTimeout(function() {
        clipPath.setAttribute('transform', 'translate(' + start + ', 0)');
      }, 1 * start);
    }
    
    function animD(start, c) {
      setTimeout(function() {
        clipPath.setAttribute('transform', 'translate(' + start + ', 0)');
      }, 1 * c);
    }
    
    function align() {
      var x = last.getBoundingClientRect().left + (last.offsetWidth / 2) - 20;
      animStart(0, x);
    }
    
    function resize() {
      var x = last.getBoundingClientRect().left + (last.offsetWidth / 2) - 20;
      clipPath.setAttribute('transform', 'translate(' + x + ', 0)');
    }
    window.onresize = resize;
    window.onload = align;
    body {
      background: #222222;
      margin: 0;
    }
    .nav-container {
      top: 20px;
      position: relative;
      width: 100%;
      height: 50px;
      background: repeating-linear-gradient(90deg, palegoldenrod 5px, palegreen 10px, palegreen 15px, paleturquoise 15px, paleturquoise 20px, plum 20px, plum 25px);
    }
    .nav {
      width: 600px;
      height: 50px;
      margin: 0 auto;
      text-align: center;
    }
    .menu-item {
      display: inline-block;
      width: 70px;
      padding: 17px 20px;
      color: #222222;
      font-size: 14px;
    }
    .menu-item:hover {
      color: seagreen;
      cursor: pointer;
    }
    #bottom {
      width: 100%;
      height: 35px;
      background: repeating-linear-gradient(90deg, palegoldenrod 5px, palegreen 10px, palegreen 15px, paleturquoise 15px, paleturquoise 20px, plum 20px, plum 25px);
    }
    <body>
      <div class="nav-container">
        <div class="nav">
          <div id="menu-item-1" class="menu-item">Home</div>
          <div id="menu-item-2" class="menu-item">About</div>
          <div id="menu-item-3" class="menu-item">Services</div>
          <div id="menu-item-4" class="menu-item">Locations</div>
          <div id="menu-item-5" class="menu-item">Contact Us</div>
        </div>
        <svg style="position: relative; top: -1px;" class="svg-defs" height="35" width="100%">
          <defs>
            <clipPath id="clipping">
              <path id="clip-path" transform="translate(0,0)" d="M0,0 L40,0 L20,15z M20,6 m-5,0 a5,5 0 1,0 10,0 a5,5 0 1,0 -10,0z" />
            </clipPath>
          </defs>
          <foreignObject x="0" y="0" clip-path="url(#clipping)" height="35" width="100%">
            <div id="bottom"></div>
          </foreignObject>
        </svg>
      </div>
    </body>

    关于css - 如何在动画中使用剪切路径?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27628717/

    10-09 07:06
    查看更多