【问题标题】:Using CSS3 turn triangle into square with animation on hover使用 CSS3 将三角形变成正方形,悬停动画
【发布时间】:2016-05-24 11:09:18
【问题描述】:

我正在尝试使用 HTML5 和 CSS3 开发 CSS 框悬停效果,但我无法让它工作。我想制作如下所示的效果:

当用户没有悬停时

当用户悬停时

即当用户使用 HTML5 和 CSS3 将鼠标悬停在蓝色三角形上时,如何制作蓝色三角形并将其变成蓝色正方形?我只需要使用 HTML5 和 CSS3 而不使用画布。

此元素与画布完美搭配,如下所示

var ctx = document.getElementById("c").getContext("2d"); 
ctx.fillStyle = "#0000ff"; 
function normal() { 
    ctx.clearRect(0,0,256,256);
    ctx.beginPath(); 
    ctx.moveTo(256,256);                  
    ctx.lineTo(256,0);
    ctx.lineTo(0,0);
    ctx.closePath();
    ctx.fill(); bars()
    ctx.fillStyle="#0000ff"; for (i=0;i

但我只需要使用 HTML5 和 CSS3 脚本语言

【问题讨论】:

  • 在问题中添加您的代码,而不是链接到图像。我们无法调试图像。
  • 我知道它的样子,只存在于图像中,现在不创建任何代码,所以上面的图像将使用 css 创建此元素使用画布完全创建如下
  • 供以后参考,请不要重复旧问题,而是编辑原始问题。谢谢!
  • @RahulRaval:看到你对 vals 的回答的评论,我也在我的回答中添加了一个 SVG 版本。也许你会发现它有帮助。

标签: html css css-shapes


【解决方案1】:

使用 SVG:您正在寻找的整个效果

我知道您要求 HTML(5) + CSS(3),但您也可以使用 SVG path 元素来产生这种效果,如下面的 sn-p 所示。 (注意:这使用 SVG 动画,与 CSS 动画相比,它的浏览器支持可能不同。)

svg {
  height: 150px;
  width: 150px;
  stroke: black;
}
#blue {
  stroke: blue;
  stroke-width: 10;
}
svg polygon {
  fill: blue;
}
#white {
  stroke: white;
  stroke-width: 10;
}
#icon {
  fill: transparent;
}
<svg viewBox='0 0 100 100'>
  <defs>
    <clipPath id='clipper' clipPathUnits='objectBoundingBox'>
      <path d='M0,0 1,0 1,1 0,0z'>
        <animate attributeType="XML" attributeName="d" from="M0,0 1,0 1,1 0,0z" to="M0,0 1,0 1,1 0,1z" dur="1s" begin="icon.mouseover" fill="freeze" />
        <animate attributeType="XML" attributeName="d" from="M0,0 1,0 1,1 0,1z" to="M0,0 1,0 1,1 0,0z" dur="1s" begin="icon.mouseout" fill="freeze" />
      </path>
    </clipPath>
    <g id='lines'>
      <line x1='20' y1='30' x2='80' y2='30' />
      <line x1='20' y1='50' x2='80' y2='50' />
      <line x1='20' y1='70' x2='80' y2='70' />
    </g>
  </defs>
  <use xlink:href='#lines' id='blue' />
  <g clip-path='url(#clipper)'>
    <polygon points='0,0 0,100 100,100 100,0' />
    <use xlink:href='#lines' id='white' />
  </g>
  <g>
    <polygon points='0,0 0,100 100,100 100,0' id='icon' />
  </g>
</svg>

以下是问题的答案 - 如何用动画将三角形变成正方形

使用边框:

您可以使用border 来完成此操作,如下面的 sn-p 所示。最初只有右侧和顶部边框具有蓝色,但在悬停时,我们将颜色设置为所有边框边。此方法非常简单,适用于所有浏览器(包括 IE8),但您不能直接向此 div 添加内容(因为这样做会影响三角形),因此您必须将内容放在形状顶部使用定位或使用伪元素设置形状。

.shape{
  height: 0px;
  width: 0px;
  border: 50px solid transparent;
  border-color: blue blue transparent transparent;
  transition: all 1s;
}
.shape:hover{
  border-color: blue;
}
&lt;div class='shape'&gt;&lt;/div&gt;

