【问题标题】:Button with right arrow - transition带右箭头的按钮 - 过渡
【发布时间】:2016-09-09 09:21:57
【问题描述】:

借助 css 转换和伪元素,我制作了带有箭头形状的右边框的 css 按钮。现在我正在尝试在悬停时向背景添加过渡,但意识到存在问题,在按钮与伪元素重叠的地方,过渡期间颜色不同。看下面,尝试悬停按钮:

body {
  background: gray;
background-image: linear-gradient(30deg, #445 12%, transparent 12.5%, transparent 87%, #445 87.5%, #445),
linear-gradient(150deg, #445 12%, transparent 12.5%, transparent 87%, #445 87.5%, #445),
linear-gradient(30deg, #445 12%, transparent 12.5%, transparent 87%, #445 87.5%, #445),
linear-gradient(150deg, #445 12%, transparent 12.5%, transparent 87%, #445 87.5%, #445),
linear-gradient(60deg, #99a 25%, transparent 25.5%, transparent 75%, #99a 75%, #99a), 
linear-gradient(60deg, #99a 25%, transparent 25.5%, transparent 75%, #99a 75%, #99a);
background-size:80px 140px;
background-position: 0 0, 0 0, 40px 70px, 40px 70px, 0 0, 40px 70px;
  font-family: sans-serif;
}
.more-skewed {
  display: inline-block;
  position: relative;
  color: white;
  text-decoration: none;
  border: solid 2px white;
  border-right: none;
  box-sizing: border-box;
  padding: 15px 40px;
  letter-spacing: 3px;
  transition: all 1s ease-out;
}

.more-skewed:before {
  content: '';
  position: absolute;
  left: 100%;
  margin-left: -10px;
  top: -2px;
  bottom: 50%;
  border-right: solid 2px white;
  transform: skewX(25deg);
  width: 15px;
  background: inherit;
}

.more-skewed:after {
  content: '';
  position: absolute;
  left: 100%;
  margin-left: -10px;
  bottom: -2px;
  top: 50%;
  border-right: solid 2px white;
  transform: skewX(-25deg);
  width: 15px;
  background: inherit;
}

.more-skewed:hover {
  color: black;
	background: white;
}
<a class="more-skewed" href="#">MORE</a>

有可能解决这个问题吗? JSfiddle - https://jsfiddle.net/k9jhuc34/
(按钮最初是透明的,并且位于图像背景之上。)

【问题讨论】:

  • 使用 Bootstrap,事情变得简单且响应迅速。
  • 检查此网址:codecanyon.net/item/pure-css-buttons-with-arrow/… 希望对您有所帮助:)
  • 这是一个已知问题,因为(粗略地说)您在过渡期间将半透明白色放在另一个半透明白色之上,因此重叠区域自然会显得更白。您可以在this fiddle 中执行类似操作,但您会看到悬停区域(或点击区域)并不完全位于右侧的形状内。
  • @Harry 谢谢,我喜欢这个解决方案 :) 悬停区域是矩形的......但我认为它比过渡期间的那个奇怪的数字更好!如果您将其发布为答案,我会接受。
  • @SunilGehlot 谢谢...有使用纯色背景,我需要透明背景:)

标签: css css-transitions css-shapes css-transforms


【解决方案1】:

如果链接所在的背景只是所提供示例中的纯色,您可以使用几个三角形来实现这一点,如下所示:

*{box-sizing:border-box;font-family:sans-serif;}
body{
    background:#333;
}
a{
    border:2px solid #fff;
    border-right:0;
    color:#fff;
    display:inline-block;
    letter-spacing:3px;
    line-height:50px;
    padding:0 40px;
    position:relative;
    text-decoration:none;
    text-transform:uppercase;
    transition:background 1s ease-out,color 1s ease-out;
}
a::before{
    border-top:27px solid transparent;
    border-left:14px solid #fff;
    border-bottom:27px solid transparent;
    content:"";
    position:absolute;
    right:-14px;
    top:-2px;
}
a::after{
    border-top:25px solid transparent;
    border-left:12px solid #333;
    border-bottom:25px solid transparent;
    content:"";
    position:absolute;
    right:-12px;
    top:0;
    transition:opacity 1s ease-out;
}
a:hover{
    background:#fff;
    color:#333;
}
a:hover::after{
    opacity:0;
}
<a href="#">More</a>

我也想出了以下内容,但它有点“笨拙”,而且箭头并不是您想要的。我决定无论如何都要编辑它,以防将来对其他人有用。

*{box-sizing:border-box;font-family:sans-serif;}
body{
    background:#333;
}
a{
    border:2px solid #fff;
    border-right:0;
    color:#fff;
    display:inline-block;
    letter-spacing:3px;
    line-height:50px;
    padding:0 40px;
    position:relative;
    text-decoration:none;
    text-transform:uppercase;
    transition:background 1s ease-out,color 1s ease-out;
}
a::before{
    border-right:2px solid #fff;
    border-top:2px solid #fff;
    content:"";
    height:36px;
    position:absolute;
    right:-19px;
    top:6px;
    transform:rotate(45deg);
    width:36px;
}
a::after{
    background:linear-gradient(-135deg,#fff 50%,transparent 50%);
    content:"";
    height:34px;
    opacity:0;
    position:absolute;
    right:-17px;
    top:8px;
    transform:rotate(45deg);
    transition:opacity 1s ease-out;
    width:34px;
}
a:hover{
    background:#fff;
    color:#333;
}
a:hover::after{
    opacity:1;
}
<a href="#">More</a>

【讨论】:

  • 不,它在图像背景上:)
  • 您能否更新您的问题(如果可能,还包括示例)以包含该信息?
【解决方案2】:

正如我的评论中所述,这是意料之中的,因为粗略地说,会发生一个半透明的白色层被放置在有重叠区域的另一个此类层的顶部,因此,该区域很快看起来比其他人更白。

使用倾斜变换:

一个解决方案类似于下面的 sn-p,但它使悬停/命中区域成为一个矩形。我在这里所做的是让伪元素创建整个形状(左边框除外),并且父元素上的 overflow-hidden 可以防止出现左侧不需要的倾斜部分。

body {
  background: gray;
  font-family: sans-serif;
}
.more-skewed {
  display: inline-block;
  position: relative;
  color: white;
  text-decoration: none;
  border-left: solid 2px white;
  box-sizing: border-box;
  padding: 15px 40px;
  letter-spacing: 3px;
  transition: all 1s ease-out;
  overflow: hidden;
}
.more-skewed:before {
  content: '';
  position: absolute;
  left: -2px;
  top: 0px;
  bottom: 50%;
  border-right: solid 2px white;
  border-top: solid 2px white;
  transform: skewX(25deg);
  transform-origin: right bottom;
  width: 100%;
  transition: all 1s ease-out;
  z-index: -1;
}
.more-skewed:after {
  content: '';
  position: absolute;
  left: -2px;
  bottom: 0px;
  top: 50%;
  border-right: solid 2px white;
  border-bottom: solid 2px white;
  transform: skewX(-25deg);
  transform-origin: right top;
  width: 100%;
  transition: all 1s ease-out;
  z-index: -1;
}
.more-skewed:hover {
  color: black;
}
.more-skewed:hover:before,
.more-skewed:hover:after {
  background: white;
}
<a class="more-skewed" href="#">MORE</a>

使用透视旋转:

另一种解决方案是使用旋转变换并添加一点透视。通过为transform-origin 设置适当的值,我们可以使它产生一个梯形,该梯形可以放置在适当的位置以产生一个箭头。在这里,悬停/点击区域保留在形状内,但这种方法仅在文本很小且静态的情况下才有用。如果文本变长,形状右侧的斜率会变得更加平缓而不是陡峭(当然,我们可以更改变换以使其看起来更好,但如果我们必须不断更改它并不是很有用经常)。

.more-skewed {
  display: inline-block;
  position: relative;
  color: white;
  text-decoration: none;
  border: solid 2px white;
  border-right: none;
  box-sizing: border-box;
  padding: 16px 40px;
  letter-spacing: 3px;
  margin: 10px;
}
.more-skewed:before,
.more-skewed:after {
  position: absolute;
  content: '';
  height: 50%;
  width: 100%;
  left: 0;
  border-right: 3px solid white;
  transition: all 1s ease-out;
}
.more-skewed:before {
  top: -2px;
  border-top: 2px solid white;
  transform-origin: left top;
  transform: perspective(100px) rotateX(30deg);
}
.more-skewed:after {
  bottom: -2px;
  border-bottom: 2px solid white;
  transform-origin: left bottom;
  transform: perspective(100px) rotateX(-30deg);
}
.more-skewed:hover {
  color: black;
}
.more-skewed:hover:before,
.more-skewed:hover:after {
  background: white;
}
.more-skewed span {
  position: relative;
  z-index: 1;
}
body {
  background: gray;
  font-family: sans-serif;
}
<a class="more-skewed" href="#">
  <span>MORE</span>
</a>
<br/>
<a class="more-skewed" href="#">
  <span>MORE LONGER TEXT</span>
</a>

(反馈是上面的sn-p在IE中坏了。我有时间会解决的。)


这是我在玩的时候想出的另一个版本。它有点像背景滑动填充效果,将悬停/点击区域保持在形状内,但由于浏览器呈现渐变的方式,它不是很干净。想把它留在这里,以防万一你觉得它有用。

body {
  background: gray;
  font-family: sans-serif;
}
.more-skewed {
  display: inline-block;
  position: relative;
  color: white;
  text-decoration: none;
  border: solid 2px white;
  border-right: none;
  box-sizing: border-box;
  padding: 15px 40px;
  letter-spacing: 3px;
  transition: all 1s linear;
  background: linear-gradient(245deg, transparent 9px, white 10px), linear-gradient(295deg, transparent 9px, white 10px);
  background-size: 0% 50%;
  background-position: top right, bottom right;
  background-repeat: no-repeat;
}
.more-skewed:before {
  content: '';
  position: absolute;
  left: 100%;
  margin-left: -7px;
  top: -2px;
  bottom: 50%;
  transform: skewX(25deg);
  width: 15px;
  background: linear-gradient(white, white);
  background-size: 3px 100%;
  background-repeat: no-repeat;
  background-position: top right;
  transition: all 1s 1s linear;
}
.more-skewed:after {
  content: '';
  position: absolute;
  left: 100%;
  margin-left: -7px;
  bottom: -2px;
  top: 50%;
  transform: skewX(-25deg);
  width: 15px;
  background: linear-gradient(white, white);
  background-size: 3px 100%;
  background-repeat: no-repeat;
  background-position: top right;
  transition: all 1s 1s linear;
}
.more-skewed:hover {
  color: black;
  background-size: 100% 50%;
  transition: all 1s 1s linear;
}
.more-skewed:hover:before,
.more-skewed:hover:after {
  color: black;
  background-size: 100% 100%;
  transition: all 1s linear;
}
&lt;a class="more-skewed" href="#"&gt;MORE&lt;/a&gt;

【讨论】:

  • 谢谢!第二种解决方案很聪明,但在右边界附近有一条非常小的半透明线。即使我用 box-shadow 替换了渐变:jsfiddle.net/ahoc2Loc/1。我猜这是转换引起的错误。
  • 其实是边框和渐变@Julia。这就是我在回答中提到的渲染问题。我会试试看有没有办法解决。
【解决方案3】:

请检查下面的箭头代码transition,希望对您有所帮助。

谢谢

HTML

<a class="arrow_box">
MORE
</a>

CSS

body {
  background: gray;
  font-family: sans-serif;
}
.arrow_box {
    position: relative;
    background: gray;
    border: 2px solid #ffffff;
  display:inline-block;
  padding: 15px 40px;
  letter-spacing: 3px;
  color:#FFF;
  transition:0.4s;
}
.arrow_box:after, .arrow_box:before {
    left: 100%;
    top: 50%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
  transition:0.4s;
}

.arrow_box:after {
    border-color: rgba(128, 128, 128, 0);
    border-left-color: gray;
  border-width: 24px;
  margin-top: -24px;
}
.arrow_box:before {
    border-color: rgba(255, 255, 255, 0);
    border-left-color: #ffffff;
    border-width: 26px;
  margin-top: -26px;
  margin-left: 2px;
}
.arrow_box:hover{
  color:#000;
  background:#FFF;
}
.arrow_box:hover:after{
  border-left-color: #FFF;
}

查看fiddle here

【讨论】:

  • 谢谢,但在所需的设计中,按钮最初是透明的,只有白色边框,因为它被放置在图像背景上
猜你喜欢
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-13
  • 1970-01-01
  • 2019-01-25
相关资源
最近更新 更多