Input effect on keyboard tab -> focus, but NOT on click

前端 未结 3 1422
萌比男神i
萌比男神i 2020-12-30 08:37

When a user \'tabs over\' to an input, I want the focus effect to be normally displayed, but on click, I don\'t want it to be visible.

User hits tab

相关标签:
3条回答
  • 2020-12-30 09:00

    I think a lot of front-end developers struggle to find a balance between aesthetics and the best-practices for accessibility. This seems like a great compromise.

    Here's how I do it. The idea is to toggle outlining on when the user uses the tab key and turn it back off when they click.

    JS

    document.addEventListener('keydown', function(e) {
      if (e.keyCode === 9) {
        $('body').addClass('show-focus-outlines');
      }
    });
    
    document.addEventListener('click', function(e) {
      $('body').removeClass('show-focus-outlines');
    });
    

    Styles

    body:not(.show-focus-outlines) button:focus, 
    body:not(.show-focus-outlines) [tabindex]:focus {
      outline: none;
    }
    
    0 讨论(0)
  • 2020-12-30 09:08

    I'm currently doing something similar for my company. Unfortunately you must use JavaScript since CSS doesn't support this use case.

    Here's what I've done.

    var btns = document.querySelectorAll('button');
    
    var onMouseDown = function (evt) {
      evt.target.dataset.pressed = 'true';
    };
    
    var onMouseUp = function (evt) {
      evt.target.dataset.pressed = 'false';
    };
    
    var onFocus = function (evt) {
      var element = evt.target;
      if (element.dataset.pressed !== 'true') {
        element.classList.add('focus');
      }
    };
    
    var onBlur = function (evt) {
      evt.target.classList.remove('focus');
    };
    
    for(var i = 0, l = btns.length; i < l; i++) {
      btns[i].addEventListener('mousedown', onMouseDown);
      btns[i].addEventListener('mouseup', onMouseUp); 
      btns[i].addEventListener('focus', onFocus);
      btns[i].addEventListener('blur', onBlur);
    }
    * { box-sizing: border-box; }
    
    body { background-color: white; }
    
    button {
      -webkit-appearance: none;
      -moz-appearance: none;
      appearance: none;
      min-width: 100px;
      margin: 0 1px;
      padding: 12px 10px;
      font-size: 15px;
      color: white;
      background-color: #646e7c;
      border: none;
      border-radius: 5px;
      box-shadow: 0 2px 2px 0 rgba(0,0,0,.2);
      cursor: pointer;
      -webkit-user-select: none;
      -moz-user-select: none;
      user-select: none;
    }
    
    button:focus { outline: none; }
    
    button:active {
      -webkit-transform: translateY(1px);
      -moz-transform: translateY(1px);
      transform: translateY(1px);
      box-shadow: 0 0 2px 0 rgba(0, 0, 0, 0.2);
    }
    
    button.focus {
      font-weight: bold;
    }
    
    button.primary { background-color: #2093d0; }
    
    button.success { background-color: #71a842; }
    
    button.danger { background-color: #ef4448; }
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
    </head>
    <body>
      <button>Default</button>
      <button class="primary">Primary</button>
      <button class="success">Success</button>
      <button class="danger">Danger</button>
    </body>
    </html>

    Basically instead of relying on browser's native focus I add/remove a focus class on my button depending on the situation.

    0 讨论(0)
  • 2020-12-30 09:18

    If you use the what-input.js plugin you can apply styles specifically for keyboard users. You can use the following code to highlight a button that has been tabbed to. I've found what-input to be a reliable plugin (comes bundled with Zurb Foundation) and is currently regularly maintained.

    // scss
    body[data-whatinput="keyboard"] {
      button {
        &:focus {
          // other highlight code here
          box-shadow: 0 0 5px rgba(81, 203, 238, 1);
        }
      }
    }
    

    or

    /* vanilla css */
    body[data-whatinput="keyboard"] button:focus {
      box-shadow:  0 0 5px rgba(81, 203, 238, 1);
    }
    
    0 讨论(0)
提交回复
热议问题