2

我正在尝试来自http://www.smashingmagazine.com/2015/05/creating-responsive-shapes-with-clip-path/的 CSS Clip Path ,我遇到了这个疯狂的错误。简而言之,该代码适用于 CodePen 和 JSFiddle,但无法在我的本地/应用程序上运行。

这是我试图提出的多边形的代码。首先是CSS:

nav {
    position: fixed;
    bottom: 0;
    background: #BE0F16;
    color: #fff;
    width: 100%;
    padding: 2rem 1rem;
    text-align: right;
    -webkit-clip-path: polygon(0 0, 0 100%, 100% 100%, 100% 50%);
    clip-path: polygon(0 0, 0 100%, 100% 100%, 100% 50%);
    -webkit-clip-path: url("#clip-shape");
    clip-path: url("#clip-shape");
}
nav .next-chapter {
    color: #fff;
    padding-left: 1rem;
}

这是相关的HTML:

<!DOCTYPE html>
<html>

<head>
    <title>Something</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <link href="css.css" rel="stylesheet" />
</head>

<body>
    <nav>
        <a class="menu"><i title="Menu" class="fa fa-bars"></i><h1 class="visuallyhidden">Menu</h1></a>
        <a class="next-chapter" href="/<%=next%>"><i title="Next Chapter" class="fa fa-hand-o-right"></i><span class="visuallyhidden">Next Chapter</span></a>
        <a id="comment" href="http://twitter.com/?status=@uebyn"></a>
    </nav>
    <svg width="0" height="0">
        <defs>
            <clipPath id="clip-shape" clipPathUnits="objectBoundingBox">
                <polygon points="0 0, 0 1, 1 1, 1 .5" />
            </clipPath>
        </defs>
    </svg>
    <script src="script.js"></script>
</body>

</html>

当我打开 index.html(上面的 HTML)时,它显示了一个矩形而不是我期望的多边形。然而,我遵循了文章中所述的确切说明。

然后我将代码复制到同一浏览器上的CodePen ( http://codepen.io/anon/pen/JdwrQw ) 和 JSFiddle ( http://jsfiddle.net/yk95wxmL/ ),它就可以工作了。

我一生都无法理解这一点。Firefox 理解并在 CodePen 和 JSFiddle 上的相同代码上剪辑路径,但不是在我的 HTML 上?可以肯定的是,我将整个 HTML 复制到 Codepen,并且 css 剪辑路径有效。这完全超出了我的范围。如果有人能提出一个可能显而易见但我不知何故错过的建议,我将不胜感激。

4

2 回答 2

3

假设 css 在一个单独的文件中,即当您编写时 css.css

clip-path: url("#clip-shape");

这实际上是缩写

clip-path: url("css.css#clip-shape");

但是文件 css.css 没有 id 为 clip-shape 的元素(所有元素都在 html 文件中)。

你需要写

clip-path: url("<the name of the html file goes here>#clip-shape");

显然,如果您使用 jsfiddle,所有内容都在同一个文档中,因此您不会在那里看到这个问题。

这里没有 Firefox 错误。

于 2015-08-02T14:28:28.920 回答
0

谢谢,如果规则在 CSS 文件中,这将修复它:

.myclass{
clip-path: url("/~powersparks/bz.html#clip");
}

如果仅添加到 d3 模式,它也可以工作并且需要:

     svg.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("clip-path", "url('/~powersparks/bz.html#clip')")
      .attr("d", area);
于 2016-11-20T03:09:54.143 回答