Raphaeljs入門到精通(二)

來源:互聯網
上載者:User

標籤:raphael   元素屬性   元素事件   

這節我們將介紹Raphaeljs中元素的屬性和事件,案例還是以上一篇的代碼展開

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title>    <script src="Script/raphael.js"></script></head><body>    <div id="paper">    </div>    <script>        //建立一個畫布        var paper = new Raphael("paper", 500, 500);        //畫圓        var circle = paper.circle(50, 50, 40);        circle.attr({            "stroke": "red",            "stroke-width": 4,            "fill": "blue"        });        //或者寫法        circle.attr("opacity",0.5);        //畫圓角方形        var rect = paper.rect(90, 90, 50, 50, 10);            </script></body></html>
paper中給元素添加屬性是使用attr方法,可以一次添加多個,也可以單個添加。

元素的屬性有以下:

cursor (游標顏色),cx,cy (園或者橢圓 圓心點座標),fill(填充顏色),fill-opacity (濾鏡),font,font-family,font-size,font-weight,height

href,opacity,path,src,stroke,stroke-dasharray,stroke-linecap,stroke-linejoin,stroke-miterlimit,stroke-opacity,stroke-width

target,text,text-anchor,title,transform,width,x,y等等

如果是想設定元素的style的話可以使用element.node屬性來獲得當前元素解析到瀏覽器中之後的標籤,然後再使用jquery進行設定。

元素的事件:

Raphaeljs元素一般具有如下事件:

1.click 單擊事件;

2.dbclick 雙擊事件;

3.drag事件,元素拖動事件;

3.hide 隱藏事件;

4.hover 懸浮事件;

5.mousedown 滑鼠按下

6.mouseout 滑鼠移出事件

7.mouseover  ,mouseup 送掉事件;

解除事件的方式如下:

1.unclick

2.undbclick

3.unmousedown

4.等等在綁定事件的詞前面加上首碼詞un就行了。

當然元素事件和屬性也是通過元素.node來設定。

下面我們具體看一個案例,當滑鼠移入圓的時候改變圓的填充顏色,移出的時候又恢複原來的顏色


<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title>    <script src="Script/raphael.js"></script></head><body>    <div id="paper">    </div>    <script>        //建立一個畫布        var paper = new Raphael("paper", 500, 500);        //畫圓        var circle = paper.circle(50, 50, 40);        circle.attr({            "stroke": "red",            "stroke-width": 4,            "fill": "blue"        });        circle.mousedown(function () {            circle.attr("fill", "red");        });        circle.mouseup(function () {            this.attr("fill", "blue");        });        //或者寫法        circle.attr("opacity", 0.5);        //畫圓角方形        var rect = paper.rect(90, 90, 50, 50, 10);        rect.attr({            "stroke": "red",            "stroke-width": 4,            "fill": "blue"        });        rect.attr("opacity", 0.5);        rect.mousemove(function () {            rect.attr("fill", "gray");        });        rect.mouseout(function () {            this.attr("fill", "blue");        });    </script></body></html>

如下:






Raphaeljs入門到精通(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.