用jquery來實作類別似“網易新聞”橫向標題滑動的移動端頁面

來源:互聯網
上載者:User

標籤:

HTML:

<div id="navbar">                <div id=‘navbar_content‘ style="left:0px;">                    <div class="channel active">                        <span>shouye</span>                    </div>                    <div class="channel">                        <span>shouyeshouye</span>                    </div>                    <div class="channel">                        <span>shouyeshouyeshouyeshouyes</span>                    </div>                    <div class="channel">                        <span>shouye</span>                    </div>                    <div class="channel">                        <span>shouye</span>                    </div>                    <div class="channel">                        <span>shouye</span>                    </div>                    <div class="channel">                        <span>shouye</span>                    </div>                    <div class="channel">                        <span>shouye</span>                    </div>                    <div class="channel">                        <span>shouye</span>                    </div>                </div>            </div>        </div>

CSS:

#navbar{    background: rgba(247,247,247,0.95);    overflow: hidden;    position: relative;    text-align: center;    font-size: 1em;    padding-left: 5px;    line-height: 28px;}#navbar_content{    font-size: 1em;    text-align: left;    white-space: nowrap;    position: relative;}.channel{    vertical-align: top;    display: inline-block;    padding: 0 5px;    color: rgba(0,0,0,0.6);    font-weight: normal;    line-height: 26px;    border-bottom: 2px solid transparent;}.channel.active {    color: #1c88cd;    border-color: #1c88cd;}

JS:

 $(function(){        console.log($(‘#navbar‘).width())        var total_width=0;        var div_arr=$(‘.channel‘);        for(var i=0,len=div_arr.length;i<len;i++){            total_width+=div_arr[i].offsetWidth;        }        console.log(total_width);    var x,old_left;    $(‘#navbar_content‘)        .live(‘touchstart‘, function(e) {            console.log(e.originalEvent.pageX)            x = e.originalEvent.targetTouches[0].pageX // anchor point            old_left = parseInt(e.currentTarget.style.left);        })        .live(‘touchmove‘, function(e) {            var change = e.originalEvent.targetTouches[0].pageX - x;            e.currentTarget.style.left = (old_left+change)+"px";        })        .live(‘touchend‘, function(e) {            var left = parseInt(e.currentTarget.style.left);            var new_left;            var window_width=document.body.scrollWidth;            if (left < parseInt("-"+total_width)+$(‘#navbar‘).width()+100) {                new_left = "-"+(total_width-$(‘#navbar‘).width()+100);            }else if (left > 0) {                new_left = ‘0‘            }             $(e.currentTarget).animate({left: new_left}, 500);            e.currentTarget.style.left = new_left;                    });    });

 

用jquery來實作類別似“網易新聞”橫向標題滑動的移動端頁面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.