Reputation: 3
I have div#container
, which contains two divs div.current-content
and div.next-content
.
Both are filled with some content.
How I can switch class between next and current content without losing data?
This is my original reason, but with this I lost everything in current-content.
changeContent = function (direction, toLoad) {
if (direction == 'next') {
container.append('<div class="next-content"></div>');
toScroll = -contentWidth;
} else {
container.prepend('<div class="next-content"></div>').css('margin-left', -contentWidth);
toScroll = 0;
}
container.css('width', contentWidth * 2);
$('.current-content, .next-content').css('width', contentWidth);
$('.next-content').load(toLoad, '', function () {
// PSEUDO 3D
if (direction == 'next') {
$('.next-content .content')
.css('margin-left', contentWidth * 2)
.animate({
'margin-left': (contentWidth / 2) - 485
}, {
duration: speed,
easing: 'easeInOutQuad',
complete: function () {
$('.content').css('margin-left', 'auto');
}
});
$('.current-content .content')
.css('margin-left', (contentWidth / 2) - 485)
.animate({
'margin-left': -contentWidth
}, {
duration: speed,
easing: 'easeInOutQuad'
});
} else {
$('.next-content .content')
.css('margin-left', -contentWidth * 2)
.animate({
'margin-left': (contentWidth / 2) - 485
}, {
duration: speed,
easing: 'easeInOutQuad',
complete: function () {
$('.content').css('margin-left', 'auto');
}
});
$('.current-content .content')
.css('margin-left', (contentWidth / 2) - 485)
.animate({
'margin-left': contentWidth * 2
}, {
duration: speed,
easing: 'easeInOutQuad'
});
}
container.animate({
'margin-left': toScroll
}, {
duration: speed,
easing: 'easeInOutQuad',
complete: function () {
container.css({
'margin-left': 0,
'width': 'auto'
});
$('.current-content').remove();
$('.next-content').addClass('current-content').removeClass('next-content');
$('.current-content').css('width', '100%');
}
});
}
Upvotes: 0
Views: 69
Reputation: 8207
At the end of your changeContent
function, you're calling:
$('.current-content').remove();
This removes element(s) with class .current-content
. Instead of this, you should hide it, either using css or javascript/jquery.
Using css:
.prev-content
display: none
or visibility:hidden
Using jQuery:
$("{selector}").hide()
And if you want to show it back, you can remove .prev-content
class and add .current-content
back, or just select it using jQuery and un-hide it, using $("{selector}").show()
.
{selector}
stands for a selector that selects only the element you want to hide/show
Upvotes: 2