简体   繁体   English

图像地图/定位锚在定位图像上方(Firefox问题)

[英]Image map / positioned anchor over top of a positioned image (Firefox issue)

I'm noticing a strange behaviour in Firefox. 我注意到Firefox中有奇怪的行为。

I have a banner that's positioned absolutely and centred, so I use a bit of a css trick to do this: 我的横幅广告的位置绝对居中,因此我使用了一些CSS技巧来做到这一点:

#banner { 
  position: absolute; 
  top: 85px; 
  z-index: 1; 
  width: 1280px; 
  left: 50%; 
  margin-left: -640px; 
}

This works just fine across my target browsers. 在我的目标浏览器上都可以正常工作。

To add some complexity, this banner has hot spots. 更复杂的是,此横幅有热点。

So, I tried attaching an image map to the image. 因此,我尝试将图像映射附加到图像。 This works fine in IE7/8. 在IE7 / 8中可以正常工作。 No luck with Firefox. Firefox运气不好。 Next, I tried putting an anchor inside the banner container, which worked visually (ie I put a border on the anchor to see if it was positioned correctly), but it does not respond to a click event, even with javascript. 接下来,我尝试将锚放置在横幅容器内,该容器在视觉上起作用(即,在锚上放置边框以查看其是否正确放置),但是即使使用JavaScript,它也不会响应click事件。

Here is the markup that I've tried: 这是我尝试过的标记:

<div id="banner">
    <img src="/images/banners/splash.jpg" alt="" width="1280" height="481" usemap="#splashMap" />
    <a href="#" id="banner-anchor1" title="">Some Text</a>    
</div>
    <map name="splashMap" id="splashMap">
        <area shape="rect" coords="174,192,304,464" href="#" alt="" />
        <area shape="rect" coords="277,76,397,169" href="#" alt="" />
        <area shape="rect" coords="306,360,415,470" href="#" alt="" />
        <area shape="rect" coords="662,347,763,479" href="#" alt="" />
    </map>

With the following css: 使用以下css:

#banner a { 
display: block; 
text-indent: -999px; 
position: absolute; 
z-index: 2; 
left: 50%; 
border: 1px solid red; 
}
#banner-anchor1 { 
top: 133px; 
width: 129px; 
height: 289px; 
margin-left: -467px; 
}

Both the anchor and image map options work in IE. 锚和图像映射选项都可以在IE中使用。

Thankfully the shapes are rectangles, so I have some flexibility, but this is otherwise a strange case. 值得庆幸的是,形状是矩形,所以我有一定的灵活性,但这是一个奇怪的情况。

Is there a way to overcome this in Firefox, or different technique I can use to centre an absolutely positioned element? 在Firefox中是否可以解决此问题,或者可以使用其他技术来使绝对定位的元素居中?

Update 更新资料

There was a z-index conflict with other elements. 与其他元素存在z-index冲突。 There was a div around the body content, and the banner laid over top of that div. 正文内容周围有一个div,并且横幅显示在该div上方。 The z-index on the content div was causing trouble in FF with the anchors, even though the z-index was lower - I guess because they don't share the same parent. 内容div上的z索引在FF中造成了锚点问题,尽管z索引较低-我猜是因为它们不共享同一父对象。 Odd how IE didn't have a problem with this. 奇怪的是IE对此没有问题。

My first tip: don't use imagemaps. 我的第一个提示:不要使用图像映射。 They're very user unfriendly, and search engines have a hard time to follow the links as well. 它们对用户非常不友好,搜索引擎也很难跟踪链接。 It's kind of regarded as a deprecated technique (although not officially, I believe). 它被认为是一种已过时的技术(尽管我认为不是正式的)。

Anyway, I don't know what it should look like (where should the anchor be positioned?) but in Chrome it all works, except the negative margin on the #banner-anchor1 puts it in a weird place. 无论如何,我不知道它的外观(应该将锚定位在何处?),但是在Chrome中,它可以正常工作,除了#banner-anchor1上的负边距将其放置在一个怪异的地方。

In firefox it all seems to work as well if I remove the margin on the link... 在firefox中,如果我删除链接上的空白,一切似乎也都可以正常工作...

Example: http://jsfiddle.net/96etN/1/ 示例: http//jsfiddle.net/96etN/1/

Try making sure you're using position: relative on that Anchor element? 尝试确保您在该Anchor元素上使用position:relative? I do something very similar in a visual glossary, Where I mark rectangles over top of an image to allow users to click and see more information about a document's sections, And it works fine in all browsers. 我在视觉词汇表中做了非常相似的事情,即在图像上方标记矩形,以允许用户单击并查看有关文档部分的更多信息,并且在所有浏览器中都可以正常工作。

One other thing to note, is that if you're going to nest multiple rectangles and want the same offsets for them, You'll need a wrapper to reset the relative position to 0,0 of your parent container, For example 需要注意的另一件事是,如果您要嵌套多个矩形并希望它们具有相同的偏移量,则需要使用包装器将相对位置重置为父容器的0,0,例如

<div class="myContainer">
 <div class="myWrapper">
  <a href="#" id="myElement1" class="myTarget" />
 </div>
 <div class="myWrapper">
  <a href="#" id="myElement2" class="myTarget" />
 </div>
 <img src="foo" id="myTitleImage" />
</div>

#myContainer {
 display: block;
 margin-left: auto;
 margin-right: auto;
 width: 80%;
}

.myWrapper { 
 position: absolute;
}

.myTarget {
 position: relative;
 display: block;
}

#myElement1 {
 top: 10px;
 right: 10px;
 width: 50px;
 height: 20px;
}

etc. etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM