1

I have a page with jQuery content loaded, one of the div will be a slider

$('#slides').slides({
            preload: true,
            preloadImage: 'img/loading.gif',
            play: 6000,
            pause: 2500,
            hoverPause: true,
            slideSpeed: 1000,
            animationStart: function(current){
                $('.caption').animate({
                    bottom:-35
                },100);
            },
            animationComplete: function(current){
                $('.caption').animate({
                    bottom:0
                },200);
            },
            slidesLoaded: function() {
                $('.caption').animate({
                    bottom:0
                },200);
            }
        });
    });

now, the html is structured like this:

<div id"content">
   <div id="slider"></div>
</div>

I've got my page setup using jquery, when a user clicks on a link, it will load that page's content to the #content

var homepage = $("#content").html();
$("#content").load("myContent.html");

If the user goes back to home, simply :

$("#content").html(homepage);

but then the $('#slides').slides(); doesn't work anymore.

how do I fix this?

SlidesJS

2 Answers 2

3

You could use the complete from .load() to check if your content has a #slider in it. If it does, initialize your slides plugin again.

var homepage = $('#content'),
options = {
    generateNextPrev: true,
    play: 1000
};
$('#slides').slides(options);

// Button click or something
$('#content').load('myContent.html', function(response, status, xhr) {
   if ($(response).find('#slider').length > 0) {
       $('#slides').slides(options);
   }
});

EDIT:

I was making a jsfiddle to demonstrate it, but jsfiddle is having server problems, so I made a codepen demo. http://codepen.io/Vestride/pen/LicKd. Calling the plugin again worked for me (although I was simulating ajax requests by just replacing the content with .html()).

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

2 Comments

i'm sure that the #slides will be there since it's stored in the "var homepage", what i'm asking is that how to make the slides(); work again after i've replaced the html and then put the same html(the one with #slides) back again
@Tom91136 also make sure you're calling $('#slides').slides(options). If that doesn't work either, save your homepage variable before calling slides() I've edited the example above.
1

Try this,

Put slider in a function,

function SliderCode()
{

$('#slides').slides({
            preload: true,
            preloadImage: 'img/loading.gif',
            play: 6000,
            pause: 2500,
            hoverPause: true,
            slideSpeed: 1000,
            animationStart: function(current){
                $('.caption').animate({
                    bottom:-35
                },100);
            },
            animationComplete: function(current){
                $('.caption').animate({
                    bottom:0
                },200);
            },
            slidesLoaded: function() {
                $('.caption').animate({
                    bottom:0
                },200);
            }
        });
    });
}

After setting hompage callslider function again,

$("#content").html(homepage);
SliderCode();

I didnot tested the code. hope it will work.

7 Comments

is there a better way to do this? like destroy the .slides(); first? how do i do that?
Or you can put slider in iframe. Then iframe in hompage html
i'm using XHTML 1.0 strict, i can't use iframes
Okay, Then I would suggest to show or hide the container element containing slider rather than setting the entire HTML all the times.
but it looses it's html when i do this: $("#content").load("myContent.html");
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.