JQuery - 背景图像缓慢变化
我正在尝试使我的悬停动作随着背景图像的缓慢变化而变化。
.menu_part
{
border-left: 1px solid black;
background-image: url(../images/bg_menu.png);
float:left;
padding: 2px 10px;
}
.menu_part:hover
{
background-image: url(../images/bg_menu_hove.png);
color: #FFF;
}
菜单:
<div id="head_menu">
<a href="#order"><div>make order</div></a>
<a href="#portfolio"><div>portfolie</div></a>
<a href="#contacts"><div>contacts</div></a>
<a href="#vacancies"><div>vacancies</div></a>
<a href="#about"><div>about company</div></a>
</div>
一些 JQuery:
$('#head_menu a').addClass('menu_part');
现在我正在尝试为选择器 $('#head_menu a') 编写悬停操作。我可以将背景图像更改为悬停时需要的缓慢吗?
这是我的尝试代码:
$("#head_menu a").hover(
function() {
$(this).animate({backgroundImage: 'url(images/bg_menu_hove.png)', color: '#fff'}, 1000);
},
function() {
$(this).animate({backgroundImage: 'url(images/bg_menu.png)', color: '#000'}, 1000);
}
);
但它现在甚至不显示菜单。我做错了什么?我也会尝试使用背景位置。
这是带有 bg-position 的代码,我无法理解。我将 bg_menu.png 和 bg_menu_hove.png 合并为 1 张 200px+200px 的图像。 即使没有 JQuery,上面的样式也不起作用。
.menu_part:hover
{
background-image: url(../images/new_menu.png);
background-position: 0 -200px;
}
I'm trying to make my hover-action with slowly bg-image changing.
.menu_part
{
border-left: 1px solid black;
background-image: url(../images/bg_menu.png);
float:left;
padding: 2px 10px;
}
.menu_part:hover
{
background-image: url(../images/bg_menu_hove.png);
color: #FFF;
}
Menu:
<div id="head_menu">
<a href="#order"><div>make order</div></a>
<a href="#portfolio"><div>portfolie</div></a>
<a href="#contacts"><div>contacts</div></a>
<a href="#vacancies"><div>vacancies</div></a>
<a href="#about"><div>about company</div></a>
</div>
Some JQuery:
$('#head_menu a').addClass('menu_part');
Now I'm trying to write hover-action for selector $('#head_menu a'). Could i change bg-image to need, when hover, slowly?
Here is my trying-code:
$("#head_menu a").hover(
function() {
$(this).animate({backgroundImage: 'url(images/bg_menu_hove.png)', color: '#fff'}, 1000);
},
function() {
$(this).animate({backgroundImage: 'url(images/bg_menu.png)', color: '#000'}, 1000);
}
);
But it even doesn't show menu now. What i did wrong? And I'll also try to use bg-position.
Here is code with bg-position, I can't understand. I merged bg_menu.png and bg_menu_hove.png into 1 image 200px+200px.
Style above doesn't work even without JQuery.
.menu_part:hover
{
background-image: url(../images/new_menu.png);
background-position: 0 -200px;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,背景变化的悬停效果的标准技术是合并两张图片。因此,将
bg_menu_hove.png
粘贴到bg_menu.png
之后,只需调整背景位置即可。从
a
标签中删除div
(div 是块元素,a 是内联元素......反之亦然)。现在,如果我们只有一张图像,当然可以使用
jQuery
中的animate
函数 (backgroundPosition
)。看一下很好的例子,对于初学者来说非常有用的网站:http://visualjquery.com/
(点击:
效果 -> 自定义 -> 动画
)First of all, standard technics for hover effects with changing background is merge both of pictures. So, stick
bg_menu_hove.png
tobg_menu.png
after that, just play with position of background.Remove
div
froma
tag (div is block element and a is inline... it could be vice versa).Now if we have only one image, use for example
animate
function fromjQuery
of course (backgroundPosition
).Take a look on nice example, and really useful site for begginer: http://visualjquery.com/
(click:
Effects -> Custom -> Animate
)我认为您可以使用 jquery animate 函数来做到这一点,并对CSS - 类似于:
然后:
目前无法测试它,抱歉 - 但你已经了解了基本的想法。
I think you might be able do this with the jquery animate function, and some slight changes to the CSS - something like:
And then:
Couldn't test it at the moment, sorry - but you get the basic idea.