怎么在html5中模擬長(zhǎng)按事件-創(chuàng)新互聯(lián)

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

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),雨山企業(yè)網(wǎng)站建設(shè),雨山品牌網(wǎng)站建設(shè),網(wǎng)站定制,雨山網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,雨山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

思路


  • 放棄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)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站欄目:怎么在html5中模擬長(zhǎng)按事件-創(chuàng)新互聯(lián)
分享URL:http://muchs.cn/article4/dcjhoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、響應(yīng)式網(wǎng)站、做網(wǎng)站、網(wǎng)站維護(hù)、網(wǎng)站收錄企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化