移動端圖片切換

來源:互聯網
上載者:User

標籤:

<section id="tabPic">            <ul id="picList">                <li><img src="img/1.jpg" /></li>                <li><img src="img/2.jpg" /></li>                <li><img src="img/3.jpg" /></li>                <li><img src="img/4.jpg" /></li>                <li><img src="img/5.jpg" /></li>            </ul>            <section class="picMask">                <p>成都九寨溝景區</p>                <nav>                    <a href="javascript:;" class="active"></a>                    <a href="javascript:;"></a>                    <a href="javascript:;"></a>                    <a href="javascript:;"></a>                    <a href="javascript:;"></a>                </nav>            </section></section> function bind(obj, ev, fn) {     if (obj.addEventListener) {        obj.addEventListener(ev, fn, false);    } else {        obj.attachEvent(‘on‘ + ev, function() {            fn.call(obj);        });    }}bind(document,"touchmove",function(ev){    ev.preventDefault();})function fnTab(){    var oTab=id("tabPic");    var oList=id("picList");//圖片    var aNav=oTab.getElementsByTagName("nav")[0].children;//切換點    var iNow=0;    var iX=0;    var iW=view().w;    var iTimer=0;    var iStartTouchX=0;    var iStartX=0;    bind(oTab,"touchstart",fnStart);    bind(oTab,"touchmove",fnMove);    bind(oTab,"touchend",fnEnd);    auto();    function fnStart(ev){        oList.style.transition="none";        oList.style.WebkitTransition="none";        ev=ev.changedTouches[0];        iStartTouchX=ev.pageX;        iStartX=iX;        clearInterval(iTimer);    }    function fnMove(ev){        ev=ev.changedTouches[0];        var iDis=ev.pageX-iStartTouchX;        iX=iDis+iStartX;        oList.style.WebkitTransform=oList.style.transform="translateX("+iX+"px)";    }    function fnEnd(){        iNow=iX/iW;        iNow=-Math.round(iNow);//用四捨五入判斷圖片是前進還是後退        if(iNow<0){            iNow=0;        }        if(iNow>aNav.length-1){            iNow=aNav.length-1;        }        tab();        auto();    }    function auto(){        iTimer=setInterval(function(){            iNow++;            if(iNow>aNav.length-1){                iNow=aNav.length-1;            }            tab();        },2000);    }    function tab(){        iX=-iNow*iW;        oList.style.transition="0.5s";        oList.style.WebkitTransition="0.5s";        oList.style.WebkitTransform=oList.style.transform="translateX("+iX+"px)";        for(var i=0;i<aNav.length;i++){            removeClass(aNav[i],"active");        }        addClass(aNav[iNow],"active");    }}

 

移動端圖片切換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.