0

I need to remove slides from a slider (made with iscroll) dynamically, but also need the slider to be able to 'refresh' or all the original slides to be put back in.

How is best to do this?

html looks like:

<div id="wrapper">
    <div id="slider>
        <div id="job-container1"><!-- slide 1 content here--></div>
        <div id="job-container2"><!-- slide 2 content here--></div>
        <div id="job-container3"><!-- slide 3 content here--></div>
    </div>
</div>

js code to determine if I have less than 3 slides worth of info, therefore need to remove left over slides:

if(savedJobs.length === 1){
    $('#job-container2, #job-container3').remove()
    $('#slider').css('width','100%')
}else if(savedJobs.length === 2){
    $('#job-container3').remove()
    $('#slider').css('width','200%')
}else if(savedJobs.length >2){
    //todo: check if its the last in the array also
}

I tried simply hiding the divs but the slider will still scroll to a blank slide even when myscroll.refresh() is called

4

1 回答 1

1

使用.detach

var containers = $('#job-container2, #job-container3');

if(savedJobs.length === 1){
    containers.detach()
    $('#slider').css('width','100%')
}else if(savedJobs.length === 2){
    $('#job-container3').detach()
    $('#slider').css('width','200%')
}else if(savedJobs.length >2){
    //todo: check if its the last in the array also
}

不确定这是否是您稍后缓存那些重新插入它们的最佳方式,但您需要保存对它们的引用,因为 jQuery 使用document来遍历并且您将其从document.

于 2013-08-06T15:53:37.043 回答