【发布时间】:2017-03-17 20:06:50
【问题描述】:
我的 SVG 是通过一个对象标签嵌入的,并且还有 CSS3 悬停动画。尝试将 SVG 与悬停动画超链接会弄乱指针或单击操作。我没有将长 SVG 路径代码用于代码组织偏好。
下面的这个 JavaScript 使它可以点击,但它不会转到所需的锚点 # 以便稍后在页面中滚动。 你知道如何结合这两个点击功能吗?
这很乱,我知道!
HTML
<object id="example" type="image/svg+xml" data="/images/icon.svg"> </object>
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
<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>
【问题讨论】:
标签: javascript jquery css svg