简体   繁体   English

带动画的jQuery的addClass removeClass

[英]addClass removeClass w/jQuery for Animations

SEE MY ANSWER BELOW 在下面查看我的答案

I'm implementing my own sliding page transition with CSS using jQuery and having the following issue: 我正在使用jQuery使用CSS实现我自己的滑动页面过渡,并且遇到以下问题:

I'm using the addClass method and it works the first time a link is fired to load a page dynamically into the div. 我正在使用addClass方法,它在第一次触发链接以将页面动态加载到div中时起作用。 When another link is fired, the page loads without the transition. 当另一个链接被触发时,页面加载而没有过渡。 I figured it was that I need to use the removeClass method but this prevents the transitions from not fireing at all. 我认为这是我需要使用removeClass方法,但这可以防止根本不触发转换。 Iv'e tried various ways of using the two classes but I cant get it to work every time a link is fire. 我尝试了使用这两个类的各种方法,但是每次链接出现问题时,我都无法使它正常工作。

Any idea to what I have wrong/missing? 对我的错/遗漏有任何想法吗?

EDIT For those who doesnt under stand the CSS subClass: 编辑对于那些不懂CSS子类的人:

More than one class may be added at a time, separated by a space, to the set of matched elements, like so: 一次可以将多个类(以空格分隔)添加到一组匹配的元素中,如下所示:

$("p").addClass("myClass yourClass");

Taken from the jQuery Docs 取自jQuery Docs

thnx. thnx。

/* --------------- Handle Page Body Loading ----------------- */

function loadBody(target, gotoURL)
{
    $('#' + target)
    .addClass('slide out')
    .load(gotoURL, function (response)
    {
        iniScroll('scrollBody', 'bodyScrollContainer');
    })
    .addClass('slide in')
    removeSlideClass();//WITHOUT THIS, IT FIRES ONLY THE FIRST USE!!
};

function removeSlideClass()
{
    $('#bodyContent').removeClass('slide in');
    $('#bodyContent').removeClass('slide out');
};

IF i WRITE THE CODE AS SAID BELOW, IT WORKS EXACTLY THE SAME (jQuery 101); 如果我按照下面的说明写代码,则它的工作原理完全相同(jQuery 101);

function loadBody(target, gotoURL)
{
    $('#' + target).addClass('slide out');
    $('#' + target).load(gotoURL, function (response)
    {
        iniScroll('scrollBody', 'bodyScrollContainer');
    });
    $('#' + target).addClass('slide in');
    $('#' + target).removeSlideClass();
};

function removeSlideClass()
{
    $('#bodyContent').removeClass('slide in');
    $('#bodyContent').removeClass('slide out');
};

the CSS: CSS:

.out, .in {
    -webkit-animation-timing-function: ease-out;
    -webkit-animation-duration: 650ms;
}
.slide.out {
    -webkit-transform: translateX(-100%);
    -webkit-animation-name: slideouttoleft;
}

.slide.in {
    -webkit-transform: translateX(0);
    -webkit-animation-name: slideinfromright;
}

I figured it out: 我想到了:

Binding the .addClass('slide out') method with webkitAnimationEnd event, followed by a return statement, fires the second transition when the first has completed. .addClass('slide out')方法与webkitAnimationEnd事件webkitAnimationEnd ,后跟return语句,当第一个转换完成时,将触发第二个转换。 When this event fires, it removes all classes from the original page. 触发此事件时,它将从原始页面中删除所有类。 Remembering to unbind the webkitAnimationEnd event prevents adding on extra handlers to the new page so the process can start again when we need it. 记住取消绑定webkitAnimationEnd事件可以防止在新页面上添加额外的处理程序,以便在需要时可以重新开始该过程。 Thus jQuery's removeClass() method is not needed. 因此,不需要jQuery的removeClass()方法。

The new page transition method. 新的页面过渡方法。

/* --------------- Handle Page Body Loading ----------------- */

function loadBody(target, gotoURL)
{
    $('#' + target)
    .addClass('slide out')
    .bind('webkitAnimationEnd', function(){
        $(this).load(gotoURL, function (e)
        {
            iniScroll('scrollBody', 'bodyScrollContainer');
            $('#' + target)
            .toggleClass('slide in')
            .unbind('webkitAnimationEnd');
        });
    });
};

CSS class should not include any spaces... CSS类不应包含任何空格...

ie: 即:

slide out, should be slide-out, or slide_out or slideOut 滑出,应该滑出,还是slide_out或slideOut

Try removing the spaces and fixing the classes in the css to Match. 尝试删除空格并固定css中的类以使其匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM