*新闻详情页*/>
为啥写这篇文章内容
近期接了个要求,规定长按某个标识显示信息删掉1个飘浮的删掉按钮。这个要求实际上在app上很普遍,可是在挪动端h5中,大家沒有长按的恶性事件,因此就必须自身仿真模拟这个恶性事件了。
大约实际效果以下:
ps: 以便做个gif还下了app,还得根据电子邮件发到电脑上上,脑瓜疼。。
思路
由此大家能够完成仿真模拟的长按恶性事件了。
上编码
请把关键放在JS上,这里贴出来详细的编码是以便便捷大伙儿看个细心,编码能够复制立即看实际效果
css广州中山大学一部分只是做了款式的清理,也有1刚开始让删掉按钮掩藏起来
HTML:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF⑻"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" type="text/css" href="./longpress.css" /> </head> <body> <div class="container"> <div class="label" id="label">长按我</div> <div class="delete_btn">删掉</div> </div> <script src="./longpress.js"></script> </body> </html>
JS
let timer = null let startTime = '' let endTime = '' const label = document.querySelector('.label') const deleteBtn = document.querySelector('.delete_btn') label.addEventListener('touchstart', function () { startTime = +new Date() timer = setTimeout(function () { deleteBtn.style.display = 'block' }, 700) }) label.addEventListener('touchend', function () { endTime = +new Date() clearTimeout(timer) if (endTime - startTime < 700) { // 解决点一下恶性事件 label.classList.add('selected') } })
CSS
.container { position: relative; display: inline-block; margin-top: 50px; } .label { display: inline-block; box-sizing: border-box; width: 105px; height: 32px; line-height: 32px; background-color: #F2F2F2; color: #5F5F5F; text-align: center; border-radius: 3px; font-size: 14px; } .label.selected { background-color: #4180cc; color: white; } .delete_btn { display: none; position: absolute; top: ⑻px; left: 50%; transform: translateX(⑸0%) translateY(⑴00%); color: white; padding: 10px 16px; background-color: rgba(0, 0, 0, .7); border-radius: 6px; line-height: 1; white-space: nowrap; font-size: 12px; } .delete_btn::after { content: ''; width: 0; height: 0; border-width: 5px; border-style: solid; border-color: rgba(0, 0, 0, .7) transparent transparent transparent; position: absolute; bottom: ⑼px; left: 50%; transform: translateX(⑸0%); }
ps: touchstart和touchend仅有在挪动端机器设备上才有效,假如要看编码示例的话请:
即点一下以下图:
以上便是本文的所有內容,期待对大伙儿的学习培训有一定的协助,也期待大伙儿多多适用脚本制作之家。
Copyright © 2002-2020 如何织梦建站_如何创建网站_网站建立_公众号搭建_网站建站的 版权所有 (网站地图) 粤ICP备10235580号