2012-07-31 87 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