<h3>长按</h3>
<h5>通过html绑定</h5>
<button class="button" id="btn1" onlongclick="fn(event)" onclick="fn1()">按钮</button>
<h5>通过addEventListener绑定</h5>
<button class="button" id="btn2" >按钮2</button>
<h5>通过onlongclick绑定事件</h5>
<button class="button" id="btn3" >按钮3</button>
.button:hover:active{
  opacity:.99;
  transition:opacity 1s;
}
function fn1(){
  alert('onclick')
}

function fn(ev){
  console.log(ev)
  alert('onlongclick')
}

//通过addEventListener绑定事件
document.querySelector('#btn2').addEventListener('longclick',fn);

//通过onlongclick绑定事件
document.querySelector('#btn3').onlongclick = fn;





document.querySelectorAll('.button').forEach(function(el){
  el.addEventListener('transitionend',function(){
    // 创建并分发事件
    var event = new CustomEvent("longclick");
    el.dispatchEvent(event);
    eval(''+this.getAttribute('onlongclick')+'');
    if(el['onlongclick']){
       el['onlongclick'](event);
    }
  })
})

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.