【问题标题】:Is it possible to make a squiggly line?是否可以制作波浪线?
【发布时间】:2013-06-24 21:34:03
【问题描述】:

如果我想画一条水平线,我会这样做:

<style>
#line{
    width:100px;
    height:1px;
    background-color:#000;
}
</style>
<body>
<div id="line"></div>

如果我想画一条垂直线,我会这样做:

#line{
    width:1px;
    height:100px;
    background-color:#000;
}
</style>
<body>
<div id="line"></div>

曲线比较复杂,但可以使用border-radius 并包裹元素:

<style>
.curve{
    width:100px;
    height:500px;
    border:1px #000 solid;
    border-radius:100%;
}
#wrapper{
    overflow:hidden;
    width:40px;
    height:200px;
}
</style>
<body>
<div id="wrapper">
    <div class="curve"></div>
</div>
</body>

但我什至无法理解如何生成波浪线! 仅使用 css(和 javascript,因为似乎有必要更轻松地生成它们)甚至可以远程实现。

注意:

正如预期的那样,鉴于您的回答,没有办法在唯一的 css 中做到这一点...javascript 和 jquery 100% 可以满足您的回答...不能使用任何图像

【问题讨论】:

  • 几个.curve元素粘在一起了?
  • CSS3 border images怎么样?
  • 您可以使用多个相邻且具有非常高边界半径的 div 集来完成类似的操作。您还必须隐藏元素的上半部分/下半部分,具体取决于它们是奇数还是偶数,您可以使用 CSS 进行操作。
  • 为什么没有图片?甚至 SVG 都没有?
  • @david 因为我使用它的应用程序会让用户创建曲线

标签: javascript jquery html css


【解决方案1】:

如果您希望某些文本的下划线是波浪线,您可以使用以下 css:

span {
  text-decoration-line: underline;
  text-decoration-style: wavy;
  text-decoration-color: red;
}
&lt;span&gt;Example text here&lt;/span&gt;

来源:https://developer.mozilla.org/en/docs/Web/CSS/text-decoration-line#example

