swiper 實現滑動解鎖

最近項目中有這樣一個需求,研究了兩種寫法一個原生,一個使用框架css

原生寫法:html

<!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="images/1closeLight.png" class="p2_bg1" /> //充當一個提示過分的效果
                <!--手滑動關閉圖-->
                <img src="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: 0;
    margin: 0;
    overflow: hidden;
}
html,body{
    width: 100%;
    height: 100%;
}
.page2{
    
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.page2>.bg2{
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: 100vw;
    background-color: #000000;
    opacity: 0.6;

}
.silder_bg{
    width: 78vw;
    height: 10vh;
    position: absolute;
    top:5vh;
    left: 0;
    right: 0;
    margin: 0 auto;
    z-index:200;
    background-color: #F2F2F2;
    border-radius: 6vh;

}

.p2_bg1{
    width: 10vh;
    height: 10vh;
    position: absolute;
    top:0;
    left: 20vw;
    right: 0;
    z-index:400;
    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:0;
    left: 0;
    right: 0;
    z-index:200;

}
.silder_bg span{
    width: 78vw;
    height: 5vh;
    position: absolute;
    top:3vh;
    left: 4vw;
    right: 0;

    z-index:200;
    font-family: "微軟雅黑";
    font-size:14px ;
    text-align: center;
}

以上是原生的寫法,還能夠使用jq的拖拽(draggable)這個方法css3

下面說下使用swiper的寫法web

<!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="images/1closeLight.png" class="p2_bg1" /> <!--手滑動關閉圖--> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide"> //去掉swiper.min.cssswiper-slide的默認樣式 <img src="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滑動的相對有彈性一點,所要監聽的距離更短,也可實現效果api

相關文章
相關標籤/搜索