【问题标题】:How can i collect 4 small triangle shapes to make a big triangle?我如何收集 4 个小三角形来制作一个大三角形?
【发布时间】:2015-08-25 22:21:30
【问题描述】:

我想制作一个由4个小三角形组成的图像背景三角形,就像这张图片一样

如何制作这个三角形图像背景形状的集合?!

.block {
    width: 0;
    height: 0;
    border: solid 20px;    
    float: left;
}
.clear {
    clear: both;
}
.top {
    margin-left: 38px;
}
.top .left {    
    border-color: transparent green green transparent;
}
.top .right {    
    border-color: transparent transparent green green;
}
.bottom .left1 {    
    border-color: transparent red red transparent;
}
.bottom .mid1 {    
    border-color: blue blue red red;
}
.bottom .mid2 {    
    border-color: blue purple purple blue;
}
.bottom .right1 {    
    border-color: transparent transparent purple purple;
}
<div class="top">
    <div class="block left"></div>
    <div class="block right"></div>
    <div class="clear"></div>
</div>
<div class="bottom">
    <div class="block left1"></div>
    <div class="block mid1"></div>
    <div class="block mid2"></div>
    <div class="block right1"></div>
</div>

enter link description here

【问题讨论】:

  • 您可以随时使用自己喜欢的图像编辑器制作图像。否则,您需要满足您的问题的解释可能比您通常在此网站上得到的要长。
  • 确实,您可能需要剪切每个图像(CSS 中不支持)然后单独定位每个图像/div。这个问题(IMO)对 SO 来说太宽泛了。
  • 完全可行,但没有得到很好的支持。你支持什么浏览器?图片是动态的、用户提供的,还是网站上的静态图片?需要更具体地满足您的需求。
  • @SeanStopnik 实际上我想为其添加一项功能,即我的网站用户可以通过从静态菜单中选择(如益智游戏)来更改每个小三角形的图像背景。我该怎么做?!
  • 是的,这绝对是您应该使用 SVG(或 Canvas,但 SVG 可能是更好的选择)的地方。不要费心尝试用 CSS 强制它 - SVG 会给你更好的结果,并且比你实现这一点所需的 CSS 功能还具有更好的浏览器支持。

标签: javascript jquery html css svg


【解决方案1】:

正如我在 cmets 中提到的,在这种情况下 SVG/canvas 可能是更好的解决方案。我不是他们的专家,但创建一个像你想要的那样简单的模式很简单(我used the background solution from this question):

<svg width="300" height="300">
    <defs>
        <pattern id="img1" width="100%" height="100%">
            <image xlink:href="http://lorempixel.com/200/200/people/" x="0" y="0" width="200" height="200" />
        </pattern>
        <pattern id="img2" width="100%" height="100%">
            <image xlink:href="http://lorempixel.com/200/200/animals/" x="0" y="0" width="200" height="200" />
        </pattern>
        <pattern id="img3" width="100%" height="100%">
            <image xlink:href="http://lorempixel.com/200/200/abstract/" x="0" y="0" width="200" height="200" />
        </pattern>
        <pattern id="img4" width="100%" height="100%">
            <image xlink:href="http://lorempixel.com/200/200/sports/" x="0" y="0" width="200" height="200"/>
        </pattern>
    </defs>
    <path d="M150,0 225,150 75,150" fill="url(#img1)" />
    <path d="M0,300 75,150 150,300" fill="url(#img2)" />
    <path d="M75,150 225,150 150,300" fill="url(#img3)" />
    <path d="M150,300 300,300 225,150" fill="url(#img4)" />
</svg>

编辑:根据下面 cmets 的要求,添加了一些代码来显示如何操作元素(单击两个三角形,它们的图像将交换):

var step = 0;
var $prev;

$("path").on("click", function() {
    switch (step) {
        // if it's the first step: save the current element for later
        case 0:
            step = 1;
            $prev = $(this);
            break;
        // if it's the second step: swap images and start again
        case 1:
            step = 0;
            var aux = $prev.attr("fill");
            $prev.attr("fill", $(this).attr("fill"));
            $(this).attr("fill", aux);
            break;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<svg width="300" height="300">
    <defs>
        <pattern id="img1" width="100%" height="100%">
            <image xlink:href="http://lorempixel.com/200/200/people/" x="0" y="0" width="200" height="200" />
        </pattern>
        <pattern id="img2" width="100%" height="100%">
            <image xlink:href="http://lorempixel.com/200/200/animals/" x="0" y="0" width="200" height="200" />
        </pattern>
        <pattern id="img3" width="100%" height="100%">
            <image xlink:href="http://lorempixel.com/200/200/abstract/" x="0" y="0" width="200" height="200" />
        </pattern>
        <pattern id="img4" width="100%" height="100%">
            <image xlink:href="http://lorempixel.com/200/200/sports/" x="0" y="0" width="200" height="200"/>
        </pattern>
    </defs>
    <path id="path1" stroke="black" d="M150,0 225,150 75,150 150,0" fill="url(#img1)" />
    <path id="path2" stroke="black" d="M0,300 75,150 150,300 0,300" fill="url(#img2)" />
    <path id="path3" stroke="black" d="M75,150 225,150 150,300 75,150" fill="url(#img3)" />
    <path id="path4" stroke="black" d="M150,300 300,300 225,150 150,300" fill="url(#img4)" />
</svg>

你也可以在这个 JSFiddle 上看到它:http://jsfiddle.net/4x7sh6bj/1/

【讨论】:

  • 你打败了我。这里有一个 +1。
  • 爱它:) 奇怪的问题,但一个有趣的问题。
  • 这个问题需要一个更好的标题...有什么建议吗?
  • 非常好! ;) 只是一个问题:实际上我想为其添加一项功能,即我的网站用户可以通过从静态菜单中选择(如益智游戏)来更改每个小三角形的图像背景。例如,当用户单击一个小三角形时然后点击另一个三角形,两个三角形交换?!我该怎么做?!
  • 您可以将不同的元素视为 DOM 的任何其他元素并相应地对其进行操作。让我用一个演示来更新答案
【解决方案2】:

使用边框不起作用...您可以使用剪裁,but it isn't well supported。看看下面的sn-p:

.imgwrap{
    text-align:center;
}
.up{
    -webkit-clip-path: polygon(100px 0px, 0px 150px, 200px 150px);
    clip-path: polygon(100px 0px, 0px 150px, 200px 150px);
    margin:0 -100px;
}
.down{
    -webkit-clip-path: polygon(0px 0px, 200px 0px, 100px 150px);
    clip-path: polygon(0px 0px, 200px 0px, 100px 150px);
}
<div class="imgwrap">
    <img  class="down" src="http://www.placehold.it/200x150"/>
    <img  class="up"src="http://www.placehold.it/200x150"/>
    <img  class="down"src="http://www.placehold.it/200x150"/>
    <br/>
    <img  class="down"src="http://www.placehold.it/200x150"/>
</div>

【讨论】:

  • 非常感谢 Ted,这个 html 适合我的工作吗?!实际上,我想为其添加一个功能,即我的网站用户可以通过从静态菜单中选择(如益智游戏)来更改每个小三角形的图像背景。我该怎么做?!
  • 我无法回答是否合适,那是你的电话。请参阅有关支持的链接。至于如何构建您的应用程序,我建议您开始使用它,如果遇到问题,请返回 StackOverflow。
  • 谢谢你亲爱的@Ted。我会做的。但只是一个问题:一些成员建议我使用 SVG,你的意见是什么?!
  • @hghayour 我想浏览器会更好地支持 SVG。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2013-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
相关资源
最近更新 更多