Css在

时间:2016-10-30 10:44:24

标签: css tooltip gradient

我正在使用Css绘制工具提示框。 现在我想对包括三角形在内的整个框应用渐变。但渐变始终只适用于框。

我试图将渐变放入after元素,但这也不起作用。我只想用一种颜色对三角形进行着色,但我也需要将其移动到其他地方。

这不是一个重复的问题,因为在其他答案中,三角形只是渐变的最终颜色。但我希望渐变也适用于三角形。

enter image description here

以下是一个例子:

body {
  background-color: tomato;
}
#bubble {
  position: absolute;
  top: 50px;
  left: 50px;
  width: 154px;
  height: 140px;
  padding: 0px;
  background: #FFFFFF;
  -webkit-border-radius: 6px;
  -moz-border-radius: 6px;
  border-radius: 6px;
  text-align: center;
  font-size: 1em;
  font-weight: 500;
  background: -webkit-linear-gradient(rgba(223, 204, 206, 0.9) 0%, rgba(242, 144, 127, 1) 100%);
  background: -o-linear-gradient(rgba(223, 204, 206, 0.9) 0%, rgba(242, 144, 127, 1) 100%);
  background: linear-gradient(rgba(223, 204, 206, 0.9) 0%, rgba(242, 144, 127, 1) 100%);
  / filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#e6dfccce', endColorstr='#f2907f', GradientType=0);
}
#bubble:after {
  content: '';
  position: absolute;
  border-style: solid;
  border-width: 15px 16px 0px;
  border-color: #FFFFFF transparent;
  display: block;
  width: 0;
  z-index: 1;
  bottom: -15px;
  left: 59px;
}
#bubble > :first-child {
  margin-top: 19px;
}
.answerWrapper {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.answerWrapperText {
  width: 40px;
  height: 38px;
  color: #131313;
  border: solid 2px #131313;
}
.answerWrapper > :first-child {
  margin: 0 0 0 18px;
}
.answerWrapper > :nth-child(2) {
  margin: 0 18px 0 0;
}
.answerWrapperText p {
  margin: 0;
  margin-top: 9px;
}
<div id="bubble">
  <p>Have you
    <br>chosen wisely&#8239;?</p>
  <div class="answerWrapper">
    <div class="answerWrapperText">
      <p id="yes">yes</p>
    </div>
    <div class="answerWrapperText">
      <p id="no">no</p>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

所以我的解决方案是用画布绘制它。

enter image description here

&#13;
&#13;
var bubbleBackground;

function initCanvas () {
	bubbleBackground = document.createElement('canvas');
	bubbleBackground.id = "BubbleId";
	bubbleBackground.width = 184;
	bubbleBackground.height = 170;
	bubbleBackground.style.position = "absolute";
	document.body.appendChild(bubbleBackground);
}

function drawCanvas (drawDirection) {

	var ctx = document.getElementById('BubbleId').getContext("2d");

	//clear canvas before draw
	ctx.clearRect(0, 0, bubbleBackground.width, bubbleBackground.height);
	//set to draw normally
	ctx.globalCompositeOperation="source-over";

	var posX = 15;
	var posY = 15;
	ctx.roundRect(posX, posY, 154, 140, 6);
	ctx.fillStyle = 'rgba(0, 0, 0, 1)';
	ctx.fill();

	switch(drawDirection) {
    case "top":
        ctx.beginPath();
	    ctx.moveTo(106,15);
	    ctx.lineTo(78,15);
	    ctx.lineTo(92,3);
	    ctx.fill();
        break;
    case "right":
    	ctx.beginPath();
	    ctx.moveTo(169,71);
	    ctx.lineTo(181,85);
	    ctx.lineTo(169,99);
	    ctx.fill();        
        break;
    case "bottom":
        ctx.beginPath();
	    ctx.moveTo(78,155);
	    ctx.lineTo(106,155);
	    ctx.lineTo(92,167);
	    ctx.fill();
        break;
    case "left":
        ctx.beginPath();
	    ctx.moveTo(15,71);
	    ctx.lineTo(15,99);
	    ctx.lineTo(3,85);
	    ctx.fill();
        break;
    default:
        console.log("no arrow")
	} 

	//draw gradient
	ctx.globalCompositeOperation="source-in";
	var grd=ctx.createLinearGradient(0,0,0,170);
	grd.addColorStop(0, 'rgba(223,204,206,0.9)');
	grd.addColorStop(1, 'rgba(242,144,127,1)');

	ctx.fillStyle=grd;
	ctx.fillRect(0,0,184,170);

}

CanvasRenderingContext2D.prototype.roundRect = function (x, y, width, height, radius) {
  if (width < 2 * radius) radius = width / 2;
  if (height < 2 * radius) radius = height / 2;
  this.beginPath();
  this.moveTo(x + radius, y);
  this.arcTo(x + width, y, x + width, y + height, radius);
  this.arcTo(x + width, y + height, x, y + height, radius);
  this.arcTo(x, y + height, x, y, radius);
  this.arcTo(x, y, x + width, y, radius);
  this.closePath();
  return this;
}

initCanvas();
drawCanvas("right");
&#13;
&#13;
&#13;

答案 1 :(得分:1)

在这种情况下,您无法使用&#34;:&#34;之后使用边框解决方案,而是创建另一个渐变并旋转元素。

#bubble:after {
  content: '';
  position: absolute;
  /* border-style: solid; */
  /* border-width: 15px 16px 0px; */
  /* border-color: #FFFFFF transparent; */
  display: block;
  width: 0;
  z-index: 1;
  bottom: -10px;
  left: 0;
  right: 0;
  margin: auto;
  width: 20px;
  height: 20px;
  /* background: red; */
  transform: rotate(45deg);
  z-index: -1;
  background: linear-gradient(130deg, rgba(223, 204, 206, 0.9) 0%, rgba(242, 144, 127, 1) 50%);
}

以下是完整示例:Codepen