javascript - SVG带文本和 div 填充宽度,带过渡
问题描述
我正在这里处理 SVG 我想要一个矩形来在 div 内添加文本并用过渡颜色填充我曾尝试过这样
<g>
<rect width="25%" height="100%" fill="blue"></rect>
<rect width="100%" height="100%" fill="none"></rect>
<text x="0" y="50" font-size="35" fill="green">text here</text>
</g>
仍然没有来,对于动画我试过这样
<animate attributeName="width" from="0" to="200" dur="5s" />
任何人都可以向我建议如何实现此输出。任何帮助将不胜感激
svg {
width: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="slider1" type="range" min="0" max="100" step="25" value="25" oninput="document.querySelector(`#SVG${this.id} rect`).setAttribute('width',this.value+'%')" />
<svg id="SVGslider1" viewBox="0 0 30 6">
<rect width="25%" height="100%" fill="blue" />
<rect width="100%" height="100%" fill="none" stroke="black"/>
</svg>
解决方案
尝试调整x
,y
并font-size
在文本上添加一个transition
属性rect
:
svg {
width: 200px;
}
rect {
transition: width 0.5s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="slider1" type="range" min="0" max="100" step="25" value="25" oninput="document.querySelector(`#SVG${this.id} rect`).setAttribute('width',this.value+'%')" />
<svg id="SVGslider1" viewBox="0 0 30 6">
<rect width="25%" height="100%" fill="blue" />
<rect width="100%" height="100%" fill="none" stroke="black"/>
<text x="5" y="5" font-size="6" fill="green">text here</text>
</svg>
编辑:要使元素垂直填充,请<rect>
交换width
和:height
y
svg {
width: 200px;
}
rect {
transition: y 0.5s, height 0.5s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="slider1" type="range" min="0" max="100" step="25" value="25" oninput="document.querySelector(`#SVG${this.id} rect`).setAttribute('height',this.value+'%');document.querySelector(`#SVG${this.id} rect`).setAttribute('y',100-this.value+'%')" />
<svg id="SVGslider1" viewBox="0 0 30 6">
<rect y="75%" width="100%" height="25%" fill="blue" />
<rect width="100%" height="100%" fill="none" stroke="black"/>
<text x="5" y="5" font-size="6" fill="green">text here</text>
</svg>
推荐阅读
- java - 如何使用Java查找至少一个字段在mongodb中具有值
- java - 如何强制 Spark 创建度量 sink.csv.directory?
- ios - Swift Keychain 无法从 viewDidLoad 中检索保存的密码以实现自动登录功能
- c# - 带有 SQL 传输的 NService 总线
- java - 如何修复 apache felix 中的已解决状态?
- javascript - 如何 document.querySelectAll 内联 CSS 样式?
- eclipse - 如何在 Eclipse RCP 应用程序中使用 mylyn 通知扩展?
- javascript - 复选框在映射功能中不起作用反应js css
- xcode - High Sierra 和 Mojave 操作系统
- python-3.x - 分组并计算时间戳的差异