jQuery拓展简易快速实现触摸效果
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拓展简易快速实现触摸效果的更多相关文章
- jQuery响应式幻灯片插件jquery.glide.js(支持触摸&轻量级)
找到一款好的幻灯片插件不容易,找到一款功能全并且使用很简单的幻灯片更不容易,今天为大家分享一款全能的幻灯片插件glide.js,也是我现在在使用的一款插件. jquery.glide.js是响应和触摸 ...
- jquery 仿手机屏幕切换界面效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jQuery实现鼠标经过图片变亮效果
在线体验效果:http://hovertree.com/texiao/jquery/1.htm 以下是完整源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- jQuery文本段落展开和折叠效果
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...
- jQuery对json快速赋值
jQuery对json快速赋值,重点在于将input的id取跟JSON同样的名称. <!DOCTYPE html> <html> <head lang="en& ...
- JQuery实现的模块交换动画效果
<!doctype html> <html> <head> <meta http-equiv="content-type" content ...
- 基于 jQuery 实现垂直滑动的手风琴效果
今天我们要与大家分享一个漂亮而灵活的垂直 jQuery 手风琴效果.其主要思想是扩大手风琴片上的点击和显示更多的信息.其他内容片段将变得不那么透明.当使用一个导航箭头导航下一个片段,新的片会从顶部或底 ...
- jQuery鼠标悬停内容动画切换效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【jquery】基于 jquery 实现 ie 浏览器兼容 placeholder 效果
placeholder 是 html5 新增加的属性,主要提供一种提示(hint),用于描述输入域所期待的值.该提示会在输入字段为空时显示,并会在字段获得焦点时消失.placeholder 属性适用于 ...
随机推荐
- 联合查询到gridview
using com.DAL.Base; using DAL.ruanmou; using System; using System.Collections.Generic; using System. ...
- nginx隐藏版本号
在实际运用nginx中我们最好将我们的版本号直接隐藏,因为有些版本号的nginx被爆出了漏洞,所以我们为了安全起见,最好将我们的版本号隐藏. 1.对于未安装的nginx我们使用源码编译安装才可以将版本 ...
- HashMap----工作原理
先来些简单的问题 "你用过HashMap吗?" "什么是HashMap?你为什么用到它?" 几乎每个人都会回答"是的",然后回答HashMa ...
- Java常用日志框架介绍
Java常用日志框架介绍 java日志概述 对于一个应用程序来说日志记录是必不可少的一部分.线上问题追踪,基于日志的业务逻辑统计分析等都离不日志.java领域存在多种日志框架,目前常用的日志框架包括L ...
- 炸金花的JS实现从0开始之 -------现在什么都不会(1)
新年结束了.回想起来唯一留下乐趣的就是在家和朋友玩玩炸金花. 遂有此文. 对不起,我这时候还没有思路. 让我捋一捋. ... ... 捋一捋啊... ... 好了.今天先这样吧: (1)先整理出所有的 ...
- [业界良心系列] OI资料分享
正式退役辣....混吃等死了这么久以后....终于也是必然的结果吧.... 分享一些资料: 链接:http://pan.baidu.com/s/1c1SRFmo 密码:bcfc 有一些资料有版权, 如 ...
- IDA学习笔记 函数调用约定
stdcall和cdecl: stdcall和cdecl 压栈方向都是从右到左 区别在于c约定是调用方在函数返回后add esp,n指令清除堆栈中的参数,而stdcall在被调函数内使用ret n来清 ...
- [Uva10294]Arif in Dhaka
[Uva10294]Arif in Dhaka 标签: 置换 Burnside引理 题目链接 题意 有很多个珠子穿成环形首饰,手镯可以翻转和旋转,项链只能旋转.(翻转过的手镯相同,而项链不同) 有n个 ...
- 关于 JS 拖拽功能的冲突问题及解决方法
前言 我在之前写过关于 JS 拖拽的文章,实现方式和网上能搜到的方法大致相同,别无二致,但是在一次偶然的测试中发现,这种绑定事件的方式可能会和其它的拖拽事件产生冲突,由此产生了对于事件绑定的思考.本文 ...
- linux开放80 端口
1.使用su登录管理员用户 2.编辑防火墙配置文件 vim /etc/sysconfig/iptables 3.在里面加入后保存 #open port 80 -A INPUT -p TCP -m st ...