slide div offscreen, append content with ajax call, slide div onscreen

走远了吗. 提交于 2020-01-05 11:02:11

问题


I have a div which is centred on the page that contains text loaded through an jquery ajax call. When a button is clicked it refreshes the content of that DIV:

randomEntry = function() {
    $.ajaxSetup({
        cache: false
    });

    $('article#portraits').load('random.php');
    $('#refresh').click(function() {
        event.preventDefault();
        $('article#portraits').load('random.php');
    });
};​

What I would now like to do is animate the div when the button is pressed, in the following order:

  1. button is pressed
  2. div moves off screen from right to left
  3. div content is updated
  4. div with new content moves on screen from right to left.

This example illustrates the visual effect perfectly. I have looked at the code and am able to replicate the effect with two divs, each containing unique content, but am struggling to load the ajax call into the second div.

This is what I have so far – it's clearly incorrect, as the new content loads before the div begins to move (and doesn't come back!):

$('#refresh').click(function() {
    event.preventDefault();
    $('article#portraits').animate({ 'margin-left' : "-100%" },1500);
    $('article#portraits').load('random.php');
    $('article#portraits').animate({ 'margin-right': "-100%" }, 1500);
};

I suspect load() isn't the right function here, and that perhaps there needs to be a second empty div to load the content into, but I'm a bit stumped. A jsfiddle with the html/css can be found here. Many thanks.


回答1:


You need to use the "complete" callbacks:

$('#refresh').click(function() {
    event.preventDefault();
    $('article#portraits').animate({ 'margin-left' : "-100%" },1500, function() {
        $('article#portraits').load('random.php', function() {
            $('article#portraits').animate({ 'margin-right': "-100%" }, 1500);
        );
    );
};



回答2:


Looking more closely at the original example I posted, and at @Blazemonger's answer, the following seems to achieve the desired result:

  1. Load random.php into #box1:

    randomEntry = function() {
        $.ajaxSetup({
        cache: false
        });
        $('#box1').load('random.php');
    };http://www.readability.com/mrfredhale/
    
  2. Move #box1 off-screen when clicked

    animateBox = function() {
        $('.box').click(function() {
            $(this).animate({
                left: '-50%'
            }, 500, function() {
                $(this).css('left', '150%');
                $(this).appendTo('article#portraits');
            });
    
  3. Load random.php into #box2 and move it on-screen. Using $(this.next) means that when #box2 is in focus random.php loads into #box1, and vice versa.

        $(this).next().load('random.php').animate({
            left: '50%'
        }, 500);
        });
    };
    


来源:https://stackoverflow.com/questions/10743250/slide-div-offscreen-append-content-with-ajax-call-slide-div-onscreen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!