指向按钮的CSS三角形

时间:2018-07-16 11:43:31

标签: javascript html css

我从cssarrowplease获得了以下标记:

.arrow_box {
	position: relative;
	background: #88b7d5;
	border: 2px solid #c2e1f5;
  margin-top: 20px;
}
.arrow_box:after, .arrow_box:before {
	bottom: 100%;
	left: 50%;
	border: solid transparent;
	content: " ";
	height: 0;
	width: 0;
	position: absolute;
	pointer-events: none;
}

.arrow_box:after {
	border-color: rgba(136, 183, 213, 0);
	border-bottom-color: #88b7d5;
	border-width: 10px;
	margin-left: -10px;
}
.arrow_box:before {
	border-color: rgba(194, 225, 245, 0);
	border-bottom-color: #c2e1f5;
	border-width: 13px;
	margin-left: -13px;
}
<input type="submit" id="btn1" />
<input type="submit" id="btn2" />
<input type="submit" id="btn3" />

<div class="arrow_box">My content goes here</div>

我在UI方面做得不好,想知道采用什么方法,以便当我将div悬停在它们上时,div的三角形指针指向每个按钮的中间吗?可以仅通过HTML / CSS完成此操作,还是需要应用JavaScript?

1 个答案:

答案 0 :(得分:0)

您只需要更改鼠标悬停时箭头的左侧位置(也可以在非悬停时将其隐藏)

.arrow_box {
  position: relative;
  background: #88b7d5;
  border: 2px solid #c2e1f5;
  margin-top: 20px;
}

.arrow_box:after,
.arrow_box:before {
  bottom: 100%;
  left: 50%;
  border: solid transparent;
  content: " ";
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
  display: none;                  /* add this so arrow is hidden if no button is hovered */
}

.arrow_box:after {
  border-color: rgba(136, 183, 213, 0);
  border-bottom-color: #88b7d5;
  border-width: 10px;
  margin-left: -10px;
}

.arrow_box:before {
  border-color: rgba(194, 225, 245, 0);
  border-bottom-color: #c2e1f5;
  border-width: 13px;
  margin-left: -13px;
}

#btn1:hover~.arrow_box:after,  /* this means whilst button 1 is hovered style the after of any following siblings with the class arrow box */
#btn1:hover~.arrow_box:before {
  display: block;
  left: 30px;                  /* these are examples only, if you want it dead in the middle of the button, I would give the button a width so you know what the middle is */
}

#btn2:hover~.arrow_box:after,
#btn2:hover~.arrow_box:before {
  display: block;
  left: 90px;
}

#btn3:hover~.arrow_box:after,
#btn3:hover~.arrow_box:before {
  display: block;
  left: 150px;
}
<input type="submit" id="btn1" />
<input type="submit" id="btn2" />
<input type="submit" id="btn3" />

<div class="arrow_box">My content goes here</div>