前后倾斜仅一侧以获得平箭头

时间:2019-01-02 14:28:31

标签: html css css3 css-transforms

我创建了一些虚拟的面包屑步骤。 第一个面包屑步骤需要在左侧具有正常的平边框,此操作已完成。 在悬停时,我需要为每个面包屑项目显示黑色边框。

但是唯一的问题是悬停,对于第一个面包屑步骤,我没有像预期的那样在左侧获得平坦的边框。

以下是悬停时的期望值:

enter image description here

这是我的代码:

body {
	padding: 0;
	margin: 0;
}

.breadcrumbs {
	background-color: white;
	width: 100%;
	padding: 0 1rem;
	font-size: 0;
	margin: 1rem;
}

.breadcrumbs .breadcrumb-step {
	display: inline-block;
	width: 12.33%;
	height: 5rem;
	text-decoration: none;
	color: black;	
}

.step-title {
	display: none;
}

.breadcrumb-step:before {
	content: '';
	display: block;
	transform: skew(30deg);
	border: 1px solid lightgrey;
	border-bottom: none;
	height: 50%;
	width: 100%;
}

.breadcrumb-step:after {
	content: '';
	display: block;
	transform: skew(-30deg);
	border: 1px solid lightgrey;
	border-top: none;
	height: 50%;
	width: 100%;
}

.breadcrumb-step:hover:before {
	border-color: black;
}

.breadcrumb-step:hover:after {
	border-color: black;
}

.breadcrumb-step:nth-child(1):before {
	transform: none;
	border-right: none;
}
.breadcrumb-step:nth-child(1):after {
	transform: none;
	border-right: none;
}

.breadcrumb-step:nth-child(1):hover:before {
	transform: skew(30deg);
	border-right: 1px solid black;
}

.breadcrumb-step:nth-child(1):hover:after {
	transform: skew(-30deg);
	border-right: 1px solid black;
}
/*
.breadcrumb-step:nth-child(1) {
	border: 1px solid lightgrey;
}

.breadcrumb-step:nth-child(1):before {
	transform: none;
	border: none;
}

.breadcrumb-step:nth-child(1):after {
	transform: none;
	border: none;
}
*/
<div class="breadcrumbs">
	<a class="breadcrumb-step" href="#"><span class="step-title">Home</span></a>
	<a class="breadcrumb-step" href="#"><span class="step-title">About</span></a>
	<a class="breadcrumb-step" href="#"><span class="step-title">Contact Us</span></a>
</div>

1 个答案:

答案 0 :(得分:4)

您可以调整转换原点,然后隐藏倾斜元素的溢出部分,并在悬停时更改第一个孩子的左边框:

body {
  margin: 0;
}

.breadcrumbs {
  background-color: white;
  width: 100%;
  padding: 0 1rem;
  font-size: 0;
  margin: 1rem;
}

.breadcrumbs .breadcrumb-step {
  display: inline-block;
  width: 12.33%;
  height: 5rem;
  text-decoration: none;
  color: black;
}
/*added this*/
.breadcrumbs .breadcrumb-step:first-child {
  width: 15.33%; 
  overflow:hidden;
  border-left:1px solid lightgrey;
}
/**/

.step-title {
  display: none;
}

.breadcrumb-step:before,
.breadcrumb-step:after {
  content: '';
  display: block;
  height: 50%;
  box-sizing:border-box;
  width: 100%;
  border: 1px solid lightgrey;

}
.breadcrumb-step:before {
  transform: skew(30deg);
  transform-origin:right bottom; /*added this*/
  border-bottom: none;
}

.breadcrumb-step:after {
  transform: skew(-30deg);
  transform-origin:right top; /*added this*/
  border-top: none;
}

/*added this*/
.breadcrumb-step:hover:before,
.breadcrumb-step:hover:after,
.breadcrumbs .breadcrumb-step:first-child:hover{
  border-color: black;
}
<div class="breadcrumbs">
  <a class="breadcrumb-step" href="#"><span class="step-title">Home</span></a>
  <a class="breadcrumb-step" href="#"><span class="step-title">About</span></a>
  <a class="breadcrumb-step" href="#"><span class="step-title">Contact Us</span></a>
</div>