使用 jQuery 更改背景位置
我想在悬停 li 元素时更改 CSS 类的背景位置。
HTML:
<div id="carousel">
<ul id="submenu">
<li>Apple</li>
<li>Orange</li>
</ul>
</div>
CSS:
#carousel {
float: left;
width: 960px;
height: 360px;
background: url(../images/carousel.png);
}
关于如何做到这一点有什么建议吗?
I'd like to change the background position of a CSS-class while hovering a li-element.
HTML:
<div id="carousel">
<ul id="submenu">
<li>Apple</li>
<li>Orange</li>
</ul>
</div>
CSS:
#carousel {
float: left;
width: 960px;
height: 360px;
background: url(../images/carousel.png);
}
Any suggestions on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
干得好:
Here you go:
你们让事情变得复杂了。您可以通过 CSS 简单地做到这一点。
You guys are complicating things. You can simple do this from CSS.
上面的反叛答案实际上不起作用,因为对于CSS来说,“background-position”实际上是“background-position-x”和“background-position-y”的简写,所以他的代码的正确版本是
:我花了四个小时用头撞它,才意识到这个令人恼火的事实。
rebellion's answer above won't actually work, because to CSS, 'background-position' is actually shorthand for 'background-position-x' and 'background-position-y' so the correct version of his code would be:
It took about 4 hours of banging my head against it to come to that aggravating realization.
当
submenu
div 中的 li 悬停时,为carousel
div 上的backgroundPosition
设置新值。悬停结束时删除backgroundPosition
并将backgroundPosition
重置为旧值。Sets new value for
backgroundPosition
on thecarousel
div when a li in thesubmenu
div is hovered. Removes thebackgroundPosition
when hovering ends and resetsbackgroundPosition
to old value.