【问题标题】:Apply Different Color for Different SVG为不同的 SVG 应用不同的颜色
【发布时间】:2021-04-11 13:45:58
【问题描述】:

我有一些 svg 我想有条件地应用颜色,我在 jsfiddle 链接中写了详细说明

  1. 第一个 SVG 只能应用红色/绿色
  2. 第二个 SVG 可以应用蓝色/橙色
  3. 第三个 SVG 可以应用黄色/粉色

http://jsfiddle.net/6w21z1bq/

【问题讨论】:

  • 你能解释一下你面临的问题吗?
  • 请将外部托管代码编辑到帖子中;这样做将确保即使链接断开,它仍然有用。我的脚本 is not allowed to do this 因为潜在的许可问题。

标签: javascript jquery html svg


【解决方案1】:

检查FIDDLE上的更新代码 .已更改 SVG 标签并将 data-index 属性应用于圆圈和按钮。如果选定的圆形数据索引属性与单击按钮数据索引属性相同,则它将应用颜色,否则显示警报。

var clickedPath = '';
var rIndex = '';
$('#select1,#select2,#select3').on("click", function() {
    clickedPath = $(this);
    rIndex = $(this).attr('data-index');
    clickedPath.css({ stroke: "#00ff00" });
});
		
$('.btn').on('click',function(){
    var attr = '';
    attr = $(this).attr('data-index').split(',');
    if($.inArray(rIndex,attr) >= 0 ){
      clickedPath.css({ fill: $(this).attr('data-color') });
    }else{
      alert("Wrong click");
    }
});
<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>

【讨论】:

  • @JavascriptGuy 你可以取任何值。但是你需要改变逻辑来比较索引和其他东西
  • @JavascriptGuy 检查我更新的答案。我添加了代码 sn-p
  • @JavascriptGuy 检查我更新的答案。我添加了代码 sn-p
  • 不知何故无法创建小提琴..但我已经更新了答案并添加了代码 sn-ps ...检查
【解决方案2】:

我建议你这样做:

更新的 svg:

<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 的 id 一样,如果 svg id 是 id="select1",那么相应的按钮将有 class="btn select1"

和更新的js代码:

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')
    });
});

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 2023-03-25
    • 2020-01-29
    • 2012-08-24
    • 1970-01-01
    • 2021-09-29
    • 2016-04-03
    相关资源
    最近更新 更多