z 索引和 Internet Explorer 9
我有 2 个元素,2 个尺寸完全相同的图像,将一个放在另一个之上。假设它们被称为 A 和 B(A 是最上面的)。我所做的就是当你将鼠标悬停在 A 上时,它的 z-index 递减 2,这样 B 现在位于顶部,并且 B 的悬停:将其 z-index 增加 2,所以它现在比 A 的原始值高 1 z-index(因此图像 B 保持在顶部,直到您移开鼠标)。所以基本上...
#A {z-index: 5;}
#B {z-index: 4;}
#A:hover {z-index: 3;}
#B:hover {z-index: 6;}
这在 Firefox 和 Chrome 中完美运行,但 IE 不想听到它,并且我的图像在悬停在它们上方时一直令人惊叹。任何帮助表示赞赏。定位是绝对的,如果这很重要的话。
@jklm313
这实际上也适用于我的 IE9。也许我应该发布完整的代码,因为我的其中一个“图像”实际上是一个社交网络按钮。所以这里是:
HTML:
<div id="myTweetBrown"></div>
<div id="myTweet"><?php include ("myPHP/homepageSoc/tweet.php") ?></div>
CSS:
#myTweetBrown {
position: absolute;
background-image: url('../images/tweetBrown.png');
background-repeat: no-repeat;
background-position: center center;
height: 20px;
width: 54px;
left: 381px;
top: 662px;
z-index: 5;
}
#myTweetBrown:hover {
position: absolute;
z-index: 3;
}
#myTweet {
position: absolute;
height: 20px;
width: 54px;
left: 381px;
top: 662px;
z-index: 4;
}
#myTweet:hover {
position: absolute;
z-index: 6;
}
tweet.php:
<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://localhost/Joomla173/index.php?option=com_content&view=article&id=69&Itemid=507" data-count="none">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
链接到演示网站: ***** -- 向下滚动到推文按钮 这个只会持续这么长时间,因为我不希望人们有这样的访问权限<.<
I've got 2 elements, 2 images of exactly the same dimensions, positioned one on top of the other. Say they're called A and B (A is the top one). What I've done is made it so when you hover over A, its z-index decrements by 2 so that B is now on top, and B's hover: increments its z-index by 2 so it's now higher by 1 than A's original z-index (thus image B stays on top until you remove mouse). So basically...
#A {z-index: 5;}
#B {z-index: 4;}
#A:hover {z-index: 3;}
#B:hover {z-index: 6;}
This works perfectly in Firefox and Chrome, but IE doesn't want to hear about it, and my images keep spazzing while hovering over them. Any help is appreciated. Positioning is Absolute, if that matters.
@jklm313
That actually works in my IE9 as well. Maybe I should post the full code since one of my "images" is actually a social network button. So here it is:
HTML:
<div id="myTweetBrown"></div>
<div id="myTweet"><?php include ("myPHP/homepageSoc/tweet.php") ?></div>
CSS:
#myTweetBrown {
position: absolute;
background-image: url('../images/tweetBrown.png');
background-repeat: no-repeat;
background-position: center center;
height: 20px;
width: 54px;
left: 381px;
top: 662px;
z-index: 5;
}
#myTweetBrown:hover {
position: absolute;
z-index: 3;
}
#myTweet {
position: absolute;
height: 20px;
width: 54px;
left: 381px;
top: 662px;
z-index: 4;
}
#myTweet:hover {
position: absolute;
z-index: 6;
}
tweet.php:
<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://localhost/Joomla173/index.php?option=com_content&view=article&id=69&Itemid=507" data-count="none">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
Link to demo website: ***** -- scroll down to Tweet button
This will be up only for so long, because I don't want people to have access like that <.<
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在已经提供了源代码,我只想重写我的整个答案。
所有“现代”版本的 IE,当不处于怪异模式时,都可以完美地接受此代码以用于 div 和链接。 IE 中的问题出现在 iframe 和其他异常元素上,此时其渲染引擎似乎出现故障。 (震惊!) 您会无缘无故地看到这种闪烁,除了 iframe 和页面中可能存在冲突的文档类型之外,如果可能的话,我也会尝试避免这种情况。
假设此链接是由 twitter 生成的,我建议使用 IE 的后备方法。我不会在按钮图像和 twitter 提供的按钮图像之间徘徊,而是使用 javascript 操作 iframe 内提供的 twitter 按钮的 css。
该按钮看起来是由 HTML5 生成的,而不是静态图像,因此操作起来应该不难:
您可以采取的另一种方法是继续执行之前所做的操作,但应用不同的样式,如下所示,具体取决于展示:
Just going to rewrite my whole answer now the source code has been provided.
All "modern" versions of IE, when not in quirks mode, accept this code perfectly fine for divs and links. The problem in IE arises for iframes and other unusual elements, at which point its rendering engine seems to fail. (Shock!) You'll get this flickering for no apparent reason, except perhaps the conflicting doctypes in the iframe and page, which I would also try avoid if possible.
Presuming this link is generated by twitter, I would advise a fallback approach for IE. Instead of hovering between your button image and a twitter provided button image, I would just manipulate the css of the button twitter provided inside the iframe using javascript.
The button looks to be generated by HTML5 rather than being a static image, so it shouldn't be difficult to manipulate:
The other approach you could take is keep doing what you were doing before, but applying the styles differently like so, dependant on display:
从技术上讲,CSS 实际上并没有指定元素如何以及何时进入和退出“悬停”状态。因此,听起来当 A 进入 B 下方时,您的 IE 版本会从 A 中删除悬停状态,并立即弹出到 B 前面,然后 B 获得悬停状态并进一步弹出到前面。
将两者包装在一个 div 中,并测试其悬停状态怎么样?那有用吗?
http://jsfiddle.net/X64au/
Technically, CSS doesn't actually specify how and when elements go in and out of the "hover" state. So it sounds like when A goes under B, your version of IE removes the hover state from A and it immediately pops back in front of B, before B gets the hover state and pops further in front.
How about wrapping the two in a div, and testing for the hover state on that? Does that work?
http://jsfiddle.net/X64au/
尝试将它们包裹在 div 中
Try wrap them in a div