溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在html5中模擬長(zhǎng)按事件

發(fā)布時(shí)間:2021-05-18 16:30:05 來(lái)源:億速云 閱讀:264 作者:Leah 欄目:web開(kāi)發(fā)

本篇文章為大家展示了怎么在html5中模擬長(zhǎng)按事件,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

思路

  • 放棄click事件,通過(guò)判斷按的時(shí)長(zhǎng)來(lái)決定是單擊還是長(zhǎng)按

  • 使用touchstart和touchend事件

  • 在touchstart中開(kāi)啟一個(gè)定時(shí)器,比如在700ms后顯示一個(gè)長(zhǎng)按菜單

  • 在touchend中清除這個(gè)定時(shí)器,這樣如果按下的時(shí)間超過(guò)700ms,那么長(zhǎng)按菜單已經(jīng)顯示出來(lái)了,清除定時(shí)器不會(huì)有任何影響;如果按下的時(shí)間小于700ms,那么touchstart中的長(zhǎng)按菜單還沒(méi)來(lái)得及顯示出來(lái),就被清除了。

由此我們可以實(shí)現(xiàn)模擬的長(zhǎng)按事件了。

上代碼

請(qǐng)把重點(diǎn)放在JS上,這里貼出來(lái)完整的代碼是為了方便大家看個(gè)仔細(xì),代碼可以拷貝直接看效果
css中大部分只是做了樣式的美化,還有一開(kāi)始讓刪除按鈕隱藏起來(lái)

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <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">長(zhǎng)按我</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) {
    // 處理點(diǎn)擊事件
    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: -8px;
    left: 50%;
    transform: translateX(-50%) translateY(-100%);
    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: -9px;
    left: 50%;
    transform: translateX(-50%);
}

ps: touchstart和touchend只有在移動(dòng)端設(shè)備上才有用,如果要看代碼示例的話請(qǐng):

  1. 用chrome

  2. F12打開(kāi)調(diào)時(shí)窗

  3. 切換到模擬移動(dòng)設(shè)備

即點(diǎn)擊如下圖:

怎么在html5中模擬長(zhǎng)按事件

上述內(nèi)容就是怎么在html5中模擬長(zhǎng)按事件,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI