My SVG is embedded using an object tag and includes CSS3 hover animations. However, I am facing issues when trying to hyperlink the SVG with hover animation as it interferes with clicking actions. I chose not to use the long SVG path code for better code organization.
The JavaScript provided below makes the SVG clickable, but it doesn't navigate to the desired anchor # for scrolling later on the page. Is there a way to combine these two click functions effectively?
I understand this setup may seem messy at the moment!
HTML
<object id="example" type="image/svg+xml" data="/images/icon.svg"></object>
Inside the SVG:
<a xlink:href="#top">
<g class="whole" fill="#fff">
<rect id="body" x="113.83" y="185.4" width="43" height="49.5" style="stroke:#f36a24;stroke-miterlimit:10;stroke-width:3px" />
<ellipse id="head" cx="134.96" cy="165.04" rx="14.13" ry="13.92" style="stroke:#f36a24;stroke-miterlimit:10;stroke-width:3px" />
</g>
</a>
//JS below
<script>
(function ($) {
$(document).ready(function () {
$('svg a').click(function (event) {
alert(event.target.parentElement.tagName);
});
});
})(jQuery);
</script>
<script>
$(function () {
$('a[href*="#"]:not([href="#"])').click(function () {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
</script>