简体   繁体   English

使用animate()的jQuery动画

[英]Jquery animation using animate()

I am trying to animate background images on my site, when 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. 根据您的附加说明,一个容器一次只能有一个背景,因此在jquery中设置背景时,您还将删除旧的背景。

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. 为了获得所需的效果,您将需要在#wrapper旁边插入一个附加元素并滑动它,或者将背景拼接成一个精灵图像,然后可以根据需要在单个背景周围滑动。

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: 如果是这样,那么您可能只是运行了附加了处理程序的代码太早(在DOM准备就绪之前),请将代码包装在该事件的处理程序中,如下所示:

$(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. 为原始背景设置动画,使其沿您想要的方向移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM