如何在鼠标悬停时设置 JavaScript 样式区域元素?

发布于 2024-09-28 04:14:26 字数 773 浏览 0 评论 0原文

我有一个包含 20 个区域元素的图像映射,下面只显示了四个。我想对每个区域进行样式设置,以便每当用户将鼠标悬停在其上时就会出现蓝色边框 - 所有区域形状都是矩形。

<map id="mymap" name="mymap">
    <area shape="rect" coords="0,0,223,221" href="http://..." />
    <area shape="rect" coords="226,0,448,221" href="http://..." />
    <area shape="rect" coords="451,0,673,223" href="http://..." />
    <area shape="rect" coords="677,0,1122,223" href="http://..." />
    ...
</map>

我尝试过使用 CSS 来设置每个区域的样式,但它不起作用。我尝试在地图元素上放置 onmouseover=color() 并调用以下函数,但这似乎也不起作用:

function color() {
    var blueboxes = document.getElementsByTagName('area');
    for(var i=0; i<blueboxes.length; i++) {
        blueboxes[i].style.border = 'solid blue 5px';
    }
}

I've got an image map with 20 area elements, only four shown below. I want to style each area so that a blue border appears whenever a user hovers over it - all the area shapes are rectangles.

<map id="mymap" name="mymap">
    <area shape="rect" coords="0,0,223,221" href="http://..." />
    <area shape="rect" coords="226,0,448,221" href="http://..." />
    <area shape="rect" coords="451,0,673,223" href="http://..." />
    <area shape="rect" coords="677,0,1122,223" href="http://..." />
    ...
</map>

I've tried using CSS to style each area, but it's not working. And I've tried to put an onmouseover=color() on the map element and call the following function, but that doesn't seem to be working either:

function color() {
    var blueboxes = document.getElementsByTagName('area');
    for(var i=0; i<blueboxes.length; i++) {
        blueboxes[i].style.border = 'solid blue 5px';
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌梦深 2024-10-05 04:14:26

mapper.js 可以用于此目的。

Mapper.js 2.4 允许您向网页上的图像地图添加自动区域突出显示(包括导出到 SVG)。
它适用于所有主要浏览器 - Mozilla Firefox 1.5+、Opera 9+、Safari 和 IE6+。在较旧的浏览器上,它可以使用 Walter Zorn 的 "jsgraphics"(如果已安装) ),否则它会降级并且您的访问者不会注意到任何事情。

该网站的示例代码:

请注意,这一行下面的所有内容都是他的代码和措辞,而不是我的。完整归属属于上面的链接。

设置

下载mapper.js 并将其包含到您的网页中。

<script type="text/javascript" src="wz_jsgraphics.js"></script>
<script type="text/javascript" src="mapper.js"></script>

“wz_jsgraphics.js”的版权归 Walter Zorn 所有,不属于分发范围!

使用它

要获得突出显示,只需将 class="mapper" 添加到 div 包围的图像中即可。

<div>
<img src="..." class="mapper" usemap="..." alt="...">
</div>

要突出显示各个区域,请向该区域添加一个或多个类。

<map>
...
<area shape="poly" class="noborder icolor00ff00" href="#" coords="...">
...
</map>

要获得多个区域选择,请向区域 rel 属性添加一个或多个 id。

<map>
...
<area shape="poly" id="blue" rel="green,red" href="#" coords="...">
<area shape="poly" id="green" rel="red,blue" href="#" coords="...">
<area shape="poly" id="red" rel="green,blue" href="#" coords="...">
...
</map>

使用初始区域的属性强制一组区域。

<map>
...
<area shape="rect" id="black" class="icolor000000 forcegroup" rel="green,red,blue" href="#" coords="...">
...
</map>

mapper.js can be used for this.

Mapper.js 2.4 allows you to add automatic area highlighting to image maps on your webpages (inc. export to SVG).
It works in all the major browsers - Mozilla Firefox 1.5+, Opera 9+, Safari and IE6+. On older browsers, it can use "jsgraphics" from Walter Zorn (if installed), else it'll degrade and your visitors won't notice a thing.

Sample code from that website:

Please note that everything below this line is his code and wording, not mine. Full attribution belongs to the link above.

Setting Up

Download mapper.js and include it into your webpage.

<script type="text/javascript" src="wz_jsgraphics.js"></script>
<script type="text/javascript" src="mapper.js"></script>

"wz_jsgraphics.js" is copyright by Walter Zorn and not part of the distribution!

Using It

To get the highlighting just add a class="mapper" to an div surrounded image.

<div>
<img src="..." class="mapper" usemap="..." alt="...">
</div>

To get individual area highlightings add one or more classes to the area.

<map>
...
<area shape="poly" class="noborder icolor00ff00" href="#" coords="...">
...
</map>

To get multiple area selections add one or more id's to the areas rel attribute.

<map>
...
<area shape="poly" id="blue" rel="green,red" href="#" coords="...">
<area shape="poly" id="green" rel="red,blue" href="#" coords="...">
<area shape="poly" id="red" rel="green,blue" href="#" coords="...">
...
</map>

To force a group of areas using the attributes of the initial area.

<map>
...
<area shape="rect" id="black" class="icolor000000 forcegroup" rel="green,red,blue" href="#" coords="...">
...
</map>
远昼 2024-10-05 04:14:26

区域标签的样式不能像普通锚点一样。我会使用不同的方法。您可以将图像作为背景图像应用到 div,然后使用position:absolute 将可点击元素放置在 div 上。

看看这个技术:http://www.alistapart.com/articles/cssmaps/

The area tag can't be styled like a normal anchor. I would use a different approach. You could apply your image to a div as the background-image and then position clickable elements over the div by using position: absolute.

Take a look at this technique: http://www.alistapart.com/articles/cssmaps/

瀞厅☆埖开 2024-10-05 04:14:26

Raphaeljs 可以帮忙吗?

看看这个示例:)

Can Raphaeljs helps?

Have a look at this sample :)

放我走吧 2024-10-05 04:14:26

通常我看到的方法是用 CSS 中的不同图像构建图像映射本身。这是一个很好的例子:

http://ago.tanfa.co。英国/css/examples/europe-map.html

Usually the approach I see is to build the imagemap itself out of different images in CSS. Here's a good example of this:

http://ago.tanfa.co.uk/css/examples/europe-map.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文