使用变换:

您可以在伪元素上添加rotate 变换,在父元素上设置overflow: hidden 以生成三角形,然后在悬停时反转/取消变换。

.shape {
  position: relative;
  height: 100px;
  width: 100px;
  overflow: hidden;
}
.shape:after {
  position: absolute;
  content: '';
  height: calc(100% * 1.414); /* using Pythogras theorem */
  width: calc(100% * 1.414); /* using Pythogras theorem */
  transform: rotate(-45deg);
  transform-origin: left top;
  background: blue;
  transition: all 1s;
}
.shape:hover:after {
  transform: rotate(0deg);
}
&lt;div class='shape'&gt;&lt;/div&gt;

如果您希望避免像之前的 sn-p 那样计算 heightwidth,也可以使用 skewX 变换而不是 rotate 变换。

.shape {
  position: relative;
  height: 100px;
  width: 100px;
  overflow: hidden;
}
.shape:after {
  position: absolute;
  content: '';
  height: 100%;
  width: 100%;
  transform: skewX(45deg);
  transform-origin: left top;
  background: blue;
  transition: all 1s;
}
.shape:hover:after {
  transform: skewX(0deg);
}
&lt;div class='shape'&gt;&lt;/div&gt;

使用渐变:

您可以使用线性渐变创建一个三角形,然后在悬停时通过将background-size 加倍将其变成一个正方形。

.shape{
  height: 100px;
  width: 100px;
  background: linear-gradient(to bottom left, blue 49.5%, transparent 50.5%);
  background-position: 100% 0%;
  background-size: 100% 100%;
  transition: all 1s;
}
.shape:hover{
  background-size: 200% 200%; /* just double the background size on hover */
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class='shape'></div>

【讨论】:

  • 好答案 :) 真的很棒。
【解决方案2】:

尽管 Harry 给出了完整的答案,但还是忍不住用 OP 中的图片建议的另一种方法发布答案。

让我们使用混合模式,看看可以实现什么(但支持更有限)

.test {
    width: 200px;
    height: 200px;
    background-color: white;
    display: inline-block;
    background-image: linear-gradient(blue, blue), linear-gradient(blue, blue), linear-gradient(blue, blue);
    background-size: 100px 30px;
    background-repeaT: no-repeat;
    background-position: center 30px, center center, center 140px;
    border: solid 1px black;
    position: relative;
    overflow: hidden;
}

.test:after {
  content: "";
  position: absolute;
  left: 0px;
  top: 0px;
  width: 100%;
  height: 100%;
  background: linear-gradient(45deg, yellow 50%, transparent 50%);
  mix-blend-mode: difference;
  transition: all 1s;
}

.one:after {
  transform-origin: bottom right;
}

.one:hover:after {
  transform: rotate(-45deg);
}

.two:hover:after {
  opacity: 0;
}

.three:after {
  background: none;
  box-shadow: -1000px 1000px 0px 1000px yellow;
  transform-origin: top left;
  transform: rotate3d(1,1,0,87deg);
}

.three:hover:after {
  transform: rotate3d(1,1,0,0deg);
}
<div class="test one"></div>
<div class="test two"></div>
<div class="test three"></div>

第三个有点棘手,而且不是很完美。但你明白了。

【讨论】:

  • 不错的@vals。我也想涵盖混合模式,但因为答案变得太长(好像还没有:P)而将其忽略。我将标题解释为更多我需要三角形到正方形动画/过渡
  • @Harry 是的,我同意...但是在这种情况下很少看到使用混合模式,我认为发布一个显示它如何工作的答案会很好。没有多少人知道如何将蓝色换成白色,将白色换成蓝色......
  • @Vals 它是完美的第一个对我有用,但我修改它们仍然不是有效的输出,我需要第一个,但是像上面那样使用 svg throught 以这种方式创建效果的悬停效果
  • 如果我理解正确,您希望旋转中心位于右下角。我已经编辑了 sn-p。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
  • 1970-01-01
  • 2012-05-21
  • 1970-01-01
  • 2016-04-12
  • 2013-12-28
相关资源
最近更新 更多