简体   繁体   中英

Javascript: Long Press for File Dialog

Is it possible to use long press for file dialog? For example Long Press in JavaScript? has an answer to trigger an event in long press. However, this can't be used for triggering file input click in most browsers since it is not considered as a user activation.

var pressTimer;

$("a").mouseup(function(){
  clearTimeout(pressTimer);
  // Clear timeout
  return false;
}).mousedown(function(){
  // Set timeout
  pressTimer = window.setTimeout(function() 
  {    fileChooser.click() // assume fileChoose is a file input element
       // This is suppressed by most browsers.
  },1000);
  return false; 
});

Sure it's possible. You'd probably have to develop your own detection for it using the mousedown and mouseup events. Calculate the time between the mousedown and mouseup and decide if the delay was long enough to cause some other action.

https://jsfiddle.net/psc4yk76/2/

(function() {
  const longtime = 500;
  const target = document.getElementById('target');
  const input = document.getElementById('input');
  const button = document.getElementById('button');

  var timedown = 0;

  button.onmousedown = () => {
    timedown = new Date().getTime();
  };

  button.onmouseup = () => {
    let timeup = new Date().getTime();
    let insert = document.createElement('div');

    if( (timeup - timedown) < longtime ) {
      insert.appendChild(document.createTextNode('Short!'));
    } else {
      insert.appendChild(document.createTextNode('Long!'));
      input.click();
    }

    target.appendChild(insert);
  }
})()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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