问题
So I am trying to animate my navigation links background which is Css sprite. this picture: http://img689.imageshack.us/img689/2996/menufc.png
$(document).ready(function(){
$('nav a')
// On mouse over, move the background on hover
.mouseover(function(){
$(this).stop().animate({backgroundPosition: '0px ' +$(this).attr('data-one')+'px';}, 500);
})
// On mouse out, move the background back
.mouseout(function(){
$(this).stop().animate({backgroundPosition: '0px ' +$(this).attr('data-two')+'px';}, 500);
})
});
This is th jQuery which should do the job. Also here I have the HTML:
<header id="menu">
<h1>dawe's portfolio</h1>
<nav>
<a id="m_portf" data-one="0" data-two="-37" href="#portfolio">portfolio</a>
<a id="m_music" data-one="-72" data-two="-111" href="#music">music</a>
<a id="m_about" data-one="-148" data-two="-185" href="#about">about</a>
<a id="m_contact" data-one="-222" data-two="-259" href="#contact">contact</a>
</nav>
</header>
I don' know what the problem is. I have tried to do it in so many other ways. I think this one is the closest to the real solution, I put two types of codes together which I found. I was wondering whether the problem might be that I didn't define backgrounds position as absolute, relative, etc. (is it even possible?) As far as I am concerned for jquery animation requires the definition of iamge's position. Or there might be any other errors in my code. I don't know please help me. Here is my css too:
#m_portf{
background: #fff url('menu.png')repeat-X 0px -37px;
}
#m_music{
background: #fff url('menu.png')repeat-X 0px -111px;
}
#m_about{
background: #fff url('menu.png')repeat-X 0px -185px;
}
#m_contact{
background: #fff url('menu.png')repeat-X 0px -259px;
}
回答1:
For Background image position animation you need to use "jquery.backgroundpos.js" You can download it from here http://keith-wood.name/js/jquery.backgroundpos.js
and the following code is working fine like this
CSS
body{
background-color:#ccc;
}
nav a{
background-image:url(http://img689.imageshack.us/img689/2996/menufc.png);
display:block;
width:120px;
height:22px;
margin-bottom:10px;
}
#m_portf{
background-position:0 -37px;
}
#m_music{
background-position:0 -111px;
}
#m_about{
background-position:0 -185px;
}
#m_contact{
background-position:0 -259px;
}
JQuery
$(document).ready(function(){
$('nav a').hover(
function () {
$(this).addClass('active');
$(this).stop().animate({backgroundPosition: '0px ' +$(this).attr('data-one')+'px'}, 500);
},
function () {
$(this).removeClass('active');
$(this).stop().animate({backgroundPosition: '0px ' +$(this).attr('data-two')+'px'}, 500);
}
);
});
Html
<header id="menu">
<h1>dawe's portfolio</h1>
<nav>
<a id="m_portf" data-one="0" data-two="-37" href="#portfolio">portfolio</a>
<a id="m_music" data-one="-72" data-two="-111" href="#music">music</a>
<a id="m_about" data-one="-148" data-two="-185" href="#about">about</a>
<a id="m_contact" data-one="-222" data-two="-259" href="#contact">contact</a>
</nav>
</header>
Here is the working file Link
来源:https://stackoverflow.com/questions/15173719/jquery-background-position-animation-with-css-sprites-not-working