Css 给 Element + Before 添加渐变

标签 css tooltip gradient

我正在使用 CSS 绘制工具提示框。 现在我想对包括三 Angular 形在内的整个盒子应用渐变。但是渐变总是只应用于框。

我试图将渐变放入 after 元素中,但这也不起作用。我只想用一种颜色给三 Angular 形上色,但我也需要能够将它移动到其他地方。

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

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>

最佳答案

所以我的解决办法是用canvas来画。

enter image description here

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");

关于Css 给 Element + Before 添加渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40328248/

相关文章:

javascript - 固定标题元素在窗口上移动减少

css - 背景渐变 - 填充页面,没有滚动条或重复

Chrome 中的 CSS 渐变条纹

css - Firefox 和 Chrome 中页脚的可见水平滚动条/CSS 问题

html - 对齐一个 div 的多个嵌套 div。 (中心)

html - 居中工具提示?

c# - Winform自动省略号宽度

css - 覆盖 highcharts-tooltip 中的表格单元格背景颜色

python - 如何计算 tf.identity 的梯度

html - 当我将图像与跨度内联时,为什么我的图像会被下推?