0

I'm sure there is a quick and easy answer to this, it's just not coming to me right now. I have built custom html/css/js for a slider that pulls its content from an array like this:

$slides = [
    [
        'img'       => 'image1.jpg',
        'text'  => 'Slide One text',
        'svg'       => 'cat1'
    ],
    [
        'img'       => 'image2.jpg',
        'text'  => 'Slide Two Text',
        'svg'       => 'cat2'
    ],
    // First slide
    [
        'img'       => 'image3.jpg',
        'text'  => 'Slide Three Text',
        'svg'       => 'cat3'
    ],
    [
        'img'       => 'image4.jpg',
        'text'  => 'Slide Four Text',
        'svg'       => 'cat4'
    ],
    [
        'img'       => 'image5.jpg',
        'text'  => 'Slide Five Text',
        'svg'       => 'cat5'
    ],
];

I built the slider in a way that the css/js is dependent on the third slide being active. The slide wrapper then resets for mobile and the first slide in the array is the first slide in the rendered html. I do not want to change/update the css structure as it is controlling about 5 media query breakpoints and very specific to a unique design. I am hoping that there is a simple way that I can just reorder the array in the html, so that on Desktop the third slide is the first and on mobile the first slide is the first. Is there an easy way to do this that I'm just not think of? My html looks like this:

        <div class="home-slider-wrap">
                <?php foreach ($slides as $index=>$slide): ?>
                    <div class="home-slider-slide slide<?php echo $index; ?> <?php if($index == 2){ echo 'active'; } ?>" data-slide="<?php echo $index; ?>">
                        <img src="/images/homepage/<?php echo $slide[img]; ?>" alt="<?php echo $slide['text']; ?>">
                    </div>
                <?php endforeach; ?>
            </div>

I want to keep the <?php if($index == 2){ echo 'active'; } ?> so that I don't have to update the js or css, but want to reorder the array. Hope that makes sense.

2
  • so what problem you are facing? Commented Feb 1, 2017 at 16:18
  • If you're reloading the page, have the PHP reorder the elements before resending the page to the client. If it has to be done clientside, you'll have to add JS to sort() the array, then rerender everything that changed. This can probably also be done with extra css in the media queries. Commented Feb 1, 2017 at 16:24

1 Answer 1

1

Using jQuery you could do something like

$(function(){
    if($(window).width() > deskTopBreakpointValue ){         
      var $first = $('.home-slider-slide.active');
      $('.home-slide-wrap').prepend($first);
    }

    // now initialize your slider code

})
Sign up to request clarification or add additional context in comments.

2 Comments

You would probably want to remove it before prepending it to avoid duplication
@MrBizle that can't happen...when you move an element it has to be removed from it's current location in order to be put in a new one and is all handled by prepend()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.