Firefox 中的 CSS Sprite 兼容性

发布于 2024-12-02 15:51:48 字数 448 浏览 0 评论 0原文

我有一个使用下面的 css 类的 div 元素。基本上,我正在创建一个在悬停时发生变化的精灵图像链接。

.home {
    background: url('images/home.png') 0 0;
    width: 150px;
    height: 188px;
    border: none;   
}

.home:hover {
    background-position: -150px 0;  
}

当我用不同的浏览器测试它时,它似乎在大多数浏览器上运行良好,除了某些版本的 Firefox。悬停时,它不会切换 img 位置。正如我从 w3c 中读到的,为了在 Firefox 中工作,“background-attachment”属性为“fixed”。我这样做了并将属性添加到两个类中,但仍然不起作用。当我添加该属性时,图像居中,所有不在指定宽度和高度范围内的内容都会被切除。

I have a div element that uses the css classes below. Basically, I'm creating a sprite image link that changes on hover.

.home {
    background: url('images/home.png') 0 0;
    width: 150px;
    height: 188px;
    border: none;   
}

.home:hover {
    background-position: -150px 0;  
}

When I tested it with different browsers, it seemed to work well with most browsers except for some versions of Firefox. On hover, it doesn't switch the img position. As I read from w3c for this to work in Firefox, the "background-attachment" property to "fixed". I did this and added the property to both classes and still didn't work. When I added the property, the image was centered, and whatever was not within the specified width and height were cut off.

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

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

发布评论

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

评论(4

梦里兽 2024-12-09 15:51:49
 background-position: 0px 0px;
 background-position: -150px 0px;
 background-position: 0px 0px;
 background-position: -150px 0px;
远山浅 2024-12-09 15:51:48

这是没有看到问题的猜测,但有时使用后台速记可能会在 FF 中出现错误:

尝试:

.home {
  backround-image: url('images/home.png');
  background-position: 0 0;
  height: 188px;
  border: none;
}

.home:hover {
  background-position: -150px 0;
}

This is a guess without seeing the problem, but sometimes using the background shorthand can be buggy in FF:

try :

.home {
  backround-image: url('images/home.png');
  background-position: 0 0;
  height: 188px;
  border: none;
}

.home:hover {
  background-position: -150px 0;
}
踏雪无痕 2024-12-09 15:51:48

您必须为背景图像设置no-repeat。 Firefox 在使用 background-position 时可以正常工作,但 background-position-xbackground-position-y 则无法使用。我不知道您愿意对哪个元素执行什么操作,但它应该仅适用于块元素。如果不是块元素,则需要 display: block

.home {
    background: url('images/home.png') no-repeat 0 0;
    width: 150px;
    height: 188px;
    border: none;   
    display: block;
}

You have to have no-repeat for background image. Firefox works fine with background-position but background-position-x and background-position-y wont work. I don't know what you are willing to do for which element, but it should work on block elements only. If it's not a block element, display: block is required

.home {
    background: url('images/home.png') no-repeat 0 0;
    width: 150px;
    height: 188px;
    border: none;   
    display: block;
}
谜泪 2024-12-09 15:51:48

糟糕,看来我忘记在文件开头添加 DOCTYPE 标记,这导致了 FF 中无法正常工作。

添加

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

并成功了!

Oops, it looks like I forgot to add the DOCTYPE tag at the beginning at the file, which contributed to the thing not working in FF.

Added

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

and it worked!

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