I've created svg elements for titles. I dynamically create these elements with jQuery. I want to use this element in different places and I want them to be responsive. When I use $(window).resize function, browser gets stuck.
Here is my code:
function widgetTitle() {
$(".title-svg").each(function(i, item) {
item = $(item);
$(".svgWidget", item).css("width", item.width() + 17);
var a = item.outerWidth() + 17;
var constantPoint1 = "0.83 0.56 15.83 22.56 ";
var constantPoint2 = " 22.56 ";
var constantPoint3 = " 50.56";
var changePoint = a - 2;
var points = constantPoint1 + changePoint + constantPoint2 + changePoint + constantPoint3;
var viewBox = "0 0 " + a + " 50.56";
$(".svgWidget polyline", item).attr("points", points);
$(".svgWidget", item).attr("viewBox", viewBox);
});
}
widgetTitle();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="width: 80%; position: relative;margin-bottom: 50px">
<div class="title-svg" style="width: 100%;position: absolute;top: 10px;">
<svg class="svgWidget" data-name="svgWidget" xmlns="http://www.w3.org/2000/svg">
<linearGradient id="firstGradient" x="100%" y="100%">
<stop offset="0" stop-color="yellow">
<animate attributeName="stop-color" values="green;teal;purple;orange;red" dur="10s" from="0"
to="100%" repeatCount="indefinite"/>
</stop>
<stop offset=100 stop-color="purple">
<animate attributeName="stop-color"
values="lightblue;blue;red;red;black;red;red;purple;lightblue" dur="10s" from="0%"
to="100%" repeatCount="indefinite"/>
</stop>
</linearGradient>
<g style="opacity: 0.38" stroke="url(#firstGradient)">
<polyline style="fill: none;stroke-miterlimit: 10;stroke-width: 1px"/>
</g>
</svg>
</div>
</div>
<div style="clear:both"></div>
<div style="width: 10%: position: relative;">
<div class="title-svg" style="width: 100%;position: absolute;top: 10px;">
<svg class="svgWidget" data-name="svgWidget" xmlns="http://www.w3.org/2000/svg">
<linearGradient id="firstGradient" x="100%" y="100%">
<stop offset="0" stop-color="yellow">
<animate attributeName="stop-color" values="green;teal;purple;orange;red" dur="10s" from="0"
to="100%" repeatCount="indefinite"/>
</stop>
<stop offset=100 stop-color="purple">
<animate attributeName="stop-color"
values="lightblue;blue;red;red;black;red;red;purple;lightblue" dur="10s" from="0%"
to="100%" repeatCount="indefinite"/>
</stop>
</linearGradient>
<g style="opacity: 0.38" stroke="url(#firstGradient)">
<polyline style="fill: none;stroke-miterlimit: 10;stroke-width: 1px"/>
</g>
</svg>
</div>
</div>
As you can see, it did not make my elements responsive.
SOLVED THE PROBLEM WITH THIS CODE:
function widgetTitle() {
$(".title-svg").each(function(i, item) {
item = $(item);
//$(".svgWidget", item).css("width", item.width()+17);
var a = item.outerWidth() - 2;
var constantPoint1 = "0.83 0.56 15.83 22.56 ";
var constantPoint2 = " 22.56 ";
var constantPoint3 = " 50.56";
var changePoint = a;
var points = constantPoint1 + changePoint + constantPoint2 + changePoint + constantPoint3;
var viewBox = "0 0 " + a + " 50.56";
$(".svgWidget polyline", item).attr("points", points);
$(".svgWidget", item).attr("viewbox", viewBox);
});
}
$(window).on('resize', function() {
widgetTitle();
});
widgetTitle();