主頁 > 知識庫 > 基于zepto的插件之移動端無縫向上滾動并上下觸摸滑動實例代碼

基于zepto的插件之移動端無縫向上滾動并上下觸摸滑動實例代碼

熱門標(biāo)簽:電話機(jī)器人如何 貸款電銷人工和機(jī)器人哪個好 電視購物電銷外呼系統(tǒng) 飛亞外呼系統(tǒng) 西寧智能外呼系統(tǒng)加盟 聯(lián)通400電話申請 百應(yīng)電銷機(jī)器人產(chǎn)業(yè) 杭州營銷電銷機(jī)器人供應(yīng)商 高德地圖標(biāo)注賓館位置

該插件乃本博客作者所寫,目的在于提升作者的js能力,也給一些js菜鳥在使用插件時提供一些便利,老鳥就悠然地飛過吧。

公司的移動端項目是基于zepto的,有一個頁面要求文字能夠無縫地不停向上滾動,但查了網(wǎng)上的資料,大多都是基于jquery的,雖然稍作修改就可以用于移動端,但不能實現(xiàn)觸摸上下翻滾。所以就去了zepto的官網(wǎng)查看其API,卻發(fā)現(xiàn)如果要使用zepto的swipe()方法,需要引用其已經(jīng)封裝好的touch.js文件,我就趕緊引用了這個js文件,可在實際測試中,官網(wǎng)給出的touch.js文件不能適用于swipe()方法,于是,一頭霧水,繼續(xù)查資料,由于網(wǎng)上關(guān)于zepto方面的東西較少,所以,也沒有找出其不能適用于swipe()方法的原因。后來,不經(jīng)意間發(fā)現(xiàn)由百度云Clouda團(tuán)隊開發(fā)的一個touch.js(目前,該js也是由這個團(tuán)隊在維護(hù)),在這個js環(huán)境下居然能使用swipe()方法,于是,趕緊拿來測試。結(jié)果很OK哇!這里要著重感謝百度云Clouda團(tuán)隊,你們真牛!!!  在這里要注意,zepto本身是沒有animate()方法的,它是將這個方法封裝成了一個fx.js(去官網(wǎng)下載),所以在使用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>無標(biāo)題文檔</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>
<div 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>
</div>
<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, //數(shù)值越大,速度越慢
        line:10    //觸摸翻滾的條數(shù)
    });
    })
</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){
    //默認(rèn)配置
    var defaults = {
        speed:40,  //滾動速度,值越大速度越慢
        line:1     //滾動的行數(shù)
    };
    
    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('swipedown', 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);
            }
        });
        
        //觸摸結(jié)束
        _this.on('touchend',function(ev){
            ev.preventDefault();
            if($ul.height() > _this.height()){
              marquee(_this, speed);
            }
        });        
    });
  }
})(Zepto);

DEMO下載:基于zepto的插件之移動端無縫向上滾動并上下觸摸滑動

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:煙臺 玉溪 安慶 撫州 邯鄲 晉中 牡丹江 內(nèi)蒙古

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于zepto的插件之移動端無縫向上滾動并上下觸摸滑動實例代碼》,本文關(guān)鍵詞  基于,zepto,的,插件,之,移動,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于zepto的插件之移動端無縫向上滾動并上下觸摸滑動實例代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于基于zepto的插件之移動端無縫向上滾動并上下觸摸滑動實例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章