2

I'm not that good with jQuery animations, but i'm trying to animate an background image when mouse enters on its element. The animation is simple, mouse enters, the image moves a little to the left. Mouse leaves, image returns to its position.

I could have that working on Chrome, but with a different behaviour in IE. FF doesn't event move anything. My is the following:

$(".arrow").on("mouseenter", function()
{
    $(this).stop(true).animate({ "background-position-x": "75%" });
}).on("mouseleave", function()
{
    $(this).stop(true).animate({ "background-position-x": "50%" });
});

Where .arrow is a div with these properties:

.arrow {
    width: 50px;
    padding: 10px 0;
    background: url(http://upload.wikimedia.org/wikipedia/commons/4/45/Right-facing-Arrow-icon.jpg) no-repeat center;
    background-size: 16px
}

And here is a demo.

What is most strange for me is the case of IE. It seems that the animation start always from left to right, not middle right. It occours when mouses leaves too. Any ideas ?

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105

2 Answers2

4

Because Firefox doesn't support backgroundPositionX, but it does support background position

Try this code in firefox and InternetExplorer:

$(".arrow").on("mouseenter", function()
{
    $(this).stop(true).animate({ "background-position": "75%" });
}).on("mouseleave", function()
{
    $(this).stop(true).animate({ "background-position": "50%" });
});

More Info: backgroundPositionX not working on Firefox

Here is Updated Demo working well with FF and IE

Community
  • 1
  • 1
Manwal
  • 23,450
  • 12
  • 63
  • 93
2

I know Manwal has solved it , but it can also be done very easily in CSS3 like so:

    .arrow {
        float:left;
        width: 50px;
        padding: 10px 0;
        background: url(http://upload.wikimedia.org/wikipedia/commons/4/45/Right-facing-Arrow-icon.jpg) no-repeat center;
        background-size: 16px;
        transition: all 2s ease-in-out;
    }

        .arrow:hover {

            transform :translateX(50px);
        }

where translate 50px will be the value you wish to move it along the X axis

EaziLuizi
  • 1,557
  • 1
  • 16
  • 25