我有一个函数可以检查在div上执行了多少次单击操作。此div具有调整大小的功能。因此,当我尝试调整它的大小时,调整大小操作将计入在该div上的点击。如何才能避免这种调整大小的点击计数。
let i = 0;
$(".resize").click(function(){
console.log(i++);
});
.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;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="resize"></div>
如果调整了i
的大小,我不想增加div的值。仅当在div的边框内单击时,才应递增i
的值。
发布于 2020-06-28 21:24:30
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);
.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;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="resize"></div>
发布于 2020-06-28 20:03:35
以下是可行的解决方案:
let i = 0;
$('.resize').click(function(e){
if(!$(e.target).is('.ui-resizable-handle'))
console.log(i++);
$(this).toggleClass('selected');
}).resizable();
.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;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="resize"></div>
发布于 2020-06-28 19:53:10
一个简单的解决方案是计算窗口调整大小的次数,并减小i,这样它就只显示已单击但未调整大小的次数。
$(".resize").addEventListener(myFunction);
var x =0;
function myFunction(){
x++;
//Decrease the i with the times that the window has been
resized
i-=x
}
https://stackoverflow.com/questions/62621914
复制相似问题