javascript - 如何获取变量?

标签 javascript svg

我想在这个结论中插入一个变量 ttl:'rotate('".c1", 150, 100, "blue", 60, "30 628", -90);"而不是数字 628

将变量插入到生成中后,代码停止工作

我就是这样插入的。

1) rotate(".c1", 150, 150, 100, "blue", 60, "30 ttl", -90);
2) rotate(".c1", 150, 150, 100, "blue", 60, "30 `${ttl}`", -90);
3) rotate(".c1", 150, 150, 100, "blue", 60,  `30  ${ttl}`, -90);

之后,代码就不起作用了

在代码运行后,应该会得到一个片段,我将展示如何将其转换为 svg

<svg xmlns="http://www.w3.org/2000/svg"
     width="300" 
     height="300" 
     viewBox="0 0 300 300" >
  <circle 
          fill="none" 
          stroke="red" 
          stroke-width="30" 
          cx="150" 
          cy="150" 
          r="100"
          stroke-dasharray="30 628"/>
</svg>

这就是我期望得到的结果。

如何解决将变量放入函数中的情况?

let obj = document.querySelector("circle");
let ttl = Math.round(obj.getTotalLength());

function rotate(obj, cx, cy, r, stroke, w, da, rad) {
  let item = document.querySelector(obj);

  this.cx = item.setAttribute("cx", cx);
  this.cy = item.setAttribute("cy", cy);
  this.r = item.setAttribute("r", r);
  this.stroke = item.setAttribute("stroke", stroke);
  this.w = item.setAttribute("stroke-width", w);
  this.da = item.setAttribute("stroke-dasharray", da);
  this.rad = item.style.transform = `rotate(${rad}deg)`;
}

rotate(".c1", 150, 150, 100, "blue", 60, "30 628", -90);
svg {
  border: 1px solid red;
}

circle {
  tranform-box: fill-box;
  transform-origin: center;
  fill: none;
}
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300" viewBox="0 0 300 300">
  <circle class="c1" />
</svg>

如何在完成的函数中插入变量,如何替换变量ttl上的628?

最佳答案

您的第三个版本(使用 template literal )应该可以工作。如果没有,您应该检查您使用的浏览器是否支持它们(任何版本的 Internet Explorer 均不支持),或者 ttl 的计算是否产生了您期望的结果。如果您的浏览器不支持模板文字,您可以使用

'30  ' + ttl

在它的位置。

let ttl = 628;
console.log(`30  ${ttl}`);
console.log('30  ' + ttl);

您的代码在 Firefox 74.0 中运行良好:

let obj = document.querySelector("circle");
let ttl = Math.round(obj.getTotalLength());

function rotate(obj, cx, cy, r, stroke, w, da, rad) {
  let item = document.querySelector(obj);

  this.cx = item.setAttribute("cx", cx);
  this.cy = item.setAttribute("cy", cy);
  this.r = item.setAttribute("r", r);
  this.stroke = item.setAttribute("stroke", stroke);
  this.w = item.setAttribute("stroke-width", w);
  this.da = item.setAttribute("stroke-dasharray", da);
  this.rad = item.style.transform = `rotate(${rad}deg)`;
}

rotate(".c1", 150, 150, 100, "blue", 60, `30 $ttl`, -90);
svg {
  border: 1px solid red;
}

circle {
  tranform-box: fill-box;
  transform-origin: center;
  fill: none;
}
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300" viewBox="0 0 300 300" >
  <circle class="c1" />
</svg>


<svg xmlns="http://www.w3.org/2000/svg"
     width="300" 
     height="300" 
     viewBox="0 0 300 300" >
  <circle 
          fill="none" 
          stroke="red" 
          stroke-width="30" 
          cx="150" 
          cy="150" 
          r="100"
          stroke-dasharray="30 628"/>
</svg>

或者这应该适用于 IE 或早期版本的 Firefox 等。

let obj = document.querySelector("circle");
let ttl = Math.round(obj.getTotalLength());

function rotate(obj, cx, cy, r, stroke, w, da, rad) {
  let item = document.querySelector(obj);

  this.cx = item.setAttribute("cx", cx);
  this.cy = item.setAttribute("cy", cy);
  this.r = item.setAttribute("r", r);
  this.stroke = item.setAttribute("stroke", stroke);
  this.w = item.setAttribute("stroke-width", w);
  this.da = item.setAttribute("stroke-dasharray", da);
  this.rad = item.style.transform = `rotate(${rad}deg)`;
}

rotate(".c1", 150, 150, 100, "blue", 60, '30 ' + ttl, -90);
svg {
  border: 1px solid red;
}

circle {
  tranform-box: fill-box;
  transform-origin: center;
  fill: none;
}
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300" viewBox="0 0 300 300" >
  <circle class="c1" />
</svg>


<svg xmlns="http://www.w3.org/2000/svg"
     width="300" 
     height="300" 
     viewBox="0 0 300 300" >
  <circle 
          fill="none" 
          stroke="red" 
          stroke-width="30" 
          cx="150" 
          cy="150" 
          r="100"
          stroke-dasharray="30 628"/>
</svg>

关于javascript - 如何获取变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60784485/

相关文章:

html - 存储 svg 图标的最佳实践?

css - 字体定义的最小 CSS3 兼容性规则是什么?

javascript - JavaScript for 循环初学者

javascript - typescript 对象 : How do I restrict the keys to specific strings?

javascript - CSS 使 div 在页面加载时处于事件状态

javascript - 未捕获的类型错误 : Cannot read property 'setAttribute' of null

javascript - Nodejs运行js文件的流程

javascript - 在数组上使用 hasOwnProperty()

svg - 如何使用 SVG 过滤器在 SVG 中创建 Material 设计阴影

javascript - 如何定位嵌套并包含在父 svg 中的 svg