首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在div调整大小时停止点击计数

如何在div调整大小时停止点击计数
EN

Stack Overflow用户
提问于 2020-06-28 19:31:02
回答 3查看 78关注 0票数 3

我有一个函数可以检查在div上执行了多少次单击操作。此div具有调整大小的功能。因此,当我尝试调整它的大小时,调整大小操作将计入在该div上的点击。如何才能避免这种调整大小的点击计数。

代码语言:javascript
运行
复制
let i = 0;
$(".resize").click(function(){
  console.log(i++);
});
代码语言:javascript
运行
复制
.resize {
  color: rgb(0, 255, 255);
  mix-blend-mode: difference;
  margin: 0;
  padding: 0;
  width: 300px;
  height: 300px;
  border: 2px solid red;
  resize: both;
  overflow: auto;
}

/* Stack snippet specific CSS */
.as-console-wrapper {
  position: initial !important;
}
代码语言:javascript
运行
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="resize"></div>

如果调整了i的大小,我不想增加div的值。仅当在div的边框内单击时,才应递增i的值。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-06-28 21:24:30

代码语言:javascript
运行
复制
const ObserveResize = (function() {
  let store = [];

  function getDimension(el) {
    const rect = el.getBoundingClientRect();

    return {
      w: rect.width,
      h: rect.height
    };
  }

  function isResized({
    el,
    d
  }, d2) {
    return Object.keys(d).some(key => d[key] !== d2[key]);
  }

  function isSubscribed(el) {
    return !!store.filter(record => record.el === el).length;
  }

  function unsubscribe(el) {
    store = store.filter(h => h.el !== el);
  }

  function subscribe(el) {
    const unsubscriber = () => unsubscribe(el);

    if (isSubscribed(el)) return unsubscriber;

    const d = getDimension(el);

    store.push({
      el,
      d
    });

    return unsubscriber;
  }

  function run() {
    store.forEach(record => {
      const d2 = getDimension(el);

      if (isResized(record, d2)) {
        record.d = d2;
        el.dispatchEvent(new Event("resize"));
      }
    });

    requestAnimationFrame(run);
  }

  run();

  return subscribe;
})();

const el = document.querySelector(".resize");
let clickCount = 0;
let resizing = false;

el.addEventListener("click", () => {
  if (resizing) return;

  ++clickCount;

  console.log(`clickCount: ${clickCount}`);
});

function onMouseUp() {
  console.log("After Resize Mouse Up");
  requestAnimationFrame(() => {
    resizing = false;
  });

  el.removeEventListener("mouseup", onMouseUp);
}

el.addEventListener("resize", function() {
  resizing = true;

  el.removeEventListener("mouseup", onMouseUp);
  el.addEventListener("mouseup", onMouseUp);
});

const unsubscribe = ObserveResize(el);
代码语言:javascript
运行
复制
.resize {
  color: rgb(0, 255, 255);
  mix-blend-mode: difference;
  margin: 0;
  padding: 0;
  width: 100px;
  height: 100px;
  border: 2px solid red;
  resize: both;
  overflow: auto;
}


/* Stack snippet specific CSS */

.as-console-wrapper {
  position: initial !important;
}
代码语言:javascript
运行
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="resize"></div>

票数 1
EN

Stack Overflow用户

发布于 2020-06-28 20:03:35

以下是可行的解决方案:

代码语言:javascript
运行
复制
let i = 0;

$('.resize').click(function(e){
    if(!$(e.target).is('.ui-resizable-handle'))
     console.log(i++);
        $(this).toggleClass('selected');
}).resizable();
代码语言:javascript
运行
复制
.resize.selected { border: 2px solid black;}

.resize {
  color: rgb(0, 255, 255);

  margin: 0;
  padding: 0;
  width: 300px;
  height: 300px;
  border: 2px solid red;

  overflow: auto;
}

/* Stack snippet specific CSS */
.as-console-wrapper {
  position: initial !important;
}
代码语言:javascript
运行
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="resize"></div>

票数 1
EN

Stack Overflow用户

发布于 2020-06-28 19:53:10

一个简单的解决方案是计算窗口调整大小的次数,并减小i,这样它就只显示已单击但未调整大小的次数。

代码语言:javascript
运行
复制
$(".resize").addEventListener(myFunction);
var x =0;
function myFunction(){
x++; 
//Decrease the i with the times that the window has been 
resized
i-=x
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62621914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档