JavaScript Create sliding Next / Previous content VIA Ajax / JSON

This is quite an in depth question so I will try my best to explain in as much detail as possible.

I am creating a Wordpress game information website, there are rows of gamelistings which when clicked create a content box, the data within this element is loaded with Ajax from a pages JSON array.

enter image description here In my current set up when an element is clicked the contentbox data is simply updated, and the previous and next arrows simulate a click on the prev / next element to the currently active one.

The JSON data that is loaded includes 2 useful fields:

(data.previous_url)

(data.next)

I would like to use this data to create separate ajax functions that create previous and next content boxes to the active one. I would then like the prev / next arrows to slide to the appropriate content box.

THE MARKUP / WORDPRESS LOOP

    <?php  
    $counter = 0;
    if ( have_posts() ) : 

        while ( have_posts() ) : the_post();  $counter++; 
            $gameName = get_field('game_name');
            $gameLogo = str_replace(" ", "", $gameName);
            $gameLink = str_replace(" ", "-", $gameName);
            $gameReels = get_field('reels');
            $gamePaylines = get_field('paylines');
            $gameInfo = get_field('game_info');

    ?>



<div class="four columns gameListing" id="<?php echo $gameName ?>" data-count="<?php echo $counter; ?>" data-id="<?php echo $gameLink ?>">       
    <img class="gameLogo" src="<?php bloginfo('template_directory'); ?>/images/games/logos/<?php echo $gameLogo ?>.png" > 
    <div class="gameInfo">
        <h2 class="gameTitle"> <?php if($gameName): echo $gameName;  endif; ?></h2>
        <div class="gameRating">
            <i class="fa plusone fa-star"></i><i class="fa plusone fa-star"></i><i class="fa plusone fa-star"></i><i class="fa plusone fa-star"></i><i class="fa fa-star"></i> (5.0)
        </div>
    </div>
    <a class="gameCta" rel="" data-post-id="<?echo $post->ID ?>" ><span class="title">Click to Play</span></a>
</div>

    <?php endwhile; endif; ?>



<div class="clear"></div>


<div class="sixteen columns gameBox" id="gameBox" data-id="">

    <div id="gameBoxNav">
        <a class="prev controls"><i class="fa fa-chevron-left"></i></a>      
        <a class="next controls"><i class="fa fa-chevron-right"></i></a>
    </div>

    <div class="gameBoxContent">
        <div class="gameBox-L">
            <h3 class="gameBox-Header-Left heading"><span class='game-name'></span></h3>
                <div class="gameBox-Screenshots">
                    <div class="gameBox-L-Side" id='gameBoxGallery'></div>
                    <div class="gameBox-L-Main" id="gameBox-Screenshot"></div>
                    <div class="clear"></div>
                </div>
        </div>

        <div class="gameBox-R">
            <h3 class="gameBox-Header-Right heading">Play on any of these sites:</h3>
            <div id="close" class="controls">X</div>
            <div class="clear"></div>


            <div id="game_provs"></div>
        </div>
        <div class="clear"></div>

        <div class="gameBox-Details">
            <h3 class="gameBox-Header heading">All about <span class='game-name'></span></h3> 
            <ul class="gameDetailsBar">
                <li class="gameDetails">
                <span class="gameInfo-cellData" id="game-reels"></span>
                <span class="gameInfo-cellTitle">Reels</span>
                </li>
                <li class="gameDetails">
                <span class="gameInfo-cellData" id="game-paylines"></span>
                <span class="gameInfo-cellTitle">Paylines</span>
                </li>
                <li class="gameDetails">
                <span class="gameInfo-cellData" id="game-minBet"></span>
                <span class="gameInfo-cellTitle">Min Bet</span>
                </li>
                <li class="gameDetails">
                <span class="gameInfo-cellData" id="game-maxBet"></span>
                <span class="gameInfo-cellTitle">Max Bet</span>
                </li>
                <li class="gameDetails">
                <span class="gameInfo-cellData" id="game-jackpot"></span>
                <span class="gameInfo-cellTitle">Jackpot</span>
                </li>
                <li class="gameDev">
                <span gameDev-cellTitle>Developed By:</span>
                <span gameDev-cellData><img src="<?php bloginfo('template_directory'); ?>/images/devlogos/netent.png" alt=""></span>
                </li>     
            </ul>
            <div class="clear"></div>      

            <div class="gameInfo-Excerpt" id="game-info"></div>
        </div>
    </div>
