亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

長期在JavaScript?

長期在JavaScript?

慕尼黑8549860 2019-08-15 17:23:10
長期在JavaScript?是否可以在JavaScript(或jQuery)中實現“長按”?怎么樣?HTML<a href="" title="">Long press</a>JavaScript的$("a").mouseup(function(){   // Clear timeout   return false;}).mousedown(function(){   // Set timeout   return false; });
查看完整描述

3 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

沒有'jQuery'魔法,只有JavaScript定時器。

var pressTimer;$("a").mouseup(function(){
  clearTimeout(pressTimer);
  // Clear timeout
  return false;}).mousedown(function(){
  // Set timeout
  pressTimer = window.setTimeout(function() { ... Your Code ...},1000);
  return false; });


查看完整回答
反對 回復 2019-08-15
?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

根據Maycow Moura的回答,我寫了這個。它還確保用戶不會進行右鍵單擊,這會觸發長按并在移動設備上運行。DEMO

var node = document.getElementsByTagName("p")[0];var longpress = false;var presstimer = null;var longtarget = null;var cancel = function(e) {
    if (presstimer !== null) {
        clearTimeout(presstimer);
        presstimer = null;
    }

    this.classList.remove("longpress");};var click = function(e) {
    if (presstimer !== null) {
        clearTimeout(presstimer);
        presstimer = null;
    }

    this.classList.remove("longpress");

    if (longpress) {
        return false;
    }

    alert("press");};var start = function(e) {
    console.log(e);

    if (e.type === "click" && e.button !== 0) {
        return;
    }

    longpress = false;

    this.classList.add("longpress");

    if (presstimer === null) {
        presstimer = setTimeout(function() {
            alert("long click");
            longpress = true;
        }, 1000);
    }

    return false;};node.addEventListener("mousedown", start);node.addEventListener("touchstart", start);node.addEventListener("click", click);node.addEventListener("mouseout", cancel);node.addEventListener("touchend", cancel);node.addEventListener("touchleave", cancel);node.addEventListener("touchcancel", cancel);

您還應該使用CSS動畫包含一些指標:

p {
    background: red;
    padding: 100px;}.longpress {
    -webkit-animation: 1s longpress;
            animation: 1s longpress;}@-webkit-keyframes longpress {
    0%, 20% { background: red; }
    100% { background: yellow; }}@keyframes longpress {
    0%, 20% { background: red; }
    100% { background: yellow; }}


查看完整回答
反對 回復 2019-08-15
?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

您可以使用jQuery mobile API的taphold事件。

jQuery("a").on("taphold", function( event ) { ... } )


查看完整回答
反對 回復 2019-08-15
  • 3 回答
  • 0 關注
  • 545 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號