Cursor in wrong position in IE11 after CSS transform w/ transition

一个人想着一个人 提交于 2019-12-12 11:59:22

问题


jsfiddle here

This is a bug specific to IE and I'm looking for a work around.

When I apply a CSS transform: translate to a text input, that has the focus, with transition set to something valid, the cursor stays in the old location while the element moves.

Once you start typing it moves to the correct location, but before that the cursor stubbornly blinks at the old location.

This code illustrates the problem... again, it's an IE specific bug.

var toggleTop = function(){
    $('.input-container').toggleClass('top');
    $('#the-input').focus();
}

$('#the-button').click(toggleTop);
.input-container {
    top: 100px;
    left: 100px;
    position: fixed;
    transition: all 1s;
}

.input-container.top {
    transform: translateY(-100px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='input-container'>
    <input type='text' id='the-input'></input>
</div>
<button id='the-button'>Click Me!</button>

回答1:


Is there any reason why you can't wait for the transition to end before focusing on the element? Considering you're using CSS transitions you should have access to transitionend event.
This fixes the issue:

var toggleTop = function () {
    var inputContainer = $('.input-container'),
        input = inputContainer.find('#the-input');

    inputContainer.toggleClass('top');

    inputContainer.one('transitionend', function () {
        input.focus();
    });
};


$('#the-button').click(toggleTop);

Updated JSFiddle




回答2:


One year passed, and I've encountered this issue as well. Here is angular.js directive that I used to fixed it, based on the accepted answer's code and explanation.

angular.module('my.directive')
  .directive('custom-auto-focus', function ($timeout) {
    return {
      link: function (scope, elm) {
        $timeout(function() {
          elm[0].focus();
        }, 350);
      }
    };
  }); 


来源:https://stackoverflow.com/questions/26617849/cursor-in-wrong-position-in-ie11-after-css-transform-w-transition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!