2012-07-31 50 views
1

我很抱歉提出这个问题,但我只是在今天上午寻找一些指导。我只是想创建一个函数,这样我就可以通过传入该元素来使Raphael元素发光。以下是我的代码。为什么这不起作用?javascript raphael对象函数传递

var paper = Raphael("playarea", 500, 500); 

var rectangle = paper.rect(100, 100, 200, 200, 4); 

function elemHover(var el) 
{ 

    el.hover(
    // When the mouse comes over the object // 
    // Stock the created "glow" object in myCircle.g 
    function() { 
    this.g = this.glow({ 
     color: "#0000EE", 
     width: 10, 
     opacity: 0.8 
    }); 
    }, 
    // When the mouse goes away // 
    // this.g was already created. Destroy it! 
    function() { 
    this.g.remove(); 
    }); 
} 

elemHover(rectangle); 

这里是小提琴http://jsfiddle.net/aZG6C/15/

+0

你看到在你的JS控制台任何错误? – apsillers 2012-07-31 13:58:12

+0

你能提供一个jsfiddle演示吗? – 2012-07-31 14:00:17

+0

这里是小提琴http://jsfiddle.net/aZG6C/15/ – j0hnstew 2012-07-31 15:06:24

回答

2

你应该fill元素(在本例中的矩形)来触发悬停。

rectangle.attr("fill", "red"); 

试试这个小提琴http://jsfiddle.net/aZG6C/17/

完整的代码看起来像

<div id="playarea"></div> 

​<script type="text/javascript"> 
var paper = Raphael("playarea", 500, 500); 

var rectangle = paper.rect(100, 100, 200, 200, 4); 

function elemHover(el) 
{ 
    el.hover(
    // When the mouse comes over the object // 
    // Stock the created "glow" object in myCircle.g 
    function() { 
    this.g = this.glow({ 
     color: "#0000EE", 
     width: 10, 
     opacity: 0.8 
    }); 
    }, 
    // When the mouse goes away // 
    // this.g was already created. Destroy it! 
    function() { 
    this.g.remove(); 
    }); 
} 

rectangle.attr("fill", "red"); 

elemHover(rectangle); 

</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

更新如果元素被填充了时才会触发

悬停事件。如果你想有一个透明的元素,您可以尝试

rectangle.attr("fill", "transparent"); 

检查小提琴这里http://jsfiddle.net/aZG6C/20/

+0

其伟大的,它的工作原理,但我不明白为什么矩形.attr是必需的,才能这个工作。 – j0hnstew 2012-07-31 15:22:24

+1

悬停事件仅在元素充满某物时触发。回答更新了这个信息和解决透明元素的方法。 – kiranvj 2012-07-31 15:39:20

+1

不知道。感谢帮助。你今天保存了一台电脑。感觉很好。 – j0hnstew 2012-07-31 15:48:48

相关问题