Roy Sonasish
Roy Sonasish

Reputation: 4599

Background image position change in jquery animation

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.

Upvotes: 4

Views: 6785

Answers (4)

Roy Sonasish
Roy Sonasish

Reputation: 4599

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

Upvotes: 2

Manish Mishra
Manish Mishra

Reputation: 12375

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

Upvotes: 0

xpy
xpy

Reputation: 5621

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

Upvotes: 4

EnterJQ
EnterJQ

Reputation: 1014

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

Upvotes: 0

Related Questions