首頁 > web前端 > H5教程 > 主體

zepto實現行動端無縫向上下滾動

php中世界最好的语言
發布: 2018-03-26 13:28:47
原創
3485 人瀏覽過

這次給大家帶來zepto實現移動端無縫向上下滾動,zepto實現移動端無縫向上下滾動的注意事項有哪些,下面就是實戰案例,一起來看一下。

公司的行動端專案是基於zepto的,有一個頁面要求文字能夠無縫地不停向上滾動,但查了網上的資料,大多都是基於jquery的,雖然稍作修改就可以用於移動端,但不能實現觸摸上下翻滾。所以就去了zepto的官網查看其API,卻發現如果要使用zepto的swipe()方法,需要引用其已經封裝好的touch.js文件,我就趕緊引用了這個js文件,可在實際測試中,官網給的touch.js檔案不能適用於swipe()方法,於是,一頭霧水,繼續查資料,由於網上關於zepto方面的東西較少,所以,也沒有找出其不能適用於swipe()方法的原因。後來,不經意發現由百度雲Clouda團隊開發的一個touch.js(目前,js也是由這個團隊在維護),在這個js環境下居然能使用swipe()方法,於是,趕緊拿來測試。結果很OK哇!這裡要著重感謝百度雲Clouda團隊,你們真牛! ! !  在這裡要注意,zepto本身是沒有animate()方法的,它是將這個方法封裝成了一個fx.js(去官網下載),所以在使用animate()時要引用fx.js。

若您覺得本插件有bug或不足之處,請留言,我會及時修改,謝謝!

以下是基於zepto的行動端無縫向上捲動並上下觸碰滑動外掛程式的完整程式碼:

HTML部分:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" >
<title>无标题文档</title>
<style>
*{margin:0;padding:0}
li{list-style:none;}
.box{margin:20px;width:200px;height:128px;overflow:hidden;border:1px solid #ccc;padding:5px 10px 15px;font-size:14px;}
.box ul li{line-height:20px;}
</style>
</head>
<body>
<p class="box">
  <ul>
    <li>11111111111222222</li>
    <li>2222222202</li>
    <li>3333333303</li>
    <li>4444444404</li>
    <li>5555555505</li>
    <li>6666666606</li>
    <li>1111111111</li>
    <li>2222222202</li>
    <li>3333333303</li>
    <li>4444444404</li>
    <li>5555555505</li>
    <li>6666666606</li>
  </ul>
</p>
<script src="zepto.min.js"></script>
<script src="fx.js"></script>
<script src="touch-0.2.14.min.js"></script>
<script src="zepto.textSlider.js"></script>
<script>
$(function(){
    $(".box").textSlider({
        speed: 50, //数值越大,速度越慢
        line:10    //触摸翻滚的条数
    });
    })
</script>
</body>
登入後複製

外掛程式zepto.textSlider.js 部分:

/*
* textSlider 0.1
* Copyright (c) 2014 tnnyang 
* Dependence Zepto v1.1.6 & fx.js & touch-0.2.14.min.js
* Author by 小坏
*/ 
(function($){
    $.fn.textSlider = function(options){
    //默认配置
    var defaults = {
        speed:40,  //滚动速度,值越大速度越慢
        line:1     //滚动的行数
    };
    
    var opts = $.extend({}, defaults, options);
    
    var $timer;
    function marquee(obj, _speed){                                              
        var top = 0;
        var margintop;
        $timer = setInterval(function(){            
            top++;
            margintop = 0-top;
            obj.find("ul").animate({
                marginTop: margintop
                },0,function(){
                    var s = Math.abs(parseInt($(this).css("margin-top")));                                
                    if(s >= 20){
                        top = 0;
                        $(this).css("margin-top", 0);   //确保每次都是从0开始,避免抖动
                        $(this).find("li").slice(0, 1).appendTo($(this));                
                    }
                });                        
        }, _speed);
      }
      
    this.each(function(){            
        var speed = opts["speed"],line = opts["line"],_this = $(this);
        var $ul =_this.find("ul");
        if($ul.height() > _this.height()){            
            marquee(_this, speed);
        }
        
        //触摸开始
        _this.on('touchstart', function(ev){
            ev.preventDefault();
            clearInterval($timer);
        });
        
        //向上滑动
        _this.on('swipeup', function(ev){
            ev.preventDefault();
            clearInterval($timer);
            if($ul.height() > _this.height()){    
               for(i=0;i<opts.line;i++){
                    $ul.find("li").first().appendTo($ul);
                   }
                $ul.css("margin-top",0);
            }
        });
        
        //向下滑动
        _this.on(&#39;swipedown&#39;, function(ev){
            ev.preventDefault();
            clearInterval($timer);
            if($ul.height() > _this.height()){
              for(i=0;i<opts.line;i++){
                  $ul.find("li").first().before($ul.find("li").last());    
                  }                                             
                $ul.css("margin-top",0);
            }
        });
        
        //触摸结束
        _this.on(&#39;touchend&#39;,function(ev){
            ev.preventDefault();
            if($ul.height() > _this.height()){
              marquee(_this, speed);
            }
        });        
    });
  }
})(Zepto);
登入後複製

我相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

H5表單驗證有哪些方法

spring mvc+localResizeIMG實作H5端圖片壓縮上傳

canvas的繪圖api使用詳解

#

以上是zepto實現行動端無縫向上下滾動的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!