【讨论】:

    【解决方案2】:

    纯 CSS 解决方案:

    我们可以使用sin wave character'∿'字符,然后

    letter-spacing设置一个负值

    FIDDLE

    为了好玩,我们可以使用不同的字符来获得其他曲线:

    FIDDLE #2

    div {
      font-size: 50px;
      font-family: verdana;
    }
    .tilde {
      letter-spacing: -19px;
    }
    .ohm {
      letter-spacing: -6px;
    }
    .ac {
      letter-spacing: -25px;
    }
    .acd {
      letter-spacing: -11px;
    }
    .curlyv {
      letter-spacing: -12px;
    }
    .frown {
      letter-spacing: -13px;
    }
    <div class="acd">∿∿∿∿∿∿∿∿∿∿∿∿∿∿∿</div>
    <div class="tilde">˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜</div>
    <div class="curlyv">⋎⋎⋎⋎⋎⋎⋎⋎⋎⋎⋎⋎⋎⋎⋎⋎</div>
    <div class="frown">⌢⌢⌢⌢⌢⌢⌢⌢⌢⌢⌢⌢⌢⌢⌢⌢⌢</div>
    <div class="ac">∾∾∾∾∾∾∾∾∾∾∾∾∾∾∾∾∾∾∾∾∾</div>
    <div class="ohm">ΩΩΩΩΩΩΩΩΩΩ</div>

    【讨论】:

    • 罪波 ∿卷曲⋎欧姆Ω皱眉︵以下适用于字母间距 0 - ﹏ (波浪低线)﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏
    • 我可以依靠哪些常用字体来获得这些字符?我担心字母间距必须根据浏览器使用的字体而有所不同。在我的系统上,至少,Verdana 用于波浪号,但备用字体用于波浪低线和正弦波字符。
    • 这确实很简洁,但由于字体差异,我不建议在现实世界中使用它。
    【解决方案3】:

    这个问题相当老了,但我找到了一种无需 Javascript、重复 CSS 或图像的方法。

    使用 background-size 可以重复一个图案,可以使用纯 CSS 使用线性渐变或径向渐变创建。

    我在这里放了一堆例子:http://jsbin.com/hotugu/edit?html,css,output

    基本要点是:

    .holder {
      /* Clip edges, as some of the lines don't terminate nicely. */
      overflow: hidden;
      position: relative;
      width: 200px;
      height: 50px;
    }
    
    .ellipse {
      position: absolute;
      background: radial-gradient(ellipse, transparent, transparent 7px, black 7px, black 10px, transparent 11px);
      background-size: 36px 40px;
      width: 200px;
      height: 20px;
    }
    
    .ellipse2 {
      top: 20px;
      left: 18px;
      background-position: 0px -20px;
    }
    <div class="holder">
      <div class="ellipse"></div>
      <div class="ellipse ellipse2"></div>
    </div>

    您可以通过一些修改产生一些令人信服的波浪线:

    .holder {
        position: relative;
        width: 200px;
        height: 50px;
        top: 25px;
    }
    .tinyLine {
        position: absolute;
        /* Cuts off the bottom half of the pattern */
        height: 20px;
        /* For better cross browser consistency, make it larger with width.  */
        width: 1000%;
        /* And then scale it back down with scale, recentering with translateX. */
        transform: translateX(-45%) scale(0.1);
    }
    
    .tinyLine1 {
        background: linear-gradient(45deg, transparent, transparent 49%, red 49%, transparent 51%);
    }
    .tinyLine2 {
        background: linear-gradient(-45deg, transparent, transparent 49%, red 49%, transparent 51%);
    }
    .tinyLine {
        /* Must be after background definition. */
        background-size: 40px 40px;
    }
    <div class="holder">
        <div class="tinyLine tinyLine1"></div>
        <div class="tinyLine tinyLine2"></div>
    </div>

    浏览器支持还可以 (http://caniuse.com/#feat=css-gradients),IE 10 可能会工作,但是在不同的浏览器中会出现小规模的问题。如果您希望它在非常小的范围内始终如一地工作,您可能希望在更大的范围内制作线条,然后使用transform: scale(x); 缩小它。

    它也应该非常快,线性渐变在 GPU 上以 chrome 呈现。

    【讨论】:

    • 这太巧妙了!我需要一种模拟text-decoration: underline wavy red; 跨浏览器的方法,并设法调整这个答案并使用:before:after 伪元素来获得一个可行的替代品。谢谢!
    【解决方案4】:

    编辑:鉴于没有图像/数据 uri 的要求。

    您还可以将一堆边界半径元素塞在一起,交替禁用上/下或左/右边缘。我已经将其概括为一个将它们附加到元素的函数。

    Javascript,其中 squigglecount 是“squiggles”的数量。如果您愿意,可以将其概括为实际宽度。

    http://jsfiddle.net/V7QEJ/1/

    function makeLine(id, squiggleCount) {
      var curve;
      var lineEl = $(id);
    
      for (var i = 0; i < squiggleCount; i++) {
        curve = document.createElement('div');
        curve.className = 'curve-1';
        lineEl.append(curve);
    
        curve = document.createElement('div');
        curve.className = 'curve-2';
        lineEl.append(curve);
      }
    }
    $(document).ready(function(){
        makeLine('#line', 16);
    });
    .curve-1,
    .curve-2 {
      display: inline-block;
      border: solid 1px #f00;
      border-radius: 50px;
      height: 20px;
      width: 20px;
    }
    
    .curve-1 {
      border-bottom: none;
      border-left: none;
      border-right: none;
    }
    
    .curve-2 {
      border-top: none;
      border-left: none;
      border-right: none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="line">
    </div>

    旧(带图片):

    已经有很多答案了,但是这里有一个简单的方法来做一条垂直的波浪线,类似于劳森的答案。

    基本上,您使用背景图像和波浪线的数据 uri 来执行此操作。我可能不会将它用于任何事情,但这是一个有趣的思考练习。有一堆数据 uri 生成器,您可以在线使用它们来更改自己的图像。

    http://jsfiddle.net/zadP7/

    .aux{
        display: inline-block;
        vertical-align: top;
    }
    .line{
        display: inline-block;
        
        height: 400px;
        width: 10px;
        
        background-image:       url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAoCAYAAADHVmuAAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3QYYFSs0NbBHxgAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAGc0lEQVQ4EQFoBpf5AQAAAP8AAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAA/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAD/AAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAD/AAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAA/wAAAAAAAAAAAAAAAAAAAAEAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/wAAAAAAAAAAAAAAAAAAAAEBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/AAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/AAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/AAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP8AAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/AAAAAAAAAAAAAAAAAAAAAQEAAAAAAAAAAAAAAAAAAAAAAAAA/wAAAAAAAAAAAAAAAAAAAAEAAAAAAgAAAAAAAAAAAAAAAAAAAP8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAD/AAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAEAAAAAAAAA/wAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAQAAAP8AAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACelxA/rjIgrgAAAABJRU5ErkJggg==);   
    }
    <div class="aux">Stuff</div>
    <div class="line"></div>
    <div class="aux">More Stuff</div>

    【讨论】:

    • 我添加了一个不使用图像的替代解决方案,如果这对您有任何额外帮助的话。
    • 这个答案是最好的,虽然只能重复工作,但它提供了很大的空间!我会接受它,因为在看到这些答案后,一个复杂的、不重复的曲线显然只能用图像来完成
    【解决方案5】:

    感谢@yeerk 提供如此出色的解决方案!

    但我想建议对他的第一个变体进行一些改进——对那些看起来更三角形的波浪。我建议使用 :before:after 伪元素,而不是硬编码的封闭 div。

    它可能看起来像这样(html):

    <div class="triangly-line"></div>
    

    — 其中triangly-line 是目标装饰元素(不是“挥手”而是“三角形”)。

    相应的样式(使用 LESS 表示法)将如下所示:

    @line-width: 300px;
    @triangled-size: 6px;
    @triangly-color: red;
    @double-triangled-size: (@triangled-size * 2 - 1px);
    .linear-gradient (@gradient) {
        background: -webkit-linear-gradient(@gradient);
        background: -o-linear-gradient(@gradient);
        background: linear-gradient(@gradient);
    }
    .triangly-gradient (@sign, @color) {
        .linear-gradient(~"@{sign}45deg, transparent, transparent 49%, @{color} 49%, transparent 51%");
    }
    .triangly-line {
        overflow: hidden;
        position: relative;
        height: @triangled-size;
        &:before {
            .triangly-gradient("", @triangly-color);
        }
        &:after {
            .triangly-gradient("-", @triangly-color);
        }
        &:before,
        &:after {
            content: "";
            display: block;
            position: absolute;
            width: @line-width;
            height: @triangled-size;
            background-size: @double-triangled-size @double-triangled-size !important;
        }
    }
    

    生成的 CSS(使用上面指定的参数):

    .triangly-line {
        overflow: hidden;
        position: relative;
        height: 6px;
    }
    .triangly-line:before {
        background: -webkit-linear-gradient(45deg, transparent, transparent 49%, red 49%, transparent 51%);
        background: -o-linear-gradient(45deg, transparent, transparent 49%, red 49%, transparent 51%);
        background: linear-gradient(45deg, transparent, transparent 49%, red 49%, transparent 51%);
    }
    .triangly-line:after {
        background: -webkit-linear-gradient(-45deg, transparent, transparent 49%, red 49%, transparent 51%);
        background: -o-linear-gradient(-45deg, transparent, transparent 49%, red 49%, transparent 51%);
        background: linear-gradient(-45deg, transparent, transparent 49%, red 49%, transparent 51%);
    }
    .triangly-line:before,
    .triangly-line:after {
        content: "";
        display: block;
        position: absolute;
        width: 300px;
        height: 6px;
        background-size: 11px 11px !important;
    }
    

    【讨论】:

      【解决方案6】:

      如果您不是在寻找真正整洁的东西,而只是为了好玩,请使用多个 box-shadow: http://codepen.io/gcyrillus/pen/mfGdphttp://codepen.io/gcyrillus/pen/xhqFu

      .curve{
        margin:3em 0;
        width:100px;
        height:150px;
        border-radius:50%;
        box-shadow:
          0px 2px 1px -1px,
          400px 0px 0px 0px white,
          400px 2px 1px -1px ,
          300px 0px 0px 0px white,
          300px -2px 1px -1px,
          600px 0px 0px 0px white,
          600px 2px 1px -1px ,
          500px 0px 0px 0px white,
          500px -2px 1px -1px,
          800px 0px 0px 0px white,
          800px 2px 1px -1px ,
          700px 0px 0px 0px white,
          700px -2px 1px -1px,
          1000px 0px 0px 0px white,
          1000px 2px 1px -1px ,
          900px 0px 0px 0px white,
          900px -2px 1px -1px,
          1200px 0px 0px 0px white,
          1200px 2px 1px -1px ,
          1100px 0px 0px 0px white,
          1100px -2px 1px -1px,
          1400px 0px 0px 0px white,
          1400px 2px 1px -1px ,
          1300px 0px 0px 0px white,
          1300px -2px 1px -1px,
          1600px 0px 0px 0px white,
          1600px 2px 1px -1px ,
          1500px 0px 0px 0px white,
          1500px -2px 1px -1px;
        position:relative;
      }
      .curve:before,.curve:after {
        content:'';
        position:absolute;
        height:100%;
        width:100%;
        border-radius:100%;
        box-shadow:inherit;
      }
      .curve:before {
        left:100%;
        transform:rotate(180deg);
       }
      .curve:after {
        left:200%;
      }
      

      【讨论】:

        【解决方案7】:

        ﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏﹏

        ﹏ (波浪低线)

        我希望这不是太离题 - 这里是如何使用这些波浪线来为某些文本加下划线(应该是一个常见的用例。)

        方法 1(取自 Wulf 回答 related question

        <span style="border-bottom: 1px dotted #ff0000;padding:1px">
            <span style="border-bottom: 1px dotted #ff0000;">
                foobar
            </span>
        </span>
        

        (不是真正的波浪线,而是一组点,但看起来不错,而且非常简单。)

        方法 2(受 DanieldD 启发)

        使用 & #65103 ; (波浪低线)unicode 字符和绝对/相对定位,以将该字符放在某些文本下方。这是fiddle

        这是定位代码的“肉”

        function performUnderWavyLowLineazation(contentElt){
            var wavyFontSize = 40;
            var width = contentElt.width();
            contentElt.addClass("underWavyLowLined");
            replaceSpaceByNonBreakingSpace(contentElt);
            var sp = "<span/>";
            var wrapper = contentElt.wrap(sp).parent();
            wrapper.addClass("wavyParent");
            var underlining = jQuery(sp, {"class" : "wavyLowLine"}).prependTo(wrapper);
            var ghost;
            var invisibleGhostThatTakesUpTheSpaceThatUnderWavyLowLinedElementShouldTakeButDoesntDueToBeingAbsolute
                = ghost = jQuery(sp, {"class": "invisibleUnderWavyLowLined"}).appendTo(wrapper);
            ghost.html(contentElt.html());
            ghost.find("*").removeAttr("id");
            replaceSpaceByNonBreakingSpace(ghost);
            var numWavyChars = Math.floor(0.1 + width/wavyFontSize);
            innerUnderLine = jQuery(sp, {"class": "innerWaveLine"}).appendTo(underlining);
            innerUnderLine.html("&#65103;".repeat(numWavyChars));
            var lineLength = wavyFontSize * numWavyChars;
            var defect = width - lineLength;
            innerUnderLine.css("left", defect/2);
            var wavyGroup = jQuery(sp, {"class" : "wavyGroup"}).prependTo(wrapper);
            underlining.appendTo(wavyGroup);
            contentElt.appendTo(wavyGroup);
        }
        

        【讨论】:

          【解决方案8】:

          在 HTML5 和 Canvas 出现之前,有 JavaScript VectorGraphics。如果您想在纯 HTML 中绘制圆形、椭圆等,您可能想尝试一下。

          【讨论】:

            【解决方案9】:

            不要使用边框,而是使用平铺的背景图片。

            我认为没有一种解决方案可以在不使用图形文件的情况下摆脱困境并且适用于所有浏览器。

            如果你胆子大,可以试试这个:http://www.html5canvastutorials.com/tutorials/html5-canvas-arcs/

            它允许在 HTML5 的画布上绘图,但它不适用于旧版浏览器。

            如果你可以添加很多 html,你可以使用这个: http://jsfiddle.net/QsM4J/

            HTML:

            <body>
                <p>
                before
                </p>
                <div id="sqig">
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                    <div class="topsqig"><div></div></div>
                    <div class="bottomsqig"><div></div></div>
                </div>
                <p>
                after
                </p>
            </body>   
            

            CSS:

            #sqig{
                position:relative;
                width:400px;
                height:6px;
            }
            #sqig div{
                position:relative;
                width:6px;
                height:6px;
                display: inline-block;
                margin:0 0 0 -4px;
                padding:0;    
            }
            #sqig .topsqig div{
                border-radius: 3px;
                top:1px;
                border-top: 1px solid #000;
            }
            #sqig .bottomsqig div{
                border-radius: 3px;
                top:-1px;
                border-bottom: 1px solid #000;
            }
            

            【讨论】:

            • 我将研究 html 画布...似乎我可以给它一个图表,我宁愿不必想出一个等式,但这可能是唯一的方法...和我无法使用图片
            • 嗯,这很不幸,但不知何故,那个链接是旧版本的......无论如何,我更新了帖子中的链接以指向新版本:jsfiddle.net/QsM4J
            【解决方案10】:

            这是一个基于@yeerk 的答案的 SASS 波浪线生成器。如果你想要三角形,使用上面@lilliputten的生成器。

            $waveHeight: 40px;
            $waveLength: 70px;
            $waveRadius: 13px; // adjust depending on length
            $waveThickness: 8px;
            
            @mixin waveSide() {
              position: absolute;
              background: radial-gradient(ellipse, transparent, transparent $waveRadius, black $waveRadius, black #{$waveRadius + $waveThickness}, transparent #{$waveRadius + $waveThickness});
              background-size: #{$waveLength} #{$waveHeight * 2};
              height: $waveHeight;
            }
            
            .wavy {
              width: 400px; // give the wave element a length here
            
              @include waveSide;
            
              &::before {
                $waveOffset: $waveLength / 2;
                @include waveSide;
            
                content: '';
                width: calc(100% - #{$waveOffset});
                top: $waveHeight;
                left: $waveOffset;
                background-position: 0px -#{$waveHeight};
              }
            }
            

            【讨论】:

              【解决方案11】:

              我发现了一种更好的方法来在 CSS 中实现三角形波浪线,而无需将高度减半或应用在浏览器中无法正常工作的技巧。

              我尝试了@yeerk's solution,但它只在 Chrome 中运行良好。这些行在 Safari 和 Firefox 上有工件。

              火狐

              Safari

              此解决方案是@liliputen's solution 的变体,但它提高了灵活性。

              您可以通过background-size 属性轻松更改线条的大小。

              .squiggly {
                  position: relative;
                  height: 10px;
                  width: 100%;
              }
              
              .squiggly::after,
              .squiggly::before {
                  height: inherit;
                  width: inherit;
                  background-size: 12px 100%; /* Change this to adjust the size of the squiggly line. */
                  content: "";
                  position: absolute;
              }
              
              .squiggly::before {
                  top: -2px;
                  background-image: linear-gradient(45deg, red 35%, transparent 0),
                      linear-gradient(-45deg, red 35%, transparent 0);
              }
              
              .squiggly::after {
                  top: 0px;
                  background-image: linear-gradient(45deg, white 35%, transparent 0),
                      linear-gradient(-45deg, white 35%, transparent 0);
              }
              &lt;div class="squiggly"&gt;&lt;/div&gt;

              您也可以在 JS Fiddle 上找到它。

              【讨论】:

                【解决方案12】:

                如果您使用的是 Javascript,这可以使用正弦波轻松实现 - 这就是几十年来编程语言实现可控波浪线的方式!你应该可以找到很多例子,但本质上你只是用递增的 x 值循环并应用正弦函数 sin()。这曾经在 90 年代用于制作屏幕保护程序并为其制作动画等很酷。

                【讨论】:

                  猜你喜欢
                  • 2012-07-31
                  • 2022-12-18
                  • 2012-06-02
                  • 1970-01-01
                  • 2018-06-21
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多