延迟jquery/javascript中ondragover的效果
delay the effects of ondragover in jquery/javascript
我正试图以较慢的速度将div拖过10%,使其淡出。然而,如果我超快速地移动鼠标,它会立即变黑,并带有一些褪色的方块。
Q: 如何使正方形慢慢变黑10%,但当用户不再拖动鼠标时能够停止。
提前感谢,我尝试过延迟函数等技术,但没有成功。
(function() {
//When the mouse drags over the class cell, lower the opacity by 10%
$('.cell').on('dragover', function () {
var $currentOpacity = $(this).css("opacity");
$currentOpacity -= 0.1;
$(this).css('opacity', $currentOpacity);
});
})();
#grid-container {
width: 398px;
height: 25px;
font-size: 0;
background-color: black;
position: absolute;
}
.cell {
margin: 0.5px;
height: 5mm;
width: 5mm;
background-color: white;
display: inline-block;
z-index: 0;
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div id="grid-container">
<div class="row line-1">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
</div>
</body>
</html>
您可以尝试以下操作:
(function() {
//When the mouse drags over the class cell, lower the opacity by 10%
$('.cell').on('dragover', function () {
var $currentOpacity = $(this).css("opacity");
$currentOpacity -= 0.1;
var $element = $(this);
//prevent reset
clearTimeout($element.resetEvent)
//update opacity
$element.css('opacity', $currentOpacity);
//trigger reset after 2 seconds
$element.resetEvent = setTimeout(function(){
$element.css('opacity', 1);
}, 2000);
});
})();
如果有2秒钟处于不活动状态,则不透明度将返回到1。但是,如果dragover
被触发,它将阻止重置并重新启动时钟。
另一个解决方案是使用CSS转换。将"background-color"
的转换添加到类中,并使用新类更改dragover
事件中元素的"background-color"
。
(function() {
//When the mouse drags over the class cell, lower the opacity by 10%
$('.cell').on('dragover', function () {
/* instead of calculation, only add class to element. */
$(this).addClass("dragged");
});
})();
#grid-container {
width: 398px;
height: 25px;
font-size: 0;
background-color: black;
position: absolute;
}
.cell {
margin: 0.5px;
height: 5mm;
width: 5mm;
background-color: white;
display: inline-block;
z-index: 0;
-webkit-transition: background-color 1000ms linear;
-moz-transition: background-color 1000ms linear;
-o-transition: background-color 1000ms linear;
-ms-transition: background-color 1000ms linear;
transition: background-color 1000ms linear;
}
.dragged {
background-color: black;
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div id="grid-container">
<div class="row line-1">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
</div>
</body>
</html>
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 如何使用jQuery自动打开页面上的所有链接
- 如何使用jquery在填充自动完成的值后使文本框只读
- jQuery:循环一个具有不同超时值的循环
- 用程序搜索JQuery数据表中的文本
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- jQuery匹配JSON对象的部分文本
- Jquery POST未填充数组
- JQuery使计数器每次更改时都会增加
- 如何在Angular2中使用jQuery插件
- 提高JQuery的性能
- 无法在通过jQuery的ajax加载的页面中执行javascript
- JQuery合并了keyup和focusout两个函数
- 如何使用jQuery选择下拉列表的值
- 将PHP变量传递给jQuery时遇到问题
- jQuery UI自动完成突然停止工作
- 剑道网格jQuery动画()问题
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- 延迟jquery/javascript中ondragover的效果
- 为什么不't ondragover在jQuery插件中工作