佳木斯湛栽影视文化发展公司

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

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

熱門標簽:網(wǎng)站建設 智能手機 阿里云 美圖手機 檢查注冊表項 硅谷的囚徒呼叫中心 百度競價點擊價格的計算公式 使用U盤裝系統(tǒng)

該插件乃本博客作者所寫,目的在于提升作者的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)上關于zepto方面的東西較少,所以,也沒有找出其不能適用于swipe()方法的原因。后來,不經(jīng)意間發(fā)現(xiàn)由百度云Clouda團隊開發(fā)的一個touch.js(目前,該js也是由這個團隊在維護),在這個js環(huán)境下居然能使用swipe()方法,于是,趕緊拿來測試。結(jié)果很OK哇!這里要著重感謝百度云Clouda團隊,你們真牛!??!  在這里要注意,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>無標題文檔</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){
    //默認配置
    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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

標簽:懷化 黃山 煙臺 湖北 湘潭 通遼 賀州 山南

巨人網(wǎng)絡通訊聲明:本文標題《基于zepto的插件之移動端無縫向上滾動并上下觸摸滑動實例代碼》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    资中县| 长岛县| 乐清市| 婺源县| 昭平县| 古蔺县| 洪湖市| 赣州市| 蓬莱市| 龙胜| 巴南区| 古田县| 米泉市| 宜丰县| 紫云| 佳木斯市| 双流县| 绥滨县| 疏附县| 铜陵市| 修文县| 香格里拉县| 鲁山县| 迁西县| 长治县| 乌海市| 顺平县| 大石桥市| 乌兰浩特市| 黄大仙区| 高淳县| 慈利县| 四会市| 淮阳县| 牙克石市| 丹凤县| 惠安县| 芦山县| 台安县| 麻栗坡县| 教育|