伪元素:不显示后

时间:2019-05-13 14:24:43

标签: javascript html css pseudo-element

如您所见,我要在画布的中心穿过一条线,因此我决定使用after元素!正如控制台屏幕截图中所见到的蜜蜂一样,它正在渲染,但是由于任何原因它都不会显示!我不知道自己在做什么错!谢谢!这是我的Code Pen

代码:

function start() {  
  var canvas = $("canvas");
  console.log(canvas);
  canvas.each(function(index, canvas) {
      var context = canvas.getContext("2d"); 
      canvas.width = $(".box").eq(index).width();
      canvas.height = $(".box").eq(index).height();
      context.clearRect(0, 0, canvas.width, canvas.height);
      drawCurves(context, step);
      step += 1;
  });
  requestAnimationFrame(start);
}
var step = -1;
function drawCurves(ctx, step) {
    var width = ctx.canvas.width;
    var height = ctx.canvas.height;
    ctx.lineWidth = 2;
    
  for (i = 0; i < 4 ; i++) {    
    var x = 0;
    var y = 0;
    ctx.beginPath();
    if (i === 0 ) {
      ctx.strokeStyle = "red";
      var amplitude = 20;
     var frequency = height / (2 * Math.PI); 
      console.log(i, frequency);
      ctx.save();
  ctx.translate(-amplitude * Math.sin(step / frequency), 0);
     }  if ( i === 1) {
         ctx.strokeStyle = "blue";
       var amplitude = 30;
      var frequency = (height / (2 * Math.PI));
      console.log(i, frequency);
       ctx.save();
  ctx.translate(-amplitude * Math.sin(step / frequency), 0);
     }  if ( i === 2) {
          ctx.strokeStyle = "green";
       var amplitude = 40;
       var frequency = height / (2 * Math.PI) ;
      console.log(i, frequency);  
       ctx.save();
  ctx.translate(-amplitude * Math.sin(step / frequency), 0);
     }  if (i === 3) {
       ctx.strokeStyle = "yellow";
       var amplitude = 50;
      var frequency = height / (2 * Math.PI) ;
      console.log(i, frequency);
       ctx.save();
  ctx.translate(-amplitude * Math.sin(step / frequency), 0);
     }
  
  while (y < height ) {
    x = (width / 2) + (amplitude * Math.sin((y + step) / frequency)) ;
    ctx.lineTo(x, y);
    y++;
  }
  ctx.stroke();
  ctx.restore();
}
  }


$(document).ready(function() {
  start();
})
canvas {
  background-color: wheat;
  position: absolute;
   &:after {
            content: '';
            display: block;
            position: absolute;
            background-color: #EA777A;
            width: 20px;
            height: 100%;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
        }
}
.box {
width: 500px;
 height: 2000px;
  border: solid;
}

.box.t {
width: 500px;
 height: 200px;
  border: solid;
}
<!DOCTYPE html>
<html>

<head>
</head>

<body>

  <div class="box t">
   <canvas id="canvas"></canvas>
</div>
  <div class="box">
       <canvas id="canvas"></canvas>
    </div>

</body>

</html>

呈现的代码的屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:3)

画布不能包含::after。您可以将其包装在<div>中,然后给它::after

相关问题