jQuery SVG - 悬停元素

标签 jquery svg hover

我正在尝试使用 jQuery SVG 插件 http://keith-wood.name/svg.html

一开始我就陷入困境。我创建了一个简单的 svg.circle 和 svg.text 在该圆圈上。我想在鼠标悬停在圆圈上时给出一些动画。它有效,但是当我将鼠标指针移动到文本悬停状态时,它会关闭。这是正确的行为,但如何在鼠标获取文本时强制此悬停状态?

这是代码:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>jQuery SVG Basics</title>
<style type="text/css">
#svgbasics { width: 400px; height: 300px; border: 1px solid #484; font-family: Tahoma; font-size: 50px; }
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.svg.js"></script>
<script type="text/javascript" src="jquery.svganim.min.js"></script>
<script type="text/javascript" src="jquery.svgfilter.js"></script>
<script type="text/javascript">

$(function() {
    $('#svgbasics').svg({onLoad: drawInitial});
});

function drawInitial(svg) {

    var mycircle = svg.circle(75, 75, 50, {fill: '#f2477b', stroke: 'none', 'stroke-width': 0});
    var mytext = svg.text(52, 76, 'SVG', {'font-family':"Verdana", 'font-size':20, 'text-anchor':'middle', 'fill':'#fff', 'cursor': 'pointer'});

    $(mycircle).hover(function() {
        $(this).css("cursor", "pointer");
        $(mycircle).animate({svgR: 45, svgStrokeWidth: 4, svgStroke: '#f882a6'}, 100);  
    }, function() { 
        $(mycircle).animate({svgR: 50, svgStrokeWidth: 0, svgStroke: 'none'}, 100); 
    });
}
</script>
</head>
<body>
<div id="svgbasics"></div>
</body>
</html>

这是代码的结果:http://goo.gl/RwRw0

最佳答案

如果您不希望文本响应鼠标悬停,请将指针事件属性设置为无

var mytext = svg.text(52, 76, 'SVG', {'font-family':"Verdana", 'font-size':20, 'text-anchor':'middle', 'fill':'#fff', 'cursor': 'pointer'});

变成了

var mytext = svg.text(52, 76, 'SVG', {'font-family':"Verdana", 'font-size':20, 'text-anchor':'middle', 'fill':'#fff', 'cursor': 'pointer', 'pointer-events', 'none'});

关于jQuery SVG - 悬停元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12859013/

相关文章:

Angular 2 svg :circle fill attribute binding

javascript - 如何让 javascript 鼠标悬停以突出显示 svg 文件中的其他元素(上下文更改)

java - 重新加载 jsf 表单错误上的某个 jQuery 代码

javascript - jQuery 显示和隐藏表格 td 中的元素

javascript - 无法强制 Chrome 使用 Javascript 滚动到页面顶部

html - d3 节点标记

javascript - 如何将不同的背景图像(.png)添加到 SVG 圆形,并设置描边?

html - 悬停链接未覆盖导航栏的整个高度

javascript - 鼠标光标更改 Javascript - 链接 DIV

javascript - jquery 添加和删除类不起作用