在模式框打开之前更改 SVG 元素的颜色,并在框打开时保持该颜色
Change colour of SVG element before modal box opens and keep it that colour while the box is open
我想更改SVG元素的颜色,我单击以打开一个模态框。这似乎比其他情况下更困难,可能是因为我已经在使用 SVG 元素时进行了悬停行为。
Atm 当光标位于元素上时,元素是红色的并放大,但当我单击它以打开模态时,它会恢复为白色。如何在模态打开时将其保持红色,仅在模态关闭时变回白色?当我不单击任何内容时,我仍然想要我已经拥有的悬停行为。
asylumQ是Q形,AsylumTr是中间的一个洞。当鼠标悬停在 Q 或 Q 中的孔上时,Q 形状变为红色(这并不完美,但使用 js 将 2 个元素组合在一起不起作用,并且在 SVG 中将它们分组也破坏了行为,所以也许我稍后会解决这个问题)。
这是 js:
asylumQ.hover(function() {
asylumQ.attr({ opacity: 1, fill: 'red', stroke: 'red' });
asylumQ.animate({ transform: 's2,2'}, 300, mina.easein);
asylumTr.animate({ transform: 's2,2'}, 300, mina.easein);
},
function() {
asylumQ.attr({ opacity: 1, fill: 'white', stroke: 'white' });
asylumQ.animate({ transform: 's0.8,0.8'}, 300);
asylumTr.animate({ transform: 's0.8,0.8'}, 300, function () {
asylumQ.animate({ transform: 's1,1'}, 20);
asylumTr.animate({ transform: 's1,1'}, 20);
});
}
);
asylumTr.hover(function() {
asylumQ.attr({ opacity: 1, fill: 'red', stroke: 'red' });
asylumQ.animate({ transform: 's2,2'}, 300, mina.easein);
asylumTr.animate({ transform: 's2,2'}, 300, mina.easein);
},
function() {
asylumQ.attr({ opacity: 1, fill: 'white', stroke: 'white' });
asylumQ.animate({ transform: 's0.8,0.8'}, 300);
asylumTr.animate({ transform: 's0.8,0.8'}, 300, function () {
asylumQ.animate({ transform: 's1,1'}, 20);
asylumTr.animate({ transform: 's1,1'}, 20);
});
}
);
asylumTr.click(function() {
asylumQ.attr({ opacity: 1, fill: 'red', stroke: 'red' });
$('#asylumModal').modal('show');
});
使用所需的颜色定义一个 CSS 类,然后在对话框打开时将其添加到元素类列表中。并在关闭对话框时再次将其删除。
例:
$("#myrect").click(function() {
// Add a class to the rect which keeps it with the hover styling.
// Note: We can't use jQuery's addClass() for this because it doesn't work with SVGs
$("#myrect").attr("class", "dialogIsOpen");
// Open the modal
$("#myModal").modal();
// Attach a handler for the dialog's hide event so we can remove the class we added
$('#myModal').on('hidden.bs.modal', function (e) {
$("#myrect").removeAttr("class");
});
});
rect {
transform: scale(1,1);
transition: all 0.5s;
cursor: pointer;
}
rect:hover,
.dialogIsOpen {
transform: scale(1.25,1.25);
fill: red;
}
/* position the modal out of the way so we can see the SVG */
.modal-dialog {
margin-top: 160px !important;
}
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<svg>
<g transform="translate(150 75)">
<rect id="myrect" x="-50" y="-50" width="100" height="100" fill="white" stroke="black"/>
</g>
</svg>
<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>An example modal</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
相关文章:
- 使用mouseover方法时无法保护svg的颜色
- SVG:线条颜色
- 为不同的SVG应用不同的颜色
- 如何访问svg内部id并让CSS更改该id的填充颜色
- 如何在悬停时更改SVG图像的颜色
- 在模式框打开之前更改 SVG 元素的颜色,并在框打开时保持该颜色
- 如何从onClick更改多个SVG节点的颜色
- 如何将SVG的背景颜色正确转换为Canvas
- 缩放SVG图像以适应父对象,并在不同的实例中更改仪表颜色
- 使用CSS或SVG设置与背景颜色匹配的文本颜色的动画
- 悬停时使用Javascript/jQuery淡化自定义SVG的颜色
- 如何设置D3.js svg的背景颜色
- 将背景颜色和图像过滤器的组合应用于svg路径
- 我们可以使用 Javascript 更改加载的 SVG 文件的颜色吗?
- 如何使用 CSS 在 HTML 中设置外部 SVG 颜色
- 如何为 svg 元素提供 hsl 颜色值
- 无法重置 SVG 路径的填充颜色
- 使用 Javascript 更改 SVG 的颜色
- 如何更改 SVG 的路径颜色
- 如何在页面渲染期间更改颜色 SVG .