最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery插件實現(xiàn)無縫滾動特效

 更新時間:2015年11月24日 14:28:26   作者:similar  
今天給大家分享一款頁面無縫滾動的jquery插件,支持上下左右,淡入淡出,滾動時間設(shè)置,動畫時間設(shè)置,感興趣的小伙伴們可以參考一下

首先來看下html骨架,如下:

<div class="box">
    <ul>
      <li>111</li>
      <li>222</li>
      <li>333</li>
    </ul>
</div>

結(jié)構(gòu)簡單明了,沒什么說的。

講下實現(xiàn)原理:

  div box是最外層盒子,給它指定的寬高,記得給box添加一個 overflow:hidden (超出的內(nèi)容隱藏)樣式,因為滾動肯定是會超出box的。

  我們通過js控制 ul 標(biāo)簽的margin 來實現(xiàn)滾動。橫向滾動則是控制 margin-left ; 縱向滾動則是控制  margin-top;

  初始狀態(tài)時,我們還要進行條件判斷,判斷是否進行滾動。即:當(dāng) ul 長度小于 外層 box 長度時不進行滾動,反之則進行滾動。

  ul 的長度是通過計算得來的,即: ul 里面單個 li 的長度乘以 li 的個數(shù)。 ul_width = li_width * li_num;

  之所以能實現(xiàn)無縫滾動,是因為每次滾動的長度剛好大于單個 li 的長度時,我們就將ul的第一個 li 移動到ul的最后,周而復(fù)始,無限循環(huán)(關(guān)于這一點,你可以先不設(shè)置 overflow:hidden 來查看)。

插件的實現(xiàn)代碼:

(function ($) {
 $.fn.Scroll = function (options) {
  //將當(dāng)前上下文對象存入root
  var root = this;

  //默認(rèn)配置
  var settings = {
   speed: 40,  //滾動速度,值越大速度越慢
   direction: "x" //滾動方向("x"或者"y" [x橫向;y縱向])
  };

  //不為空,則合并參數(shù)
  if (options)
   $.extend(settings, options);


  var timer = [];  //計時器
  var marquee;  //滾動器(函數(shù))
  var isRoll;   //判斷是否滾動(函數(shù))

  var _ul = $("> ul", root);   //ul標(biāo)簽
  var _li = $("> ul > li", root);  //li標(biāo)簽(集合)

  var li_num = _li.length; //li標(biāo)簽個數(shù)
  var li_first = _li.first(); //獲取單個li標(biāo)簽


  //判斷為縱向還是橫向,并進行相應(yīng)操作
  if (settings.direction == "x") {
       
       var li_w = li_first.outerWidth(true); //單個li標(biāo)簽的寬度
       var ul_w = li_w * li_num;        //ul標(biāo)簽的寬度

   _ul.css({ width: ul_w }); //設(shè)置ul寬度

   marquee = function () {
    _ul.animate({ marginLeft: "-=1" }, 0, function () {
     var _mleft = Math.abs(parseInt($(this).css("margin-left")));
     if (_mleft > li_w) { //滾動長度一旦大于單個li的長度
      $("> li:first", $(this)).appendTo($(this)); //就把第一個li移到最后
      $(this).css("margin-left", 0); //滾動長度歸0
     }
    });
   };
   //ul長度小于box長度時則不滾動,反之滾動
   isRoll = function (t) {
    if (ul_w <= root.width())
     clearInterval(t);
    else
     marquee();
   }
  }
  else {
       var li_h = li_first.outerHeight(true); //單個li標(biāo)簽的高度 
       var ul_h = li_h * li_num; //ul標(biāo)簽的高度

   _ul.css({ height: ul_h }); //設(shè)置ul高度

   marquee = function () {
    _ul.animate({ marginTop: "-=1" }, 0, function () {
     var _mtop = Math.abs(parseInt($(this).css("margin-top"))); //取絕對值
     if (_mtop > li_h) { 
      $("> li:first", $(this)).appendTo($(this));
      $(this).css("margin-top", 0);
     }
    });
   };
   //ul長度小于box長度時則不滾動,反之滾動
   isRoll = function (t) {
    if (ul_h <= root.height())
     clearInterval(t);
    else
     marquee();
   }
  }

  //遵循鏈?zhǔn)皆瓌t,并進行初始化
  return root.each(function (i) {
   //超出內(nèi)容隱藏,防止用戶沒寫overflow樣式
   $(this).css({ overflow: "hidden" });

   timer[i] = setInterval(function () {
    isRoll(timer[i]);
   }, settings.speed);

   //鼠標(biāo)進入停止?jié)L動,離開繼續(xù)滾動
   $(this).hover(function () {
    clearInterval(timer[i]);
   }, function () {
    timer[i] = setInterval(function () {
     isRoll(timer[i]);
    }, settings.speed);
   });

  });

 };
})(jQuery);

