简体   繁体   English

在包含焦点输入框的元素上使用CSS动画

[英]Using CSS animations on element containing a focused input box

I am trying to use CSS animations on an element with a focused input box. 我正在尝试在具有焦点的输入框的元素上使用CSS动画。 My use case is a popup box with two or more pages. 我的用例是一个包含两个或更多页面的弹出框。 The "pages" are a single container that slides left/right using CSS transitions. “页面”是使用CSS过渡向左/向右滑动的单个容器。

Everything was great until I wanted to have an input field on page 2 be focused upon navigating to that page. 在我希望第2页上的输入字段专注于导航到该页面之前,一切都很棒。 The entire CSS animation gets screwed up. 整个CSS动画都搞砸了。 I could try a timeout for the jQuery focus() function I'm using to focus the input box, but I don't like mixing timeouts with CSS transition times (and am guessing that isn't the proper thing to do). 我可以为用于聚焦输入框的jQuery focus()函数尝试超时,但是我不喜欢将超时与CSS转换时间混合在一起(并且我猜这不是正确的做法)。

I've seen this behavior in the latest Chrome/Firefox/IE, and have replicated it in this fiddle: 我已经在最新的Chrome / Firefox / IE中看到了这种行为,并在以下小提琴中复制了它:

http://jsfiddle.net/bmh5g/40/ http://jsfiddle.net/bmh5g/40/

If you comment the noted focus() line out of it, you can see the intended animation 如果您注释掉其中标注的focus()行,则可以看到预期的动画

The relevant code here is just: 这里的相关代码是:

Javascript: 使用Javascript:

$('#next').on({
    click: function(){
        //comment the following line out to see proper animation:
        $('#the-input').focus();
        $('.content').addClass('page2');
    }
});

CSS: CSS:

.page2 {
    left: -100%;
}

I have also tried (and, on my actual project, am now) using a translateX transformation for the CSS animation. 我还尝试过(现在在我的实际项目中)对CSS动画使用translateX转换。 Exact same issue. 完全一样的问题。

I don't think I'll find a fix to make the input actually properly animate, but I can't think of any potential workarounds for focusing after the animation. 我认为我不会找到一个使输入实际上正确地动画的修补程序,但是我想不出动画后聚焦的任何潜在解决方法。 I appreciate any help with this one! 感谢您对此提供的任何帮助! Thanks in advance. 提前致谢。

Take a look here: 在这里看看:

DEMO 演示

You will have to use a CSS transition end event binder here, which will do the job. 您将必须在此处使用CSS过渡结束事件绑定程序,以完成此工作。

$('#next').on({
    click: function(){
        $('.content').addClass('page2');
    }
});

$(".content").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(e){
    if($(this).hasClass("page2"))
        $('#the-input').focus();
});

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

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