使用jQuery animate增加背景位置-x

Increment background-position-x using jQuery animate?

本文关键字:位置 背景 增加 jQuery animate 使用      更新时间:2023-09-26

是否可以使用jQuery animate来设置div背景位置-x的动画?

理想情况下,单击后背景位置-x将增加20%。

这是我到目前为止的代码:

$('.cycle').click(function() {
    $(this).animate({ 'background-position-x': '+20%' }, 500, 'linear');
});

但它只适用于第一次点击。

谢谢!

试试这个

$('.cycle').on('click', function() {
    $(this).animate({ 'background-position-x': '+=20%' }, 500, 'linear');
});

使用类似的东西:

   $(function() {
      $('.element').css("backgroundPosition","0px 0px").animate({"backgroundPosition":"-200px 10px"});
    });