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 属性适用于 ...
随机推荐
- CSS学习(一)---使用CSS的四种方式
1. 行内样式 例: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- LeetCode - 626. Exchange Seats
Mary is a teacher in a middle school and she has a table seat storing students' names and their corr ...
- Math Jax开源数学编辑器的使用
首先,这是一个开源免费,同时也可以支持扩展的软件. 使用API文档: 中文网站(http://mathjax-chinese-doc.readthedocs.io/en/latest/index.ht ...
- golang fmt.printf()
package main import "fmt" import "os" type point struct { x, y int } func main ...
- Supervisor 安装及配置管理uwsgi进程
Supervisor介绍 Supervisor 允许其用户在UNIX类操作系统上控制多个进程. 块如下: 方便 需要为每个进程实例编写rc.d脚本通常是不方便的. rc.d脚本是进程初始化/自动启动/ ...
- 940C Phone Numbers
传送门 题目大意 给你两个数字n和k,给你一个字符串s,n是s的长度,求字母集合是s的字母集合子集的字典序大于s的长度为k的字典序最小的字符串t 分析 将字符转化为数字,然后分两种情况处理: 1.n& ...
- Mysql字符串截取总结:left()、right()、substring()、substring_index()
同步首发:http://www.yuanrengu.com/index.php/20171226.html 在实际的项目开发中有时会有对数据库某字段截取部分的需求,这种场景有时直接通过数据库操作来实现 ...
- Winform下去除MDI窗体边框
做项目中间遇到了MDI窗体内边框的问题,经过苦苦寻找,最终得到了解决方案 在Main窗体中调用API // Win32 Constants ; ; private const int WS_BORDE ...
- IntentService源码
原文地址IntentService源码分析 @Override public void onCreate() { super.onCreate(); HandlerThread thread = ne ...
- python中__name__=='__main__'的作用
学习python语法的过程中碰到了__name__=='__main__',这里做个笔记. 作用 这段代码的作用就是让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行. 测试 先 ...