当前位置:Gxlcms > html代码 > 移动端HTML5模拟长按删除事件(附代码)

移动端HTML5模拟长按删除事件(附代码)

时间:2021-07-01 10:21:17 帮助过:16人阅读

本篇文章给大家带来的内容是关于移动端HTML5模拟长按删除事件(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

为啥写这篇文章

最近接了个需求,要求长按某个标签显示删除一个悬浮的删除按钮。这个需求其实在app上很常见,但是在移动端h5中,我们没有长按的事件,所以就需要自己模拟这个事件了。

大概效果如下:

2345截图20180930151947.png

思路

  • 放弃click事件,通过判断按的时长来决定是单击还是长按

  • 使用touchstart和touchend事件

  • 在touchstart中开启一个定时器,比如在700ms后显示一个长按菜单

  • 在touchend中清除这个定时器,这样如果按下的时间超过700ms,那么长按菜单已经显示出来了,清除定时器不会有任何影响;如果按下的时间小于700ms,那么touchstart中的长按菜单还没来得及显示出来,就被清除了。

由此我们可以实现模拟的长按事件了。

上代码

请把重点放在JS上,这里贴出来完整的代码是为了方便大家看个仔细,代码可以拷贝直接看效果
css中大部分只是做了样式的美化,还有一开始让删除按钮隐藏起来

HTML:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <link rel="stylesheet" type="text/css" href="./longpress.css" />
  9. </head>
  10. <body>
  11. <div>
  12. <div id="label">长按我</div>
  13. <div>删除</div>
  14. </div>
  15. <script src="./longpress.js"></script>
  16. </body>
  17. </html>

JS

  1. let timer = null
  2. let startTime = ''
  3. let endTime = ''
  4. const label = document.querySelector('.label')
  5. const deleteBtn = document.querySelector('.delete_btn')
  6. label.addEventListener('touchstart', function () {
  7. startTime = +new Date()
  8. timer = setTimeout(function () {
  9. deleteBtn.style.display = 'block'
  10. }, 700)
  11. })
  12. label.addEventListener('touchend', function () {
  13. endTime = +new Date()
  14. clearTimeout(timer)
  15. if (endTime - startTime < 700) {
  16. // 处理点击事件
  17. label.classList.add('selected')
  18. }
  19. })

CSS

  1. .container {
  2. position: relative;
  3. display: inline-block;
  4. margin-top: 50px;
  5. }
  6. .label {
  7. display: inline-block;
  8. box-sizing: border-box;
  9. width: 105px;
  10. height: 32px;
  11. line-height: 32px;
  12. background-color: #F2F2F2;
  13. color: #5F5F5F;
  14. text-align: center;
  15. border-radius: 3px;
  16. font-size: 14px;
  17. }
  18. .label.selected {
  19. background-color: #4180cc;
  20. color: white;
  21. }
  22. .delete_btn {
  23. display: none;
  24. position: absolute;
  25. top: -8px;
  26. left: 50%;
  27. transform: translateX(-50%) translateY(-100%);
  28. color: white;
  29. padding: 10px 16px;
  30. background-color: rgba(0, 0, 0, .7);
  31. border-radius: 6px;
  32. line-height: 1;
  33. white-space: nowrap;
  34. font-size: 12px;
  35. }
  36. .delete_btn::after {
  37. content: '';
  38. width: 0;
  39. height: 0;
  40. border-width: 5px;
  41. border-style: solid;
  42. border-color: rgba(0, 0, 0, .7) transparent transparent transparent;
  43. position: absolute;
  44. bottom: -9px;
  45. left: 50%;
  46. transform: translateX(-50%);
  47. }

ps: touchstart和touchend只有在移动端设备上才有用,如果要看代码示例的话请:

  • 用chrome

  • F12打开调时窗

  • 切换到模拟移动设备

即点击如下图:

2156555810-5baf4d54e9afa_articlex.png

以上就是移动端HTML5模拟长按删除事件(附代码)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行