js案例 移动端左右滑动touch+transform
2016-11-29 22:44
375 查看
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
<style>
*{
padding: 0;
margin: 0;
/*margin-top:100px;*/
}
#move{
height: 80px;
width: 99%;
border: 1px solid red;
font-size: 19px;
text-align: center;
padding-top: 20px;
box-sizing: border-box;
font-weight: 700;
}
.box{
height: 64px;
width: 200px;
border: 4px solid red;
margin: 80px auto;
/*overflow: hidden;*/
}
.box2{
height: 64px;
width: 260px;
border: 4px solid black;
/*padding: 2px;*/
box-sizing:border-box;
}
.left{
height: 56px;
width: 200px;
border: 4px solid royalblue;
float: left;
box-sizing:border-box;
}
.right{
height: 56px;
width: 52px;
border: 4px solid chartreuse;
float: right;
box-sizing:border-box;
/*transform: translate(100px);*/
}
button{
height: 50px;
width: 100%;
font-size: 19px;
}
</style>
</head>
<body>
<div id="move"> 移动端触屏左右滑动 </div>
<div class="box">
<div class="box2">
<div class="left"></div>
<div class="right"></div>
</div>
</div>
<button >移动端点击滑动</button>
</body>
<script type="text/javascript">
window.onload = function(){
var stus = true;
var but = document.querySelector('button');
var box2 = document.querySelector('.box2');
but.onclick = function(){
console.log(stus);
var right = document.querySelector('.box2');
if(stus){
right.style.transform = "translate("+-52+"px)";
right.style.transition = 'all .5s';
stus=!stus;
}else{
right.style.transform = "";
stus=!stus;
}
}
//开始移动时 要做的事
box2.addEventListener('touchstart',function(event){
startX = event.touches[0].clientX;
console.log(startX);
})
//移动过程中 要做的事
box2.addEventListener('touchmove',function(event){
moveX = event.touches[0].clientX - startX;
console.log(moveX);
})
//移动结束时 要做的事
box2.addEventListener('touchend',function(event){
var right = document.querySelector('.box2');
if(stus){
right.style.transform = "translate("+-52+"px)";
right.style.transition = 'all .7s';
stus=!stus;
}else{
right.style.transform = "";
stus=!stus;
}
})
}
</script>
</html>
相关文章推荐
- js实现移动端手指左右上下滑动翻页效果
- 浅谈移动端之js touch事件 手势滑动事件
- JS案例之5——移动端触屏滑动
- 基于JS移动端图片上下左右滑动图片的对比度与灰度变化
- 移动端touch事件---利用bootstrap实现轮播图手指左右滑动事件
- 原生JS实现移动端模块的左右滑动切换效果,基于vue、stylus
- Swiper.js 实现移动端元素左右滑动
- 原生js写头部tab栏左右滑动支持移动端
- 移动端上下滑动事件之--坑爹的touch.js
- 手机端左右滑动,不用写js(只有页面切换到移动端可以看)
- js html 轮播图 支持移动端左右滑动 pc也能用
- 浅谈移动端之js touch事件 手势滑动事件
- 移动端上下滑动事件之--坑爹的touch.js
- Js控制滑轮左右滑动实例
- 页面图片浮动左右滑动效果的简单实现案例
- TouchSlide移动端触屏滑动banner插件
- JS 移动端触屏滑动
- 最简单的左右滑动翻页案例
- swipe.js 2.0 轻量级框架实现mobile web 左右滑动
- 关于移动端的一个左右侧栏目滑动的效果