Detecting Browser Autofill

前端 未结 29 1884
天涯浪人
天涯浪人 2020-11-22 06:15

How do you tell if a browser has auto filled a text-box? Especially with username & password boxes that autofill around page load.

My first question is when does

29条回答
  •  眼角桃花
    2020-11-22 06:54

    I had a hard time detecting auto-fill in Firefox. This is the only solution that worked for me:

    Demo

    HTML:

    CSS:

    /* Detect autofill for Chrome */
    .inputFields input:-webkit-autofill {
        animation-name: onAutoFillStart;
        transition: background-color 50000s ease-in-out 0s;
    }
    .inputFields input:not(:-webkit-autofill) {
        animation-name: onAutoFillCancel;
    }
    
    @keyframes onAutoFillStart {
    }
    
    @keyframes onAutoFillCancel {
    }
    .inputFields {
      max-width: 414px;
    }
    
    .field_set .phold{
      display: inline-block;
      position: absolute;
      font-size: 14px;
      color: #848484;
      -webkit-transform: translate3d(0,8px,0);
      -ms-transform: translate3d(0,8px,0);
      transform: translate3d(0,8px,0);
      -webkit-transition: all 200ms ease-out;
      transition: all 200ms ease-out;
      background-color: transparent;
      -webkit-backface-visibility: hidden;
      backface-visibility: hidden;
      margin-left: 8px;
      z-index: 1;
      left: 0;
      pointer-events: none;
    }
    
    .field_set .phold_active {
       font-size: 12px;
       -webkit-transform: translate3d(0,-8px,0);
      -ms-transform: translate3d(0,-8px,0);
      transform: translate3d(0,-8px,0);
      background-color: #FFF;
      padding-left: 3px;
      padding-right: 3px;
    }
    
    .field_set input[type='text'], .field_set select, .field_set input[type='tel'], .field_set input[type='password'] {
        height: 36px;
    }
    
    .field_set input[type='text'], .field_set input[type='tel'], .field_set input[type='password'], .field_set select, .field_set textarea {
        box-sizing: border-box;
        width: 100%;
        padding: 5px;
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        border: 1px solid #ababab;
        border-radius: 0;
    }
    
    .field_set {
        margin-bottom: 10px;
        position: relative;
    }
    
    .inputFields .f_o {
        width: 100%;
        line-height: 1.42857143;
        float: none;
    }
    

    JavaScript:

        // detect auto-fill when page is loading
      $(window).on('load', function() {
        // for sign in forms when the user name and password are filled by browser
        getAutofill('.inputFields');
      });
    
      function getAutofill(parentClass) {
        if ($(parentClass + ' .form_field').length > 0) {    
          var formInput = $(parentClass + ' .form_field');
          formInput.each(function(){   
            // for Chrome:  $(this).css('animation-name') == 'onAutoFillStart'
            // for Firefox: $(this).val() != ''
            if ($(this).css('animation-name') == 'onAutoFillStart' || $(this).val() != '') {
              $(this).siblings('.phold').addClass('phold_active');
            } else {
              $(this).siblings('.phold').removeClass('phold_active');
            }
          });
        }
      } 
    
      $(document).ready(function(){
    
        $(document).on('click','.phold',function(){
          $(this).siblings('input, textarea').focus();
        });
        $(document).on('focus','.form_field', function(){
          $(this).siblings('.phold').addClass('phold_active');
        });
    
        // blur for Chrome and change for Firefox
        $(document).on('blur change','.form_field', function(){
          var $this = $(this);
          if ($this.val().length == 0) {        
            $(this).siblings('.phold').removeClass('phold_active');
          } else {
            $(this).siblings('.phold').addClass('phold_active');
          }
        });
    
        // case when form is reloaded due to errors
        if ($('.form_field').length > 0) {
          var formInput = $('.form_field');
          formInput.each(function(){
            if ($(this).val() != '') {
              $(this).siblings('.phold').addClass('phold_active');
            } else {
              $(this).siblings('.phold').removeClass('phold_active');
            }
          });
        }
    
      }); 
    

    For Chrome I use: if ($(this).css('animation-name') == 'onAutoFillStart')

    For Firefox: if ($(this).val() != '')

提交回复
热议问题