【问题标题】:Apply css :after with javascript (no jquery)应用 css :after with javascript (no jquery)
【发布时间】:2019-10-25 06:26:50
【问题描述】:

我发现这段代码可以在 div 中创建对角线。

https://jsbin.com/tefakagohi/edit?html,css,output

我正在尝试在 JS 中制作一个行生成器。但是我想使用 js 完整地创建这个对象...

从 document.createElement() 等开始...

我这里有一些代码:

function createLine(name, x1, y1, x2, y2, color){
var rectX1;
var rectY1;
var rectX2;
var rectY2;

//Line direction
// 0 = top left -> bottom right
// 1 = top right -> bottom left
// 2 = bottom left -> top right
// 3 = bottom right -> top left
var lineDirection = null;

//Find the direction
if (x1 < x2 && y1 < y2) {
    lineDirection = 0;
    rectX1 = x1;
    rectY1 = y1;
    rectX2 = x2;
    rectY2 = y2;
}
if (x2 < x1 && y1 < y2) {
    lineDirection = 1;
    rectX1 = x2;
    rectY1 = y1;
    rectX2 = x1;
    rectY2 = y2;

}
if (x1 < x2 && y2 < y1) {
    lineDirection = 2;
    rectX1 = x1;
    rectY1 = y2;
    rectX2 = x2;
    rectY2 = y1;
}
if (x2 < x1 && y2 < y1) {
    lineDirection = 3;
    rectX1 = x2;
    rectY1 = y2;
    rectX2 = x1;
    rectY2 = y1;
}

//Create a div with the 2 points
var div = document.createElement("div");
div.id = name;
div.style.position = "absolute";
div.style.zIndex = 5;
div.style.left = rectX1 + "px";
div.style.top = rectY1 + "px";
div.style.right = (window.innerWidth - rectX2) + "px";
div.style.bottom = (window.innerHeight - rectY2) + "px";

div.style.backgroundColor = color;

//Add the div to the body
document.body.appendChild(div);
}

这有点多,但现在我想创建对角线。

是的,我知道我需要一些公式来计算线的度数和长度,但现在我只想知道如何只用 js 创建对角线。

非常感谢, 朱尔斯

【问题讨论】:

  • “[完全]使用js”是什么意思?这听起来像是XY problem。为什么不能使用已经找到的css解决方案?
  • 和@Ruzihm 的想法一样,你问题中的标题和愿望并不完全有意义..
  • 你可以用javascript完成实际的线条画,你不需要依赖页面上的css或元素。但是由于您的问题似乎打算使用 javascript 样式 - 您可能正在寻找的解决方案是 translate 或 transform..
  • 看看为什么这是使用 JS 和 CSS 的不同之处:stackoverflow.com/a/29263753/8031815。但关键的是,Javascript 没有对伪元素的 DOM 访问,所以样式化它们只能通过 CSS 来完成。

标签: javascript html css


【解决方案1】:

你可以很容易地用一个背景层和一行JS代码来注入你想要的变量:

div.style.backgroundImage="linear-gradient(to top right,transparent calc(50% - 2px),blue,transparent calc(50% + 2px))";

完整代码:

function createLine(name, x1, y1, x2, y2, color) {
  var rectX1;
  var rectY1;
  var rectX2;
  var rectY2;

  //Line direction
  // 0 = top left -> bottom right
  // 1 = top right -> bottom left
  // 2 = bottom left -> top right
  // 3 = bottom right -> top left
  var lineDirection = null;

  //Find the direction
  if (x1 < x2 && y1 < y2) {
    lineDirection = 0;
    rectX1 = x1;
    rectY1 = y1;
    rectX2 = x2;
    rectY2 = y2;
  }
  if (x2 < x1 && y1 < y2) {
    lineDirection = 1;
    rectX1 = x2;
    rectY1 = y1;
    rectX2 = x1;
    rectY2 = y2;

  }
  if (x1 < x2 && y2 < y1) {
    lineDirection = 2;
    rectX1 = x1;
    rectY1 = y2;
    rectX2 = x2;
    rectY2 = y1;
  }
  if (x2 < x1 && y2 < y1) {
    lineDirection = 3;
    rectX1 = x2;
    rectY1 = y2;
    rectX2 = x1;
    rectY2 = y1;
  }

  //Create a div with the 2 points
  var div = document.createElement("div");
  div.id = name;
  div.style.position = "absolute";
  div.style.zIndex = 5;
  div.style.left = rectX1 + "px";
  div.style.top = rectY1 + "px";
  div.style.right = (window.innerWidth - rectX2) + "px";
  div.style.bottom = (window.innerHeight - rectY2) + "px";

  div.style.backgroundImage="linear-gradient(to top right,transparent calc(50% - 2px),blue,transparent calc(50% + 2px))";
  div.style.backgroundColor = color;

  //Add the div to the body
  document.body.appendChild(div);
}
createLine("aa", 5, 5, 200, 100, "red")

【讨论】:

  • 一种有趣的方法,但我很惊讶它是这里的第一个建议和答案。为什么要在旋转元素时使用背景图像(甚至是通过程序生成的)?
  • 好吧..也许我的问题超出了您的回答和建议的范围。
  • @BrettCaswell 如果旋转元素更容易,那么我会使用它,但我认为您不能创建一条线,计算角度、长度,使用一行代码正确放置它。跨度>
  • 好吧..你不是在一行代码中做的,或者根本没有这个建议。我认为这是你建议的一个好方法,但显然其余的样式会必须修改\更改以适应这个概念,并且他们必须考虑角度、长度等。也就是说,您添加这一行会改变 div 所代表的性质。它原本是一条线,您将其更改为边界框。
  • @BrettCaswell 没有要考虑的角度或长度,您只需要设置颜色和厚度(我在代码中使用的 2px)。更改 div 的尺寸,该行将跟随,因此它只是一行代码,您只需要最多注入 2 个变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 2012-02-28
  • 2019-08-12
  • 2011-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多