如何在图像映射上设置热点区域的颜色

时间:2012-09-18 08:42:54

标签: c# javascript jquery asp.net imagemap

我动态创建了一个热点区域,我想改变这个热点区域的颜色我该如何实现呢?我还想展示热点地区的边界。

以下是我在c#dynamic

中在图像地图上创建热点的代码
protected void txtall_TextChanged(object sender, EventArgs e)
{
    string[] arr = txtall.Text.Split(',');
//create hotspot once there are 10 cordinates
    if (arr.Length == 10)
    {
        PolygonHotSpot Polygon1 = new PolygonHotSpot();
        Polygon1.Coordinates = arr[0].ToString() + "," + arr[1].ToString() + "," + arr[2].ToString() + "," + arr[3].ToString()
            + "," + arr[4].ToString() + "," + arr[5].ToString() + "," + arr[6].ToString() + "," + arr[7].ToString() + "," + arr[8].ToString() + "," + arr[9].ToString();// "128,185,335,157,400,224,400,400,228,400";

        Polygon1.NavigateUrl="http://www.google.com";
        Polygon1.AlternateText = "Southern Region";

        ImageMap1.HotSpots.Add(Polygon1);
    }
}

我曾尝试使用here中的jquery,但我无法成功。我是jquery的新手,我只是复制粘贴jquery函数并添加引用,但它不适用于我。我正在收集js代码,因为它只是从上面的链接复制粘贴。任何人都可以提供简单的样式或任何java / jquery函数所以我可以看到对热点区域的影响吗?

请帮助。谢谢。

更新 JAVASCRIPT

<script type="text/javascript" src="JS/JScript.js"></script>
    //
    <script type="text/javascript" src="JS/jquery.imagemapster.js"></script>
    <script type="text/javascript">
        function point_it(event) {
            pos_x = event.offsetX ? (event.offsetX) : event.pageX - document.getElementById("pointer_div").offsetLeft;
            pos_y = event.offsetY ? (event.offsetY) : event.pageY - document.getElementById("pointer_div").offsetTop;
            document.getElementById("ImageMap1").style.left = (pos_x - 1);
            document.getElementById("ImageMap1").style.top = (pos_y - 15);

            document.getElementById("txt1").value = pos_x;
            document.getElementById("txt2").value = pos_y;
            if (document.getElementById("txtall").value == "") {
                document.getElementById("txtall").value = pos_x + "," + pos_y;
            }
            else {
                document.getElementById("txtall").value = document.getElementById("txtall").value + "," + pos_x + "," + pos_y;
            }
            var str = document.getElementById("txtall").value;
            var stringArray = new Array();
            stringArray = str.split(",");
            for (i = 0; i < stringArray.length; i++) {
                stringArray[i];
            }
            if (i == 10) {
            //Creates Hotspot Dynamically
                __doPostBack('txtall ', '');

                //Effects to be applied for the hotspot created
                  var image = $('#ImageMap1');

    image.mapster(
    {
        fillOpacity: 0.4,
        fillColor: "d42e16",
        stroke: true,
        strokeColor: "3320FF",
        strokeOpacity: 0.8,
        strokeWidth: 4,
        singleSelect: true,
        mapKey: 'name',
        listKey: 'name',

        areas: [
            {
                fillColor: "ffffff"
            },

            ]
    });​
            }
            //Clears hotspot
            if (i >= 10) {
                __doPostBack('txtall ', '');
                str = "";
                i = 0;
                stringArray = [];
            }
        }
    </script>

ASPX

<form name="pointform" runat="server">
    <div id="pointer_div" onclick="point_it(event)" style="width: 500px; height: 333px;">
        <asp:ImageMap ID="ImageMap1" runat="server" ImageUrl="~/Images/WMap.JPG" Style="width: 500px;
            height: 333px;">
        </asp:ImageMap>
    </div>
    <br />
    <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
    <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
    <br />
    <asp:TextBox ID="txtall" runat="server" OnTextChanged="txtall_TextChanged" AutoPostBack="true"></asp:TextBox>
    </form>

如果我把jquery image.mapster放到我得到异常,因为point_it(event)未定义..如果我删除jqery,代码工作正常。

2 个答案:

答案 0 :(得分:0)

使用任何JQuery插件。

找到here

答案 1 :(得分:0)

我拿了你的代码并进行了编辑。它现在 txtall上的回发有10个坐标之后有一个 postBack 然后你会看到你的彩色区域!

<强>脚本代码

<script src="js/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript" src="js/imageMapster/jquery.imagemapster.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var image = $('#ImageMap1');
        image.mapster({
            fillOpacity: 0.4,
            fillColor: "d42e16",
            stroke: true,
            strokeColor: "3320FF",
            strokeOpacity: 0.8,
            strokeWidth: 4,
            singleSelect: true,
            mapKey: 'shape',
            listKey: 'shape'
        });

        $("#pointer_div").click(function(event) {
            pos_x = event.offsetX ? (event.offsetX) : event.pageX - document.getElementById("pointer_div").offsetLeft;
            pos_y = event.offsetY ? (event.offsetY) : event.pageY - document.getElementById("pointer_div").offsetTop;
            document.getElementById("ImageMap1").style.left = (pos_x - 1);
            document.getElementById("ImageMap1").style.top = (pos_y - 15);

            document.getElementById("txt1").value = pos_x;
            document.getElementById("txt2").value = pos_y;
            if (document.getElementById("txtall").value == "") 
                document.getElementById("txtall").value = pos_x + "," + pos_y;
            else 
                document.getElementById("txtall").value = document.getElementById("txtall").value + "," + pos_x + "," + pos_y;

             var str = document.getElementById("txtall").value;
             var stringArray = new Array();
             stringArray = str.split(",");
             for (i = 0; i < stringArray.length; i++) {
                stringArray[i];
            }
            if (i == 10) 
            {
                __doPostBack('txtall ', '');

            }
            //Clears hotspot
            if (i >= 10) {
                __doPostBack('txtall ', '');
                str = "";
                i = 0;
                stringArray = [];
            }
        });
    });                
</script>

ASPX代码

<form id="form1" runat="server">
<div>
    <div id="pointer_div" style="width: 500px; height: 333px;">
        <asp:ImageMap ID="ImageMap1" runat="server" ImageUrl="pic/usa_map_720.png" Style="width: 500px;
            height: 333px;">
        </asp:ImageMap>
    </div>
    <br />
    <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
    <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
    <br />
    <asp:TextBox ID="txtall" runat="server" AutoPostBack="true" OnTextChanged="txtall_TextChanged" Text=""></asp:TextBox>
</div>
</form>

<强>代码隐藏

protected void txtall_TextChanged(object sender, EventArgs e)
{
    string[] arr = txtall.Text.Split(',');
    //create hotspot once there are 10 cordinates
    if (arr.Length == 10)
    {
        PolygonHotSpot Polygon1 = new PolygonHotSpot();
        Polygon1.Coordinates = arr[0].ToString() + "," + arr[1].ToString() + "," + arr[2].ToString() + "," + arr[3].ToString()
            + "," + arr[4].ToString() + "," + arr[5].ToString() + "," + arr[6].ToString() + "," + arr[7].ToString() + "," + arr[8].ToString() + "," + arr[9].ToString();// "128,185,335,157,400,224,400,400,228,400";

        Polygon1.NavigateUrl = "http://www.google.com";
        Polygon1.AlternateText = "Southern Region";

        ImageMap1.HotSpots.Add(Polygon1);
    }
}