1、js代码

 //触摸开始事件,改变元素的样式
function touchstart(e) {
$(this).removeClass("touchend").addClass("touchstart");
if (e.data.cancelBubble) {
if (e.cancelBubble) {
e.cancelBubble = true;
}
if (e.stopPropagation()) {
e.stopPropagation();
}
}
}
//触摸结束事件,恢复元素样式
function touchend(e) {
$(this).addClass("touchend").removeClass("touchstart");
if (e.data.cancelBubble) {
if (e.cancelBubble) {
e.cancelBubble = true;
}
if (e.stopPropagation()) {
e.stopPropagation();
}
}
}
$.extend({
//注册全局触摸事件,委托给document,只要在需要实现触摸效果的元素上加上 touchable类即可
globalTouchable: function () {
$.disableGlobalTouchable();
$(document)
.on("touchstart mousedown mouseenter", ".touchable", {}, touchstart)
.on("touchend touchcancel mouseup mouseleave", ".touchable", {}, touchend);
return this;
},
disableGlobalTouchable: function () {
$(document)
.off("touchstart mousedown mouseenter", ".touchable", touchstart)
.off("touchend touchcancel mouseup mouseleave", ".touchable", touchend);
return this;
}
});
$.fn.extend(
{
/*
* 启用匹配元素的触摸效果,
*cancelBubble:
* 是否取消事件冒泡,避免父元素出现触摸效果
*/
touchable: function (cancelBubble) {
$(this)
.addClass("touchable")
.off("ouchstart mousedown mouseenter", null, touchstart)
.off("touchend touchcancel mouseup mouseleave", null, touchend)
.on("touchstart mousedown mouseenter", null, { cancelBubble: cancelBubble }, touchstart)
.on("touchend touchcancel mouseup mouseleave", null, { cancelBubble: cancelBubble }, touchend);
return this;
},
/*
*取消匹配元素的触摸效果
*/
untouchable: function () {
$(this)
.off("ouchstart mousedown mouseenter",null, touchstart)
.off("touchend touchcancel mouseup mouseleave", null, touchend);
return this;
},
});

2、css代码

 .touchable{
background-color:whitesmoke;
}
/*点击时颜色*/
.touchable.touchstart{
background-color:gainsboro;
}
/*淡出效果*/
.touchable.touchend{
transition:background-color ease-out 0.3s;
}

3、使用示例

 <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Touchable</title>
<link href="~/css/touchable.css" rel="stylesheet" />
<style>
html, body {
width: 100%;
height: 100%;
} body {
display:flex;
align-items:center;
justify-content:center;
flex-direction:column;
}
body > div {
width: 60vw;
height: 20vh;
display:flex;
align-items:stretch;
}
body > div > div {
flex:1;
border:solid 1px white;
padding:2rem;
}
body > div > div > div {
padding:2rem;
height:100%;
width:100%;
background-color:green;
}
body > div > div > div >div{
padding: 2rem;
height: 100%;
width: 100%;
background-color:yellow;
}
</style> </head>
<body>
<div>
<div class="touchable"></div>
<div class="touchable"></div>
<div class="touchable"></div>
</div>
<div>
<div class="touchable"></div>
<div class="touchable"></div>
<div class="touchable"></div>
</div>
<div>
<div class="touchable"></div>
<div class="touchable"></div>
<div class="touchable"></div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="~/js/touchable.js?v=9"></script>
<script>
$.globalTouchable();
</script>
</body>
</html>

效果图

jQuery拓展简易快速实现触摸效果的更多相关文章

  1. jQuery响应式幻灯片插件jquery.glide.js(支持触摸&轻量级)

    找到一款好的幻灯片插件不容易,找到一款功能全并且使用很简单的幻灯片更不容易,今天为大家分享一款全能的幻灯片插件glide.js,也是我现在在使用的一款插件. jquery.glide.js是响应和触摸 ...

  2. jquery 仿手机屏幕切换界面效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. jQuery实现鼠标经过图片变亮效果

    在线体验效果:http://hovertree.com/texiao/jquery/1.htm 以下是完整源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  4. jQuery文本段落展开和折叠效果

    <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...

  5. jQuery对json快速赋值

    jQuery对json快速赋值,重点在于将input的id取跟JSON同样的名称. <!DOCTYPE html> <html> <head lang="en& ...

  6. JQuery实现的模块交换动画效果

    <!doctype html> <html> <head> <meta http-equiv="content-type" content ...

  7. 基于 jQuery 实现垂直滑动的手风琴效果

    今天我们要与大家分享一个漂亮而灵活的垂直 jQuery 手风琴效果.其主要思想是扩大手风琴片上的点击和显示更多的信息.其他内容片段将变得不那么透明.当使用一个导航箭头导航下一个片段,新的片会从顶部或底 ...

  8. jQuery鼠标悬停内容动画切换效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 【jquery】基于 jquery 实现 ie 浏览器兼容 placeholder 效果

    placeholder 是 html5 新增加的属性,主要提供一种提示(hint),用于描述输入域所期待的值.该提示会在输入字段为空时显示,并会在字段获得焦点时消失.placeholder 属性适用于 ...

随机推荐

  1. webrtc底层一对一连接过程探索(三)

    一.连接过程继续解读-----fun33-fun35解读 完整代码如下: //fun33-37 console.error('fun35-37==>2332==>2332'); var q ...

  2. etcd集群部署

    etcd是用于共享配置和服务发现的分布式KV存储系统,随着CoreOS和Kubernetes等项目在开源社区日益火热,它们都用到了etcd组件作为一个高可用.强一致性的服务发现存储仓库.操作系统版本: ...

  3. Python数据结构之三——dict(字典)

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 知识源于生活.Python也是如此. 提到字典,我首先想到的是数学大师--高斯. 为何想起他呢?这主要是因为高斯算法 ...

  4. 简单的led驱动程序设计

    基于ok6410: led驱动程序: vim led.c #include<linux/kernel.h>#include<linux/module.h>#include< ...

  5. 【JavaWeb】客户关系管理系统

    前言 为了巩固开发的流程,我们再拿一个客户关系管理系统来练手...! 成果图 我们完成的就是下面的项目! 搭建配置环境 配置Tomcat 导入开发包 建立开发用到的程序包 在数据库创建相对应的表 CR ...

  6. PHP开发中多种方案实现高并发下的抢购、秒杀功能

    抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个: 1 高并发对数据库产生的压力 2 竞争状态下如何解决库存的正确减少("超卖"问题) 对于第一个问题,已经很容易想到 ...

  7. Duilib第一步(I)-简介与环境搭建

    Primus gradus et cognoscetis veritatem et veritas liberabit vos.  --Johannes 8:32 Introduction Duili ...

  8. 1.2 decimal模块

    >>> 0.1 + 0.1 +0.1 == 0.3 False >>> >>> print(0.1 + 0.1 + 0.1) 0.30000000 ...

  9. PHP实现网页爬虫

    抓取某一个网页中的内容,需要对DOM树进行解析,找到指定节点后,再抓取我们需要的内容,过程有点繁琐.LZ总结了几种常用的.易于实现的网页抓取方式,如果熟悉JQuery选择器,这几种框架会相当简单. 一 ...

  10. qdoc.exe

    qdoc.exe Usage: qdoc [options] file1.qdocconf ... # Qt文档生成器 Qt documentation generator # 设置 Options: ...