如何在 html/css 中使用 sprite 图像并使用 jQuery 切换更改

发布于 2024-10-03 03:13:15 字数 650 浏览 0 评论 0原文

我尝试以这种方式使用它:

CSS

#sprite2 {
   background: url(../images/csg_sprite.png) no-repeat top left;
}

.collapse_down{ background-position: 0 0; width: 16px; height: 16px; } 
.collapse_up{ background-position: 0 -66px; width: 16px; height: 16px;  } 

HTML

<div id="sprite2"><span id="splink1" class="collapse_down">Defaults</span>

CHANGE by jQuery

$('#splink1').attr("class", 'collapse_up');

我面临的问题是图像应用于 div=sprite2而不是我想要的跨度。

如何修复文本左侧的问题以显示精灵,以及当我切换它时将图像更改为向上。

I am trying to use it this way:

CSS

#sprite2 {
   background: url(../images/csg_sprite.png) no-repeat top left;
}

.collapse_down{ background-position: 0 0; width: 16px; height: 16px; } 
.collapse_up{ background-position: 0 -66px; width: 16px; height: 16px;  } 

HTML

<div id="sprite2"><span id="splink1" class="collapse_down">Defaults</span>

CHANGE by jQuery

$('#splink1').attr("class", 'collapse_up');

The problem I face is that the image is applied for the div=sprite2 and not for the span I want for.

How to fix so on the left of the text to display the sprite, and when I toggle it, to change the image to up.

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

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

发布评论

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

评论(2

林空鹿饮溪 2024-10-10 03:13:15
.sprite2 {
   background: url(../images/csg_sprite.png) no-repeat top left;
   display:inline-block;
}

<div  id="splink1" class="sprite2 collapse_down"></div>Defaults

仅当添加了包含背景图像的 id 或 class 时,才应用背景位置属性。

.sprite2 {
   background: url(../images/csg_sprite.png) no-repeat top left;
   display:inline-block;
}

<div  id="splink1" class="sprite2 collapse_down"></div>Defaults

background position property applied only when id or class which includes background image added to it .

意中人 2024-10-10 03:13:15

很简单:

将 CSS 从这样:更改

#sprite2 {
   background: url(../images/csg_sprite.png) no-repeat top left;
}

为这样:

#sprite2 span {
   background: url(../images/csg_sprite.png) no-repeat top left;
}

Pretty simply:

Change your CSS from this:

#sprite2 {
   background: url(../images/csg_sprite.png) no-repeat top left;
}

to this:

#sprite2 span {
   background: url(../images/csg_sprite.png) no-repeat top left;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文