Jas*_*ies 6 javascript jquery touchscreen onclick
我正在一个需要鼠标悬停菜单的网站上工作.我不建议从可访问性的角度来看鼠标悬停菜单,但使用jQuery实现它很容易.
问题是:我们还需要支持触摸屏设备(平板电脑).在这样的设备上,您没有鼠标,因此鼠标悬停事件不起作用.我希望jQuery有一个longpress事件,但事实并非如此.我确实找到了一个使用谷歌的jQuery longclick插件,但是它适用于jQuery 1.4,所以我并不热衷于使用它.此外,jQuery插件网站目前正在维护,所以这不是很有帮助.
所以问题是:是否有一个优雅的jQuery 1.7/1.8插件来支持longpress/longclick事件?
事实证明,你可以使用jQuery 1.8的jQuery 1.4 现有的longclick插件.
$("#area").mousedown(function(){
$("#result").html("Waiting for it...");
});
$("#area").longclick(500, function(){
$("#result").html("You longclicked. Nice!");
});
$("#area").click(function(){
$("#result").html("You clicked. Bummer.");
});Run Code Online (Sandbox Code Playgroud)
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="http://rawgit.com/pisi/Longclick/master/jquery.longclick-min.js"></script>
<p id="area">Click me!</p>
<p id="result">You didn't click yet.</p>Run Code Online (Sandbox Code Playgroud)
您可以做的是setTimeout在各种鼠标事件期间使用延迟检查。结合 jQuery$.data()来存储跨事件(在每个元素上)的超时应该可以帮助您完成这一切。下面是一个例子:
HTML:
<ul id="menu">
<li><a href="#" onclick="return false;" class="test"></a></li>
<li><a href="#" onclick="return false;" class="test"></a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
JS:
var mousepress_time = 1000; // Maybe hardcode this value in setTimeout
var orig_message = "Click Here"; // Remove this line
var held_message = "Down"; // Remove this line
$(document).ready(function () {
$(".test")
.html(orig_message) // Remove this line
.on("mousedown", function () {
console.log("#########mousedown"); // Remove this line
var $this = $(this);
$(this).data("checkdown", setTimeout(function () {
// Add your code to run
$this.html(held_message); // Remove this line
}, mousepress_time));
}).on("mouseup", function () {
clearTimeout($(this).data("checkdown"));
console.log("#######mouseup"); // Remove this line
$(this).html(orig_message); // Remove this line
}).on("mouseout", function () {
clearTimeout($(this).data("checkdown"));
console.log("#######mouseout"); // Remove this line
$(this).html(orig_message); // Remove this line
});
});
Run Code Online (Sandbox Code Playgroud)
演示:http : //jsfiddle.net/7jKYa/10/
这还有很多事情要做,因为您还合并了悬停,但在大多数情况下,我认为这可以满足您的需求。
如有必要,它可以轻松转换为插件,否则我认为它可以单独工作。我希望这会有所帮助!
| 归档时间: |
|
| 查看次数: |
21599 次 |
| 最近记录: |