最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Long button press on iOS (Javascript) - Stack Overflow

programmeradmin0浏览0评论

Is it possible to make a button be able to have a long button press on iOS? I have searched all over and not found any solutions for this problem. At first if you long press a button it will try to select it (for copying). Using some CSS, I disabled this, but it still doesn't register a long button press. I think it is because the possibility that you might be scrolling the page that it does not long press in the first place. The purpose of this is that I want the button to disappear on a long press.

Here is a JSFiddle that works on PC, but doesn't on my iPhone (there is a div mented out because it doesn't matter if the long press is on a div or button, I can create an artificial button using a div): /

Thanks in advance.

var MenuToggle = document.getElementsByClassName("hide")[0];
MenuToggle.style["-webkit-user-select"] = "none";
MenuToggle.addEventListener("mousedown", Vanish);
MenuToggle.addEventListener("mouseup", VanishClear);
var timer;
function Vanish() {
    longpress = false;
    timer = setTimeout(function() {
        MenuToggle.style.display = "none";
    }, 1000);
}
function VanishClear() {
    clearTimeout(timer);
}

Is it possible to make a button be able to have a long button press on iOS? I have searched all over and not found any solutions for this problem. At first if you long press a button it will try to select it (for copying). Using some CSS, I disabled this, but it still doesn't register a long button press. I think it is because the possibility that you might be scrolling the page that it does not long press in the first place. The purpose of this is that I want the button to disappear on a long press.

Here is a JSFiddle that works on PC, but doesn't on my iPhone (there is a div mented out because it doesn't matter if the long press is on a div or button, I can create an artificial button using a div): http://jsfiddle/ynkLmz5n/3/

Thanks in advance.

var MenuToggle = document.getElementsByClassName("hide")[0];
MenuToggle.style["-webkit-user-select"] = "none";
MenuToggle.addEventListener("mousedown", Vanish);
MenuToggle.addEventListener("mouseup", VanishClear);
var timer;
function Vanish() {
    longpress = false;
    timer = setTimeout(function() {
        MenuToggle.style.display = "none";
    }, 1000);
}
function VanishClear() {
    clearTimeout(timer);
}
Share Improve this question asked Sep 10, 2014 at 18:11 anonanon 1
  • This question stackoverflow./questions/2625210/long-press-in-javascript has what you asked but seems noone cared to test that on iOS it seems. Maybe you can try convert github./pisi/Longclick into vanilla JS like you are using, looks like that one works. – RaphaelDDL Commented Sep 10, 2014 at 18:17
Add a ment  | 

2 Answers 2

Reset to default 4

Have you tried using touch events?

var timer

MenuToggle.addEventListener('touchstart', function() {
  timer = setTimeout(function() {
    MenuToggle.style.display = 'none'
  }, 1000)
}, false)

MenuToggle.addEventListener('touchend', function() {
  clearTimeout(timer)
}, false)

You'll want to use touch events (touchstart and touchend) in place of mouse events.

Here's an updated fiddle: http://jsfiddle/ynkLmz5n/4/

MenuToggle.addEventListener("touchstart", Vanish);
MenuToggle.addEventListener("touchend", VanishClear);

Also, if you're looking to add touch interaction, I'd remend hammer.js. It's a great touch library that includes support for things like Press)

Project page: http://hammerjs.github.io/

Press event documentation: http://hammerjs.github.io/recognizer-press/

发布评论

评论列表(0)

  1. 暂无评论