简体   繁体   English

使用jquery延迟css转换

[英]Delaying css transition with jquery

I am trying to delay a css transition of an element based on delay function + additional 0.2s applied on it so it slides 0.2s later than initial delay of main wrapper. 我试图基于延迟函数延迟元素的css转换+在其上施加额外的0.2s,因此它比主包装器的初始延迟滑动0.2秒。 I add a class to it which gives it a transition to slide from right to left. 我一类添加到它,给人一种transition从右向左滑动。

If i disable this additional delay (transition-delay), then the element slides fine when initial delay fires up. 如果我禁用这个额外的延迟(转换延迟),那么当初始延迟启动时元素会很好地滑动。 If i leave it on and add 0.2s additional delay on this inner div then the transition won't work. 如果我打开它并在这个内部div上增加0.2秒的延迟,那么转换将不起作用。

Current progress on fiddle and jquery: 小提琴和jquery的当前进展:

(function ($) {
    $.fn.testPlugin = function (options) {

        var settings = $.extend({
            showDiv: false,
            delayIt: null,
            trans: null,
            timing: null,
            speed: null,

        }, options);

        return this.each(function () {
            var self = this;

            // Show main
            if (settings.showDiv == true) {
                setTimeout(function () {
                    $(self).addClass("showIt");
                }, settings.delayIt);
            };

            // Show inner
            $(".inner").addClass(settings.trans + " " + settings.timing + " " + settings.speed).css({
                "transition-delay" : settings.delayIt / 1000 + 0.2 + "s", // if i delete this delay then inner div slides fine
            });

        });
    }
}(jQuery));

$(document).ready(function () {
    $("#testDiv").testPlugin({
        showDiv: true,
        delayIt: 500,
        trans: "left",
        timing: "ease",
        speed: "fast",
    });
});

I placed your actions with the ".inner" to the delay timeout.Try with the next code 我将您的操作与".inner"放到延迟".inner"下一个代码

 $.fn.testPlugin = function (options) {

    var settings = $.extend({
        showDiv: false,
        delayIt: null,
        trans: null,
        timing: null,
        speed: null,

    }, options);

    return this.each(function () {
        var self = this;

        // Show main
        if (settings.showDiv == true) {
            setTimeout(function () {
                $(self).addClass("showIt");
                // Show inner
                $(".inner").addClass(settings.trans + " " + settings.timing + " " + settings.speed);

            }, settings.delayIt);
        };


    });
}

Div with class inner has already CSS transform property. 具有类inner Div已经具有CSS transform属性。 So, you need to change CSS selector for class 'left' to select through id so that it has higher specificity 因此,您需要更改类'left'的CSS选择器以通过id进行选择,以便它具有更高的特异性

Change 更改

.left {
  transform: translateX(20%);
}

to

#testDiv.showIt .left {
  transform: translateX(20%);
}

JSFiddle: https://jsfiddle.net/7qdyeq0x/5/ JSFiddle: https ://jsfiddle.net/7qdyeq0x/5/

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

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