更改背景位置 - 使用 jQuery 或 :hover 与 Selectivizr 更好?

发布于 2024-10-09 01:54:44 字数 377 浏览 0 评论 0原文

我已经用 jQuery 进行后台翻转有一段时间了,如下所示:

 $("#menusearchbutton").hover(function(){$(this).css('backgroundPosition', '0px -24px');},function(){$(this).css('backgroundPosition', '0px 0px');});

但我最近遇到了 Selectivizr,它允许在 IE6-8 中使用伪类,其中包括 :hover。

这会是比使用 jQuery 更好的方法吗?当然,它们都是 JS,因此它无法绕过用户启用 JavaScript 的要求。但除此之外,使用其中一种而不是另一种有什么好处吗?

干杯:)

I've been doing background rollovers with jQuery for a while now, like this:

 $("#menusearchbutton").hover(function(){$(this).css('backgroundPosition', '0px -24px');},function(){$(this).css('backgroundPosition', '0px 0px');});

but I recently came across Selectivizr which allows the use of pseudoclasses in IE6-8, which would include :hover.

Would that be a better method than using jQuery? They're both JS, of course, so it doesn't get round the requirement for users to have javascript enabled. But apart from that, is there any benefit to be gained from using one instead of the other?

Cheers :)

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

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

发布评论

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

评论(2

灯角 2024-10-16 01:54:44

:hover 已经在 IE7+ 中工作,IE6 只允许它与 元素一起使用...所以我想说你已经可以在没有库的情况下单独使用 CSS,只要 IE6 不是问题。

#menusearchbutton { background-position: 0px 0px; }
#menusearchbutton:hover { background-position: 0px -24px; }

:hover already works in IE7+, IE6 only allows it with a <a> elements...so I'd say you can already use CSS alone with no library, as long as IE6 isn't a concern.

#menusearchbutton { background-position: 0px 0px; }
#menusearchbutton:hover { background-position: 0px -24px; }
翻身的咸鱼 2024-10-16 01:54:44

尼克是对的:您应该使用本机 :hover 功能。
有几点需要注意:如果 没有 href="" 属性,则悬停效果在 IE 中无法正常工作。别忘了。

另外,我们经常希望在 div 上有悬停效果。这是因为div的默认显示。如果您想让 标记看起来像 div,请在 CSS 中使用 display:block

Nick is right : you should use native :hover capabilities.
Just a few things to note : The hover effect doesn't work well in IE if the <a> doesn't have a href="" attribute. don't forget it.

Also, we often want to have a hover effect on a div. It's because of the default displaying of divs. If you want to have a <a> tag to look as a div, use display:block in your CSS.

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