基本的代碼說明注釋寫的很清楚了,下面對個別知識點作下講解:

1) 、var timer=[];  之前timer并不是聲明為數(shù)組類型的,是在我寫demo的時候,由于頁面同時存在兩個無縫滾動的應(yīng)用(為了演示橫向和縱向), 出現(xiàn)了bug。

因為他們兩個共用了一個timer計時器,當(dāng)鼠標(biāo)進入其中一個時,另一個的timer也被clear了。之后修改代碼將其聲明為數(shù)組對象,再通過root.each()就實現(xiàn)了每個插件應(yīng)用都有自己獨立的timer計時器,互不干擾。也就是說此插件支持頁面同時存在多個無縫滾動應(yīng)用。

2) 、outerWidth() /outerHeight()函數(shù)。 這個函數(shù)比較強大,它獲取的不僅僅是元素的寬度/高度,實際上 outerWidth()=width+borderLeft+borderRight+marginLeft+marinRight;當(dāng)它設(shè)置為true后,即:outerWidth(true),它也會將padding計算進來:outerWidth()=width+borderLeft+borderRight+marginLeft+marinRight+paddingLeft+paddingRight;

下面給出DEMO代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
 *{ margin:0; padding:0;}
 ul,ul li{ list-style:none;} 
 .wrap{ width:1000px; margin:50px auto;} 
 .box1,.box2,.box3{ overflow:hidden; float:left;border:1px solid gray;} 
 .box1{ width:200px; height:450px;}
 .box1 ul li{ width:200px; height:100px;} 
 .box2,.box3{ width:450px;height:150px; margin:40px;}
 .box2 ul li,.box3 ul li{ width:100px; height:150px; float:left;}
 
</style>
</head>

<body>
<div class="wrap">

 <div class="box1">
  <ul>
   <li>111縱向</li>
   <li>222縱向</li>
   <li>333縱向</li>
   <li>444縱向</li>
   <li>555縱向</li>
   <li>666縱向</li>
  </ul>
 </div>

 <div class="box2">
  <ul>
   <li>111橫向</li>
   <li>222橫向</li>
   <li>333橫向</li>
   <li>444橫向</li>
   <li>555橫向</li>
   <li>666橫向</li>
  </ul>
 </div> 
 
 <div class="box3"> 
  <ul>
   <li>ul長度小于box長度,不滾動</li>
   <li>222橫向</li>
   <li>333橫向</li>   
  </ul>
 </div> 
</div>

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.similar.scroll.js"></script>
<script type="text/javascript">
 $(function () {
  //奇數(shù)背景設(shè)置為灰色
  $('.box1 li:even,.box2 li:even,.box3 li:even').css({ backgroundColor: "gray" });

  $(".box1").Scroll({ direction: "y" }); //設(shè)置為縱向滾動
  $(".box2").Scroll(); //默認(rèn)橫向滾動
  $(".box3").Scroll();
 });
</script>
</body>
</html>

效果圖片:

以上就是jQuery插件實現(xiàn)無縫滾動特效,效果實現(xiàn)有些簡陋,不是很美觀,但是方法是正確的,希望大家自己在此基礎(chǔ)上進行美化。

相關(guān)文章

最新評論

江口县| 成都市| 桂林市| 凤台县| 和平县| 乌鲁木齐县| 同江市| 大宁县| 安图县| 错那县| 松原市| 巢湖市| 莲花县| 兰考县| 华容县| 郸城县| 三原县| 故城县| 肃南| 枞阳县| 石城县| 平阳县| 来凤县| 马尔康县| 原平市| 民丰县| 宜兰市| 和政县| 波密县| 阳江市| 龙泉市| 滨海县| 佛学| 精河县| 揭阳市| 梁平县| 巴彦县| 昌图县| 黔江区| 象山县| 马山县|