一個簡單的Tab平滑切換特效jquery代碼,沒有做最大效率最佳化

來源:互聯網
上載者:User

注意事項:

div格式應該如下:

<div id="slides"><div>xxxxxxx</div><div>xxxxxxx</div><div>xxxxxxx</div></div>

使用方式:

slides._init($('#slides'));

代碼如下:

/*  SlidesJS 1.0  (c) 2013 by 金色豬子(longx1001@qq.com)  Updated: 2013-9-28 16:23:58  Apache License: http://www.apache.org/licenses/LICENSE-2.0*/var slides = {    _obj : new Object(),//初始化#slides    _objpage : new Object(),//初始化分頁代碼    _objchilds : new Object(),//初始化#slides的子項目div    _current : 0,//初始化當前分頁索引    _count : 0,//初始化#slides的子項目數量    _init: function(obj){        slides._obj = obj;        slides._objchilds = slides._obj.children('div');        slides._count = slides._objchilds.length;        var _wid = slides._obj.width();        slides._objchilds.each(function(){            $(this).css({float:'left',width:_wid,overflow:'hidden'});        });        slides._obj.css({width:_wid*slides._count, height:slides._objchilds.eq(0).height(), overflow:'hidden'});                slides._objpage = $('<div id="slides-page"></div>');        var pagecontent = '';        pagecontent += '<a class="slidesjs-previous slidesjs-navigation active" href="#" title="上一題" style="margin-left: 30%;">上一題</a>';        pagecontent += '<ul class="slidesjs-pagination">';        for(var i=1; i<=slides._count; i++){            pagecontent += '<li class="slidesjs-pagination-item"><a href="#" data-slidesjs-item="'+i+'" class="'+(i===1?'active':'')+'">'+i+'</a></li>';        }        pagecontent += '</ul>';        pagecontent += '<a class="slidesjs-next slidesjs-navigation" href="#" title="下一題">下一題</a>';        slides._objpage.append(pagecontent);        slides._obj.after(slides._objpage);                $('.slidesjs-previous').click(function(){            slides._pre();return false;        });        $('.slidesjs-next').click(function(){            slides._next();return false;        });        $('.slidesjs-pagination-item').click(function(){            slides._go($(this).index());return false;        });    },        _next: function(){        if(slides._current!==slides._count-1){            ++slides._current;            slides._obj.css('height',slides._objchilds.eq(slides._current).height()+'px');            var _objleft = Number(slides._obj.css('marginLeft').replace('px',''));            slides._obj.animate({ marginLeft: (_objleft-960)+'px' }, 'slow');            slides._checkcurrent();        }    },        _pre: function(){        if(slides._current!==0){            --slides._current;            slides._obj.css('height',slides._objchilds.eq(slides._current).height()+'px');            var _objleft = Number(slides._obj.css('marginLeft').replace('px',''));            slides._obj.animate({ marginLeft: (_objleft+960)+'px' }, 'slow');            slides._checkcurrent();        }    },        _go: function(indexs){        slides._obj.css('height',slides._objchilds.eq(indexs).height()+'px');        var _objleft = Number(slides._obj.css('marginLeft').replace('px',''));        slides._obj.css({ marginLeft: (_objleft-(960*(indexs-slides._current)))+'px' });        slides._current = indexs;        slides._checkcurrent();    },        _checkcurrent: function(){        $('body,html').animate({scrollTop:0},200);        slides._objpage.children('ul').children().each(function(i){            if(i===slides._current){                $(this).children('a').eq(0).addClass('active');            }else{                $(this).children('a').eq(0).removeClass('active');            }        });        if (slides._current === 0) {            $('.slidesjs-previous').addClass('active');        } else {            $('.slidesjs-previous').removeClass('active');        }        if (slides._current === slides._count-1) {            $('.slidesjs-next').addClass('active');        } else {            $('.slidesjs-next').removeClass('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.