开发者

Jquery animation using animate()

开发者 https://www.devze.com 2022-12-16 14:05 出处:网络
I am trying to animate background images on my site, when开发者_JS百科 a background image is selected I want it to slide in from the left and push the old background out of picture, can anybody help w

I am trying to animate background images on my site, when开发者_JS百科 a background image is selected I want it to slide in from the left and push the old background out of picture, can anybody help with the code as the stuff I have written does not work,

    <script type="text/javascript">
$("a.background_btn").click(function(ev){
    ev.preventDefault();
//  alert("hello");
    var url = $(this).attr("href");
//  alert(url);
    $.ajax ({
        url : url, 
        type: "POST",
        success : function (data) {
            $('#wrapper').css('background', 'url(/media/uploads/backgrounds/'+(data)+')');
            $('#wrapper').css('background-position', '-1000px 0px');
            $('#wrapper').css('background-repeat', 'no-repeat');
            $('#wrapper').animate({backgroundPosition: '0px 0px'}) 
        }
    })
});


Based on your additional note, a container can only have one background at a time, so when you are setting the background in jquery, you are also removing the old background.

To get the effect you're wanting, you will either need to insert an additional element "beside" #wrapper and slide it in, or stitch your backgrounds together as a sprite image and then you can slide the single background around as needed.

That is, if you want the new background to literally "push" the old one out. If you are ok with the old background fully exiting and then the new one entering, Brandon's point may be the easiest.


What doesn't work about it? Does it just do nothing? If so then you are probably just running your code that attached the handler too soon (before the DOM is ready), wrap your code in a handler for that event, like so:

$(document).ready( function() {
  .. your code here ...
} );


before you change the background with this line

$('#wrapper').css('background', 'url(/media/uploads/backgrounds/'+(data)+')');

animate the original background to the direction you want it to go.

0

精彩评论

暂无评论...
验证码 换一张
取 消