</div>

The JS

// -------------- MAIN CLICK FUNCTION --------------
$('.gameListing').click(function () {

    $('.gameListing').removeClass('active');



    $(this).addClass('active');

    var id = $(this).attr('data-id');
    var url = "http://localhost:8888/projects/superfreerespo/" + id + "?json=get_category_posts&slug=games";

    // Pass the url and the clicked element
    call_ajax(url, this);
});


// -------------- GET PREV / NEXT ITEMS --------------
$('.prev').click(function () {

    var $current = $('.gameListing.active');
    var postNumber = parseInt($current.attr('data-count'));
    var nextPost = (postNumber - 1);

    if (nextPost != 0) {
        $current.removeClass('active')
        $("[data-count='" + nextPost + "']").trigger("click");
    }
});

$('.next').click(function () {
    var $current = $('.gameListing.active');
    var postNumber = parseInt($current.attr('data-count'));
    var nextPost = (postNumber + 1);


    var postCount = $('.gameListing').length; 

        if (nextPost != postCount + 1) {
                $current.removeClass('active')
                $("[data-count='" + nextPost + "']").trigger("click");
        }

});





// -------------- AJAX CALL FUNCTION  --------------
function call_ajax(url, elem) {

    $.ajax({
        url: url,
        method: "GET",
        data: {json: 1},
        dataType: "JSON"
    })


    // -------------- FUNCTIONS FOR AFTER AJAX DONE --------------
    .done(function (data) {

        // Append the box
        appendBox(elem);

        // LOAD GAMEBOX JSON DATA

        $("#game-name").html(data.post.title);
        $("#game-reels").html(data.post.custom_fields.reels);
        $("#game-paylines").html(data.post.custom_fields.paylines);
        $("#game-minBet").html(data.post.custom_fields.min_bet);
        $("#game-maxBet").html(data.post.custom_fields.max_bet);
        $("#game-jackpot").html(data.post.custom_fields.jackpot);
        $("#game-info").html(data.post.custom_fields.game_info);




             // -------------------------- YOU CAN SEE HERE THE NEXT / PREV DATA LOADED VIA THE ABOVE AJAX CALL
            var nextURL = (data.previous_url) + "?json=1";
            var prevURL = (data.next_url) + "?json=1";







        // -------------------------- LOAD GAME PROVIDERS VIA NEW AJAX LOAD
        var provSource = new String(data.post.custom_fields.game_name);
        provSource = provSource.replace(/ /g, "-");
        $("#game_provs").load("http://localhost:8888/projects/superfreerespo/" + provSource + "/ .gameBox-Ops");


        // -------------------------- LOAD GAME THUMBNALS 
        var gameThumbSrc = new String(data.post.custom_fields.game_name);
        gameThumbSrc = gameThumbSrc.replace(/ /g, '');
        $('#gameBoxGallery').html('');
        for (i = 0; i <= 2; i++) {
    image = '<img src="' + templateDir +'/images/games/screenshots/' + gameThumbSrc + '-' + i + '.jpg" class="gameThumb">'
            $('#gameBoxGallery').append(image);
        };
        // -------------------------- ZOOM FIRST THUMBNAIL
        $('#gameBox-Screenshot').html('');
        image = '<img src="'+ templateDir +'/images/games/screenshots/' + gameThumbSrc + '-0' + '.jpg" id="gameScreenshot">'
        $('#gameBox-Screenshot').append(image);

    });


    // -------------------------- CREATE GAMEBOX
function appendBox(elem) {
    var $chosen = $(elem),
        $gameBox = $('#gameBox'),
        top = $chosen.offset().top,
        $blocks = $chosen.nextAll('.gameListing');

    // -------------------------- TOGGLE GAMEBOX OPEN
    if($chosen.attr('data-id') === $gameBox.attr('data-id')) {
        $gameBox.stop().slideUp(500, function () {
            $(this).css('display', 'none');
        });
        $gameBox.attr('data-id', '');
    } 

    // -------------------------- TOGGLE GAMEBOX CLOSE
    else {
        $gameBox.slideDown(500, function () {
    $(this).css('display', 'inline-block');
        });
        $gameBox.attr('data-id', $chosen.attr('data-id'));
    }



    function placeAfter($block) {
        $block.after($gameBox);
    }
    if ($blocks.length == 0) {
        placeAfter($chosen);
        return false;
    }

    $blocks.each(function (i, j) {
        if ($(this).offset().top != top) {
            placeAfter($(this).prev('.gameListing'));
            return false;
        } else if ((i + 1) == $blocks.length) {
            placeAfter($(this));
            return false;
        }
    });


}


    $('html, body').animate({
        scrollTop: $('.active').offset().top - 40
    }, 600);


}
Answer:1

