基於jQuery的左右滾動實現代碼

來源:互聯網
上載者:User

兩個div。
一個div為固定寬度 。在次div中內嵌一個div ,這個為資料列。 複製代碼 代碼如下:<div class=”box”>
<div class=”box-li”>
<ul>
<li>資料2</li>
<li>資料2</li>
<li>資料2</li>
<li>資料2</li>
<li>資料2</li>
<li>資料2</li>
<ul>
</div>
<div>
<span id=”next”>向右移動</span>
<span id=”pre”>向左移動</span>
樣式表
.box{
float: left;
height: 93px;
width: 560px;
left:0px;
white-space:nowrap;
overflow:hidden;
position:relative /* 不加次屬性在ie7中無法隱藏內容*/
}
.box-li{
float: left;
height: 90px;
left:0px;
position:relative;
white-space:nowrap;
clear: both;
}
.box-li ul{
width:100000px; /* 不加次資料IE中資料會自動換行*/
white-space:nowrap;
}
.box-li li{
margin-left:0px;
margin-right:0px;
float: left;
text-align:center;
width: 92px;
}
$(function () {
var $cur = 1; //初始化顯示的版面
var $i= 6; //每版顯示數
var $len = $('.box-li>ul>li').length; //計算資料行表總長度(個數)
var $pagecount = Math.ceil($len / $i); //計算展示版面數量
var $showbox = $('.box');
var $w = $('.box').width(); //取得展示區外圍寬度
var $pre = $('#pre');
var $next = $('#next');
//向前滾動
$pre.click(function () {
if (!$showbox.is(':animated')) { //判斷展示區是否動畫
if ($cur == 1) { //在第一個版面時,再向前滾動無動作 }
else {
$showbox.animate({
left: '+=' + $w
}, 600); //改變left值,切換顯示版面
$cur--; //版面累減
}
}
});
//向後滾動
$next.click(function () {
if (!$showbox.is(':animated')) { //判斷展示區是否動畫
if ($cur == $pagecount) { //在最後一個版面時,再向後滾動無動作 }
else {
$showbox.animate({
left: '-=' + $w
}, 600); //改變left值,切換顯示版面
$cur++; //版面數累加
}
}
});
});

聯繫我們

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