为不同的SVG应用不同的颜色
Apply Different Color for Different SVG
我有一些svg,我想应用颜色的条件,我在jsfiddle链接中写了详细信息
- 第一个SVG只能应用红/绿
- 第二个SVG可以应用蓝色/橙色
- 第三个SVG可以应用黄色/粉红色
http://jsfiddle.net/6w21z1bq/
检查FIDDLE上的更新代码.对SVG标记进行了更改,并将数据索引属性应用于圆和按钮。若选定的圆dta索引属性和单击的按钮数据索引属性相同,则它将应用颜色else显示警报。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Select SVG and Apply Color <br/>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="blue" stroke-width="4" fill="yellow" id="select1" data-index = '1' />
</svg>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="yellow" stroke-width="4" fill="red" id="select2" data-index = '2' />
</svg>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="blue" stroke-width="4" fill="red" id="select3" data-index = '3' />
</svg>
<br/> <br/>
Apply to Only 1st SVG / For 2nd and 3rd should not be Apply, Alert Box Should Come
<button class="btn" id="btn-test1" data-color="#ff0000" data-index = '1,2'>Red</button>
<button class="btn" id="btn-test2" data-color="#00ff00" data-index = '1,2'>Green</button>
<br/> <br/>
Apply to Only 2nd SVG / For 1st and 3rd should not be Apply, Apply Alert Box Should Come
<button class="btn" id="btn-test3" data-color="#0000ff" data-index = '2,3'>Blue</button>
<button class="btn" id="btn-test4" data-color="#ff9300" data-index = '2,3'>Orange</button>
<br/> <br/>
Apply to Only 3rd SVG / For 3rd and 1st should not be Apply, Apply Alert Box Should Come
<button class="btn" id="btn-test5" data-color="#ffba00" data-index = '3,1'>Yellow</button>
<button class="btn" id="btn-test5" data-color="#ff006c" data-index = '3,1'>Pink</button>
<button class="btn select1" id="btn-test1" data-color="#ff0000">Red</button>
<button class="btn select1" id="btn-test2" data-color="#00ff00">Green</button>
<button class="btn select2" id="btn-test3" data-color="#0000ff">Blue</button>
<button class="btn select2" id="btn-test4" data-color="#ff9300">Orange</button>
<button class="btn select3" id="btn-test5" data-color="#ffba00">Yellow</button>
<button class="btn select3" id="btn-test5" data-color="#ff006c">Pink</button>
我建议你这样做:
更新的svg:
var clickedPath = '';
$('#select1,#select2,#select3').on("click", function () {
clickedPath = $(this);
$('.btn').prop('disabled', false); // enable all buttons
$('.btn').not('.' + clickedPath.attr('id')).prop('disabled', true); // enable the corresponding buttons.
clickedPath.css({
stroke: "#00ff00"
});
});
$('.btn').on('click', function () {
clickedPath.css({
fill: $(this).attr('data-color')
});
});
我上面更新的只是向按钮添加了一个与svg-like的id相同的类名,如果svg-id是class="btn select1"
,那么相应的按钮将具有CCD_3。
和更新的js代码:
var clickedPath = '';
$('#select1,#select2,#select3').on("click", function() {
clickedPath = $(this);
$('.btn').prop('disabled', false);
$('.btn').not('.' + clickedPath.attr('id')).prop('disabled', true);
clickedPath.css({
stroke: "#00ff00"
});
});
$('.btn').on('click', function() {
clickedPath.css({
fill: $(this).attr('data-color')
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Select SVG and Apply Color
<br/>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="blue" stroke-width="4" fill="yellow" id="select1" />
</svg>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="yellow" stroke-width="4" fill="red" id="select2" />
</svg>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="blue" stroke-width="4" fill="red" id="select3" />
</svg>
<br clear='all'/>
<br/>Apply to Only 1st SVG / For 2nd and 3rd should not be Apply, Alert Box Should Come
<button class="btn select1" id="btn-test1" data-color="#ff0000">Red</button>
<button class="btn select1" id="btn-test2" data-color="#00ff00">Green</button>
<br/>
<br/>Apply to Only 2nd SVG / For 1st and 3rd should not be Apply, Apply Alert Box Should Come
<button class="btn select2" id="btn-test3" data-color="#0000ff">Blue</button>
<button class="btn select2" id="btn-test4" data-color="#ff9300">Orange</button>
<br/>
<br/>Apply to Only 3rd SVG / For 3rd and 1st should not be Apply, Apply Alert Box Should Come
<button class="btn select3" id="btn-test5" data-color="#ffba00">Yellow</button>
<button class="btn select3" id="btn-test5" data-color="#ff006c">Pink</button>
PD_7相关文章:
- 为不同的SVG应用不同的颜色
- 对选定的排序项目应用不同的颜色
- 在表格中应用动态行颜色 Javascript/VBscript
- 获取动态背景颜色,使用 LESS 应用于单独的元素
- DC.js 应用过滤器后热图颜色范围未更新
- d3.js应用不适用于 JSON 的颜色范围
- EaseJS将颜色过滤器应用于位图
- 将不同的颜色应用于动态生成的 HTML 表行
- 将多种颜色应用于谷歌图表中的单个元素
- 如何将动态颜色应用于与 HTML 表不同的每一行
- 如何将颜色选择器添加到我的绘图应用
- 如何在绘图应用程序上单击颜色时播放声音
- 将预定义的颜色序列应用于元素的动态列表
- 单击输入字段将背景颜色应用于父项,并取消选择初始选择的单选按钮
- 为什么我的webGL应用程序在iPhone上查看时背景颜色不同
- 如何在three.js中使用挤出几何体创建的形状的每一侧应用不同的颜色
- 如何为第一次选择和第二次选择复选框应用不同的颜色
- 如何在谷歌地图API中应用不同颜色的重叠区域的圆
- -webkit点击未应用于区域标记的突出显示颜色
- 将HSL随机颜色应用于函数