I have a little question. I try to build this simple quiz. Right now it looks like this: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"...

I have a little question. I try to build this simple quiz. Right now it looks like this: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"...

  1. jquery each element with class
  2. jquery each element with data attribute
  3. jquery each element in array
  4. jquery each element in div
  5. jquery each element loop
  6. jquery each element with id
  7. jquery each element index
  8. jquery each element find
  9. jquery each element value
  10. jquery each element click
  11. jquery each element with class name
  12. jquery each element with name
  13. jquery each element remove class
  14. jquery each element in form
  15. jquery each element starting with
  16. jquery each element attribute

Does anyone know if there's a way to add text into the tumblr ask box when the page is first loaded? I'm running a blog where people can ask for advice from either a girl or a guy. I want them to ...

Does anyone know if there's a way to add text into the tumblr ask box when the page is first loaded? I'm running a blog where people can ask for advice from either a girl or a guy. I want them to ...

  1. insert text into pdf
  2. insert text into excel
  3. insert text into picture
  4. insert text into imovie
  5. insert text into excel formula
  6. insert text into photo
  7. insert text into cell vba
  8. insert text into photoshop
  9. insert text into excel cell
  10. insert text into a shape in word
  11. insert text into string python
  12. insert text into word document
  13. insert text into picture in word
  14. insert text into equation latex
  15. insert text into video
  16. insert text into premiere
  17. insert text into image
  18. insert text into file linux
  19. insert text into pdf free
  20. insert text into html

I need to validate number that use inputs into input box. Number must be exactly 12 characters long and start with 931. var reg = /^931[0-9]$/; var val = $("#sms_number").val(); if (val.length === ...

I need to validate number that use inputs into input box. Number must be exactly 12 characters long and start with 931. var reg = /^931[0-9]$/; var val = $("#sms_number").val(); if (val.length === ...

  1. validate jquery regex
  2. jquery validate regex rule
  3. jquery validate regex pattern
  4. jquery validate regex special characters
  5. jquery validate regex example
  6. jquery validate regex email
  7. jquery validate regex password
  8. jquery validate regex not working
  9. jquery validate regex message
  10. jquery validate regex method
  11. jquery validation regex phone number
  12. jquery validation regex alphanumeric
  13. jquery validation regex check
  14. jquery validation regex number
  15. validate url jquery regex
  16. form validation jquery regex
  17. custom validation jquery regex
  18. date validation jquery regex
  19. jquery validate space regex
  20. jquery validate url regex without http

I use pywebkit and html5 to develope desktop map server. Map tiles store in sqlite database. So when I set html img's src, I have two options. One is read image from database and b64encode it, ...

I use pywebkit and html5 to develope desktop map server. Map tiles store in sqlite database. So when I set html img's src, I have two options. One is read image from database and b64encode it, ...