如何制作 CSS 翻转图像? (精灵)

发布于 2024-09-27 23:01:05 字数 280 浏览 0 评论 0原文

我只是想用我在一本网页设计杂志上看到的这个小技巧来制作一些图像翻转,但我遇到了一点麻烦。我的尝试是一次可怕的失败,哈哈。我只想看到上半部分(42 像素高),然后翻转时的下半部分(显然是-42 像素)

宽度也是 42 像素。有人可以写一些东西来实现这一点吗? 图像: http://img826.imageshack.us/img826/6568/homebi.png

I'm just trying to use this little trick I saw in one of my web design magazines to make a little image rollover but I'm having just a small bit of trouble. My attempt was a terrible fail lol. I just want to see the top half (42px tall) and then the bottom half on rollover (-42px obviously)

width is also 42px. Could someone write something up to make that happen?
image:
http://img826.imageshack.us/img826/6568/homebi.png

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

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

发布评论

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

评论(3

心房的律动 2024-10-04 23:01:05

这一切都与 background-position 的初始值(非 :hover)和最终值(:hover)有关。

#test {
    height: 42px;
    width: 42px;
    background-image: url(http://img826.imageshack.us/img826/6568/homebi.png);
    background-repeat: no-repeat;
    background-color: transparent;
    background-position: top;  /* <-- key step #1 */
}
#test:hover {
    background-position: bottom; /* <-- key step #2 */
}

演示


根据您的评论:用锚点包装

  1. 替换为 。这是因为锚点的有效子元素必须是内联元素
  2. ,但是内联显示的元素不会接受尺寸!因此,请使用跨度上的一个附加 CSS 属性来解决这一新问题:display: inline-block

演示 2

It's all about the initial (non-:hover) and final (:hover) values of background-position.

#test {
    height: 42px;
    width: 42px;
    background-image: url(http://img826.imageshack.us/img826/6568/homebi.png);
    background-repeat: no-repeat;
    background-color: transparent;
    background-position: top;  /* <-- key step #1 */
}
#test:hover {
    background-position: bottom; /* <-- key step #2 */
}

Demo


As per your comment re: wrapping the <div> with an anchor (<a>), here's what to do:

  1. Swap the <div> out for a <span>. This is because valid children of anchors must be inline elements
  2. But inline-displayed elements won't behave accept dimensions! So, fix this new problem with one additional CSS property: display: inline-block on the span.

Demo 2

迷迭香的记忆 2024-10-04 23:01:05

试试这个:

<style type="text/css">
.menu {
}
.menu a {
    float: left;
    display: inline;
    width: 42px;
    height: 42px;
    text-decoration: none;
}
.menu a span {
    display: block;
    overflow: hidden;
    height: 0;
}
.menu .home {
    background: transparent url(http://img826.imageshack.us/img826/6568/homebi.png) 0 0 no-repeat;
}
.menu .link:hover {
    background-position: 0 -42px;
}
</style>

<div class="menu">
    <a href="#" title="Home" class="link home"><span>Home</span></a>
</div>

Try this:

<style type="text/css">
.menu {
}
.menu a {
    float: left;
    display: inline;
    width: 42px;
    height: 42px;
    text-decoration: none;
}
.menu a span {
    display: block;
    overflow: hidden;
    height: 0;
}
.menu .home {
    background: transparent url(http://img826.imageshack.us/img826/6568/homebi.png) 0 0 no-repeat;
}
.menu .link:hover {
    background-position: 0 -42px;
}
</style>

<div class="menu">
    <a href="#" title="Home" class="link home"><span>Home</span></a>
</div>
话少心凉 2024-10-04 23:01:05

这是图像翻转的基本框架。

css

.rollover{display:block; height:42px; width:42px; background:url(http://img826.imageshack.us/img826/6568/homebi.png) top;}
.rollover:hover{background-position:bottom;}
.rollover span{display:none}

html

<a href="#" class="rollover"><span>Home</span></a>

重要的部分是背景位置,在按钮上正常状态设置为“顶部”,当您鼠标悬停时背景位置为“底部”。

假设包含两个按钮状态的图像的高度为 84 像素,这样就可以正常工作。

Heres the bare bones for an image rollover.

the css

.rollover{display:block; height:42px; width:42px; background:url(http://img826.imageshack.us/img826/6568/homebi.png) top;}
.rollover:hover{background-position:bottom;}
.rollover span{display:none}

The html

<a href="#" class="rollover"><span>Home</span></a>

The important part is the background position, which on the buttons normal state is set to 'top', when you rollover the background postion is 'bottom'.

Assuming your image which contains both button states is 84px high this will work fine.

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