4

I want to change the background image position in jquery animation. But my code is not working. Can any one help me to short it out.

Followings are my code

Css

#pnav a{
    background:url(http://www.us-bingo.com/images/Tickets/Solid-Color-Tyvek-Wrist-Tickets-lg.gif) 0 0 no-repeat;
    display:block;
    width:20px;
    height:42px;
}

Jquery

$('#pnav a')
.hover(function () {
    $(this).stop().animate({
        'background-position' : '(0px -42px)'
    }, 150);
}, function () {
    $(this).stop().animate({
        'background-position' : '(0 0)'
    }, 150);
});

html

<div id="pnav">
<a href="#">123</a>
</div>

this is the Fiddle file

Thank you in advance.

Roy Sonasish
  • 4,571
  • 20
  • 31
  • i guess , you might not have included your script inside document.ready. that's why it is getting called when the document is not ready. I've updated my code. and its a simple html. its working. – Manish Mishra Feb 25 '13 at 09:56

4 Answers4

4

I think that jQuery animation can't animate background-position, but you can use CSS transitions instead

    #pnav a{
    background:url(http://www.us-bingo.com/images/Tickets/Solid-Color-Tyvek-Wrist-Tickets-lg.gif) 0 0 no-repeat;
    display:block;
    width:20px;
    height:42px;

    -webkit-transition: background-position 150ms ease-in-out;
    -moz-transition: background-position 150ms ease-in-out;
    -ms-transition: background-position 150ms ease-in-out;
    -o-transition: background-position 150ms ease-in-out;
    transition: background-position 150ms ease-in-out;
}

#pnav a:hover {
    background-position:0px -42px;

}

here's a fiddle: http://jsfiddle.net/pavloschris/eg5zC/7/ and the transition browser combatibility : http://caniuse.com/#search=transition

xpy
  • 5,481
  • 3
  • 29
  • 48
2

For Background image position animation we need to use "jquery.backgroundpos.js" You can download it from here http://keith-wood.name/js/jquery.backgroundpos.js

$(document).ready(function(){

$('#pnav a')
.hover(function () {
   $(this).stop().animate({backgroundPosition: '0px ' + '35px'}, 500);
}, function () {
    $(this).stop().animate({backgroundPosition: '0px ' + '0px'}, 500);
});


});

here is the updated jsFiddle file

Flot2011
  • 4,601
  • 3
  • 44
  • 61
Roy Sonasish
  • 4,571
  • 20
  • 31
0

Replace your line with this ,

background:url('http://www.us-bingo.com/images/Tickets/Solid-Color-Tyvek-Wrist-Tickets-lg.gif') no-repeat ;

Add inverted commas for URL in CSS

EnterJQ
  • 1,014
  • 8
  • 18
0

inside animate function, background-position doesn't work like that. use this instead

$('#pnav a')
.hover(function () {
    $(this).animate({
        'background-position-x': '10%',
        'background-position-y': '20%'
    }, 550);
}, function () {
    $(this).animate({
       'background-position-x': '0%',
       'background-position-y': '0%'
    }, 550);
});

:ref

however, what you want can be achieved through CSS3.0 as 'xpy' pointed out, but it won't run on IE

Update:

below is working sample:

<html>
    <head>
        <script language="javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
        <script>
         $(document).ready(function(){
          $('#pnav a')
                    .hover(function () {
                        $(this).animate({
                            'background-position-x': '10%',
                            'background-position-y': '20%'
                        }, 550);
                    }, function () {
                        $(this).animate({
                           'background-position-x': '0%',
                           'background-position-y': '0%'
                        }, 550);
                    });
            });
        </script>
       <style>
            #pnav a{
                background:url(http://www.standard-icons.com/stock-icons/standard-road/scooter-icon.gif) 0 0 no-repeat;
                display:block;
                width:20px;
                height:42px;
            }
       </style>

    </head>
    <body>
            <div id="pnav">
              <a href="#">123</a>
           </div>
    </body>
</html>

Fiddle

Community
  • 1
  • 1
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59