如何在rect上添加onclick操作以更改所选选项卡的颜色?

时间:2012-01-07 09:56:47

标签: javascript html raphael

我需要添加动作onclick会改变颜色(灰色)。然后它将保持灰色,直到它被点击到另一个标签。

我想在选定状态下显示标签(通过灰色)。如果单击另一个选项卡,其颜色将变为灰色,之前单击选项卡颜色更改为白色。

这是我的代码的一部分: enter image description here                   

        <link rel="stylesheet" href="demo.css" type="text/css" media="screen">
        <link rel="stylesheet" href="demo-print.css" type="text/css" media="print">
        <style type="text/css" media="screen">
            #canvas {
                height: 300px;
                left: 50%;
                margin: -150px 0 0 -300px;
                position: absolute;
                top: 50%;
                width: 600px;
            }
            #paper {
                height: 300px;
                left: 0;
                position: absolute;
                top: 0;
                width: 300px;
            }
            #nsw, #vic, #wa, #sa, #nt, #qld, #tas {
                display: none;
                height: 400px;
                overflow: auto;
                position: absolute;
                right: 0;
                top: 0;
                width: 300px;
            }
            h2 {
                text-align: center;
            }
        </style>
        <script src="raphael.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript" charset="utf-8">
        window.onload = function () {
            var R = Raphael("paper", 700, 700);
            var attr = {
                fill: "#333",
                stroke: "#666",
                "stroke-width": 1,
                "stroke-linejoin": "round"
            };
            var aus = {};
            var outsideRectX1=30, outsideRectY1=30,outsideRectX2=220, outsideRectY2=480, outsideRectR=10;
            aus.nsw = R.rect(outsideRectX1+40, outsideRectY1+70, 120, 40,0);
            aus.vic = R.rect(outsideRectX1+160, outsideRectY1+70, 120, 40,0);
            aus.wa = R.rect(outsideRectX1+280, outsideRectY1+70, 120, 40,0);
            aus.sa = R.rect(outsideRectX1+400, outsideRectY1+70, 120, 40,0);

            var text3=R.text(outsideRectX1+75, outsideRectY1+85,"tab1").attr({fill: '#000000', 'font-family':'Vardana', 'font-size':'14px', 'font-weight': 'bold'});
var text4=R.text(outsideRectX1+195, outsideRectY1+85,"tab2").attr({fill: '#000000', 'font-family':'Vardana', 'font-size':'14px', 'font-weight': 'bold'});
var text5=R.text(outsideRectX1+315, outsideRectY1+85,"tab3").attr({fill: '#000000', 'font-family':'Vardana', 'font-size':'14px', 'font-weight': 'bold'});
var text6=R.text(outsideRectX1+435, outsideRectY1+85,"tab4").attr({fill: '#000000', 'font-family':'Vardana', 'font-size':'14px', 'font-weight': 'bold'});

            var current = null;
            for (var state in aus) {
                aus[state].color = Raphael.getColor();
                (function (st, state) {
                    st[0].style.cursor = "pointer";
                    st[0].click = function () {
                        current && aus[current].animate({fill: "#333", stroke: "#666"}, 500) && (document.getElementById(current).style.display = "");
                        st.animate({fill: st.color, stroke: "#ccc"}, 500);
                        st.toFront();
                        R.safari();
                        document.getElementById(state).style.display = "block";
                        current = state;
                    };

                })(aus[state], state);
            }
        };

        </script>
    </head>
    <body>
        <div id="canvas">
            <div id="paper"></div>
            <div id="nsw"> </div>
            <div id="vic">  </div>
            <div id="wa"></div>
            <div id="sa">             

        </div>

        </div>

    </body>
</html>

1 个答案:

答案 0 :(得分:0)

以下是您的代码的简化版本,我删除了无关的部分以简化工作:

var R = Raphael("paper", 700, 700);
var aus = {};
aus.nsw = R.set()
aus.vic = R.set()
aus.wa = R.set()
aus.sa = R.set()
var boxattrs = {'cursor': 'pointer', 'fill' : "#fff"}
var attrs = {fill: '#000000', 'font-family':'Verdana', 'font-size':'14px', 'font-weight': 'bold', 'cursor': 'pointer'}
var outsideRectX1=30.5, outsideRectY1=30.5,outsideRectX2=220.5, outsideRectY2=480.5, outsideRectR=10.5;
var i = 0;

for (state in aus) {
  aus[state].push(R.rect((outsideRectX1+90)*i+80, outsideRectY1+70, 120, 40,0).attr(boxattrs));
  aus[state].push(R.text((outsideRectX1+90)*i+115, outsideRectY1+85,"tab" + (i+1)).attr(attrs));
  aus[state].click(highlight)
  i++;
}

function highlight() {
   for (state in aus) {
     if(aus[state][0].attr("fill") == "#CCCCCC") {
       aus[state][0].animate({fill: "#FFFFFF", "stroke": "#000"}, 300)
     }
   }
   node = (this.type == "rect") ? this : this.prev
   node.animate({fill: "#CCCCCC", "stroke": "#CCC"}, 300)
}

您可以在此处查看http://jsfiddle.net/xYfXE/