最近项目中有这样一个需求,研究了两种写法一个原生,一个使用框架

原生写法:

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="css/huapin.css" />
<!-- <link rel="stylesheet" href="css/swiper.min.css" />-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
</head> <body>
<div class="page2">
<div class="silder_bg">
<span>滑动滑动</span>
<!--滑动的白点-->
<img src="data:images/1closeLight.png" class="p2_bg1" /> //充当一个提示过度的效果
<!--手滑动关闭图-->
<img src="data:images/2closeBar.png" id='silder' class="p2_bg2"/>
</div>
</div>
<div class="page3"> </div>
</body>
<script src="js/zepto.js"></script>
<script>
document.getElementById('silder').addEventListener('touchmove',function(event){ //使用touchmove监听滑动
event.preventDefault();
var el = event.target;
var touch = event.touches[0];
var curX = touch.pageX - this.offsetLeft - 73; if(curX <= 0) return;
if(curX > 224){
//符合条件需要执行的事件
$(".page2").hide();
$(".page3").show();
setTimeout(function(){
p2show()
},2000);
}
el.style.webkitTransform = 'translateX(' + curX + 'px)';//使其在x轴位移
},false); document.getElementById('silder').addEventListener('touchend', function(event) { //使用touchend监听滑动结束
this.style.webkitTransition = '-webkit-transform 0.3s ease-in';
this.addEventListener( 'webkitTransitionEnd', function( event ) { this.style.webkitTransition = 'none'; }, false );
this.style.webkitTransform = 'translateX(0px)';
}, false);
</script> </html>
huapin.css:
*{
border:;
margin:;
overflow: hidden;
}
html,body{
width: 100%;
height: 100%;
}
.page2{ position: fixed;
top:;
left:;
width: 100%;
height: 100%;
} .page2>.bg2{
position: fixed;
top:;
left:;
height: 100vh;
width: 100vw;
background-color: #000000;
opacity: 0.6; }
.silder_bg{
width: 78vw;
height: 10vh;
position: absolute;
top:5vh;
left:;
right:;
margin: 0 auto;
z-index:;
background-color: #F2F2F2;
border-radius: 6vh; } .p2_bg1{
width: 10vh;
height: 10vh;
position: absolute;
top:;
left: 20vw;
right:;
z-index:;
animation: light 3s linear infinite;
-webkit-animation:light 3s linear infinite;
}
/*滑动css3动画*/
@keyframes light{
from{left:12vw;}
to{left:60vw ;}
}
@-webkit-keyframes light{
from{left:10vw;opacity: 0.4;}
to{left:55vw ;opacity: 0.4;}
} .p2_bg2{
width: 10vh;
height: 10vh;
position: absolute;
top:;
left:;
right:;
z-index:; }
.silder_bg span{
width: 78vw;
height: 5vh;
position: absolute;
top:3vh;
left: 4vw;
right:; z-index:;
font-family: "微软雅黑";
font-size:14px ;
text-align: center;
}

以上是原生的写法,还可以使用jq的拖拽(draggable)这个方法

下面说下使用swiper的写法

<!doctype html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>Swiper Playground</title>
<link rel="stylesheet" href="css/swiper.min.css">
<link rel="stylesheet" href="css/huapin.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<style>
html,
body {
position: relative;
height: 100%;
} body {
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
} .swiper-container {
width: 100%;
height: 100%;
z-index: 300;
} img {
width: 100%;
} .swiper-slide {
z-index: 300;
} .p2_bg {
width: 10vh;
height: 10vh;
z-index: 300;
}
</style>
</head> <body>
//html布局同上面,只是加了个swiper-container容器
<div class="silder_bg">
<span>滑动滑动</span>
<!--滑动的白点-->
<img src="data:images/1closeLight.png" class="p2_bg1" />
<!--手滑动关闭图-->
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide"> //去掉swiper.min.css对swiper-slide的默认样式
<img src="data:images/2closeBar.png" class="p2_bg" />
</div> </div>
</div>
</div>
<script src="js/zpto.js"></script>
<script src="js/swiper.min.js"></script>
<script>
简单思路:滑动swiper时监听滑动距离判断条件即可
var swiper = new Swiper('.swiper-container', {
slidesPerView: 'auto',
freeMode: true,
on: {
touchMove: function() {
// alert(swiper.translate)
if(swiper.translate > 130) {
$(".silder_bg").fadeOut(500)
}
},
},
});
</script>
</body> </html>

以上两种写法,原生的实现起来互动的更加快速,swiper滑动的相对有弹性一点,所要监听的距离更短,也可实现效果

最新文章

  1. 跨语言和跨编译器的那些坑(CPython vs IronPython)
  2. iOS_autoLayout_Masonry
  3. 我的android学习经历14
  4. CodeIgniter 让控制器可以支持多级子目录的 Router 类库
  5. Linux下解压命令
  6. ORACLE 变量定义
  7. hibernate3整合spring2时hibernate即用注解又用配置文件情况时spring配置文件的配置写法
  8. 【BZOJ】1013: [JSOI2008]球形空间产生器sphere
  9. dmesg 程序崩溃调试
  10. ubuntu错误解决。
  11. 计算textView的高度
  12. Android中使用NDK
  13. 免费的Visual Studio的插件
  14. CodeIgniter 如何去掉 Index.php
  15. mongoose+koa2 按照_id更新多条数据,删除数组中的字段,然后添加新的字段,$pull和$or结合使用
  16. python 解除装饰器,调用原本函数。
  17. cookie的中文乱码问题【URL编码解码】
  18. Java知多少(35)Object类
  19. iOS 开发笔记-Objective-C之KVC、KVO
  20. 【java开发系列】—— 自定义注解

热门文章

  1. Kubernetes基本概念与架构
  2. linux高级应用第九章-正则表达式
  3. Redis学习笔记(1)
  4. 关于如何查看论文是否被SCI或者EI收录
  5. 你确信 X-Forwarded-For 拿到的就是用户真实 IP 吗?
  6. PreparedStatement实现针对不同表的通用查询操作
  7. 北京理工大学复试上机--2001A
  8. DCL单例为什么要加Volatile
  9. Java实现 LeetCode 128 最长连续序列
  10. Java实现 洛谷 P1015 回文数(N进制回文数)