首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >切换类而不丢失内容

问切换类而不丢失内容
EN

Stack Overflow用户
提问于 2014-02-22 16:18:02
回答 1查看 63关注 0票数 0

我有div#container,它包含两个div div.current-content和div.next-content。

两者都充满了一些内容。

如何在下一个内容和当前内容之间切换类而不丢失数据?

这是我最初的理由,但由于这个原因,我失去了当前内容中的一切。

代码语言:javascript
复制
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%');
            }
        });
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-22 16:34:28

在changeContent函数的末尾,您正在调用:

代码语言:javascript
复制
$('.current-content').remove();

这将删除类.current-content的元素。相反,您应该使用css或javascript/jquery来隐藏它。

使用css:

  • 最好分配一个新类,例如.prev-content
  • 在css文件中添加display: none或visibility:hidden

使用jQuery:

  • 我仍然建议在css上分配一个新的类。
  • 选择元素并使用$("{selector}").hide()隐藏它

如果您想要返回,可以删除.prev-content类并添加.current-content返回,或者使用jQuery选择它,然后使用$("{selector}").show()取消隐藏它。

{selector}表示选择器,该选择器只选择要隐藏/显示的元素

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21956904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档