jQuery實現圖片文字無縫滾動效果

來源:互聯網
上載者:User

你知道 jQuery,你也知道圖片滾動的效果,但是你知道啥叫無縫滾動不?無縫滾動,就是無縫的滾動!換句話說,無縫滾動就是,圖片或者文字下一張下一張地滾動,當滾動到屁股的時候就看到頭了,反過來,當滾動到頭的時候又能看到屁股了。還是看 DEMO 吧,或許更好理解一些。如你所見,當作為一排圖片展示的時候,無縫滾動可以讓人有無盡的感覺,頁面一下子就充實了。

因為我也不是刻意去寫這個 jQuery 的無縫滾動,所以 JS 也沒封裝過,只提供了幾個基本的參數作修改,只能僅作參考。

 代碼如下 複製代碼

<!DOCTYPE HTML>
<html lang="en-US">
<head>
 <meta charset="UTF-8">
 <script type="text/javascript" src="jquery.min.js"></script>
 <script type="text/javascript">
 $(function() {
  //組態變數
  var config = {
   showNum : 3, //設定滾動的顯示個數
   autoScroll : false, //是否自動滾動,預設為 false
   autoScrollInterval : 3000 //自動滾動間隔,預設為 3000 毫秒,autoScroll = true 時才有效
  }
  
  var scrollUlWidth = $('.scroll_ul li').outerWidth(true), //單個 li 的寬度
   scrollUlLeft = 0, //.scroll_ul 初使化時的 left 值為 0
   prevAllow = true, //為了防止連續點擊上一頁按鈕
   nextAllow = true; //為了防止連續點擊下一頁按鈕
   
  //計算父容量限寬
  $('.scroll_list').width(config.showNum * scrollUlWidth);

  //點擊上一頁
  $('#prev').click(function() {
   if (prevAllow) {
    prevAllow = false;
    scrollUlLeft = scrollUlLeft - scrollUlWidth;
    $('.scroll_ul').css('left', scrollUlLeft);
    //複製最後一個 li 並插入到 ul 的最前面
    $('.scroll_ul li:last').clone().prependTo('.scroll_ul');
    //刪除最後一個 li
    $('.scroll_ul li:last').remove();
    $('.scroll_ul').animate({
     left : scrollUlLeft + scrollUlWidth
    }, 300, function() {
     scrollUlLeft = parseInt($('.scroll_ul').css('left'), 10);
     prevAllow = true;
    })
   }
  });
  
  //點擊下一頁
  $('#next').click(function() {
   if (nextAllow) {
    nextAllow = false;
    $('.scroll_ul').animate({
     left : scrollUlLeft - scrollUlWidth
    }, 300, function() {
     scrollUlLeft = parseInt($('.scroll_ul').css('left'), 10);
     scrollUlLeft = scrollUlLeft + scrollUlWidth;
     $('.scroll_ul').css('left', scrollUlLeft);
     //複製第一個 li 並插入到 ul 的最後面
     $('.scroll_ul li:first').clone().appendTo('.scroll_ul');
     //刪除第一個 li
     $('.scroll_ul li:first').remove();
     nextAllow = true;
    })
   }
  });
  
  //自動滾動
  if (config.autoScroll) {
   setInterval(function() {
    $('#next').trigger('click');
   }, config.autoScrollInterval)
  }
 })
 </script>
 <style type="text/css">
 *{margin:0;padding:0;}
 .scroll_list{overflow:hidden;position:relative;}
 .scroll_ul{width:9999px;position:relative;overflow:hidden;clear:both;*zoom:1;}
 .scroll_ul li{width:100px;height:100px;background:#ccc;margin:5px;line-height:100px;text-align:center;list-style:none;float:left;}
 </style>
 <title>無縫滾動</title>
</head>
<body>
 <div class="scroll_area">
  <div class="scroll_list">
   <ul class="scroll_ul">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
   </ul>
  </div>
  <a id="prev" class="btn_icon" href="javascript:;">上一個</a>
  <a id="next" class="btn_icon" href="javascript:;">下一個</a>
 </div>
</body>
</html>


基本原理和使用方法都寫在注釋裡了,我也就不多作解釋了。最後說一句,google、百度上搜一下就知道,已經有大把大把寫好的橫的豎的無縫的滾動,總有一款適合你,但是我覺得,只有自己親自寫的才最適合自己的需要,所以就有了以上代碼。

聯繫我們

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