jQueryのシンプルで実践的なカルーセル実装方法

小云云
リリース: 2018-01-10 15:03:13
オリジナル
1054 人が閲覧しました

この記事では主にjqueryを使った簡単で実用的なカルーセルの作成方法を詳しく紹介しますので、興味のある方はぜひ参考にしてください。

WEB 開発は、コンテナ内のアイテムがループで再生および表示され、同時に対応するナビゲーション バー プロンプトが存在する状況でよく使用されます。これは、多くの場所で使用でき、機能が非常に似ているためです。ので、このように書きました。再生機能を共有してください。これには、オンラインで検索してダウンロードできます。

1.以下を別のファイル itemPlayerApp.js として保存します:

//attend: this need jQuery.js support 
var itemPlayerApp={ 
 author:'shenzhenNBA', 
 version:'v1.0', 
 appMaxNum:0, 
 playData:'1xplay', 
 playerID:"", 
 speed:3000, 
 appPlay:function(){  
  var f=this.playData.toLowerCase().split('x'); 
  if(f[1]=='play'){ 
   var i; 
   try{i=parseInt(f[0]);}catch(e){i=0;} 
   if(i>=this.appMaxNum){i=0;}    
   this.appTab(i);   
   this.playData=(++i)+"xplay"; 
  }   
 }, 
 appTab:function(tabIndex){ 
  var k,j; 
  try{k=parseInt(tabIndex);}catch(e){k=0;}   
  for(j=0;j<this.appMaxNum;j++){    
   if(k==j){      
   $(&#39;#itemNav&#39;+j).css({&#39;background-color&#39;:&#39;#333333&#39;,&#39;color&#39;:&#39;#FFFFFF&#39;});     
   $(&#39;#item&#39;+j).show(&#39;fast&#39;);    
   }else{   
   $(&#39;#itemNav&#39;+j).css({&#39;background-color&#39;:&#39;#CCCCCC&#39;,&#39;color&#39;:&#39;#000000&#39;}); 
   $(&#39;#item&#39;+j).hide(&#39;fast&#39;);  
   } 
  }   
 }, 
 appActive:function(){ 
  var _this = this; 
  this.playerID = setInterval(function(){ _this.appPlay(); },this.speed); 
 }, 
 init:function(refContainerId,intervalTime,refWidth,refHeight){  
  var cid = "";  
  var w = 300; 
  var h = 200; 
  if(refContainerId == &#39;undefined&#39; || refContainerId == null || refContainerId == &#39;&#39;){ 
   return; 
  }else{ 
   cid = $.trim(refContainerId); 
  }  
  if(refWidth == &#39;undefined&#39; || refWidth == null || refWidth == &#39;&#39;){ 
   w = 300; 
  }else{ 
   w = parseInt(refWidth); 
  }  
  if(refHeight == &#39;undefined&#39; || refHeight == null || refHeight == &#39;&#39;){ 
   h = 200; 
  }else{ 
   h = parseInt(refHeight); 
  }  
   
  $(&#39;#&#39; + cid).css({"position":"relative",&#39;width&#39;:w,&#39;height&#39;:h,&#39;overflow&#39;:&#39;hidden&#39;}); 
  $(&#39;#&#39; + cid + "NavCon").css({&#39;color&#39;:&#39;#333333&#39;,&#39;height&#39;:&#39;26px&#39;,&#39;position&#39;:&#39;absolute&#39;,&#39;width&#39;:&#39;95%&#39;,&#39;left&#39;:&#39;0&#39;,&#39;bottom&#39;:&#39;3px&#39;,&#39;text-align&#39;:&#39;right&#39;,&#39;display&#39;:&#39;block&#39;}); 
  var t = 0; 
  if(intervalTime == &#39;undefined&#39; || intervalTime == null){ 
   t = 3000; 
  }else{ 
   try{ t = parseInt(intervalTime);}catch(e){ t = 3000;} 
  } 
  this.speed = t; 
  var navList = "#" + cid + "NavCon a"; 
  this.appMaxNum = $(navList).size(); 
  if(0 == this.appMaxNum){ return; } 
  var _this = this; 
  $(navList).each(function(i){ 
   $(this).css({&#39;padding&#39;:&#39;2px 5px&#39;,&#39;margin-right&#39;:&#39;2px&#39;,&#39;background-color&#39;:&#39;#CCCCCC&#39;}); 
   if(i == 0){ 
    $(this).css({&#39;background-color&#39;:&#39;#333333&#39;,&#39;color&#39;:&#39;#FFFFFF&#39;}); 
   }     
   $(this).mouseover(function(){ 
   _this.playData=i+&#39;xstop&#39;; 
   _this.appTab(i);  
   }); 
   $(this).mouseout(function(){ 
   _this.playData=i+&#39;xplay&#39;; 
   _this.appTab(i); 
   }); 
  }); 
 } 
};
ログイン後にコピー

2. 使用方法:

1. 使用する Web ページに jQery ファイルとこの itemPlayerApp.js ファイルを導入します。例:

< ;script language="javascript" src="xpath/itemPlayer.js" >

2. 以下の形式で HTML ファイルを作成します

<p id="containerID">

<p id="containerIDNavCon">
<a id="itemNav0" class="item1" href="#">1</a>
<a id="itemNav1" class="item1" href="#">2</a>
<a id="itemNav2" class="item1" href="#">3</a>
</p>
<p id="containerIDItemCon">
<a id="item0" href="#"><img src="img/pic0.jpg" width="300" height="200"></a>
<a id="item1" href="#"><img src="img/pic1.jpg" width="300" height="200"></a>
<a id="item2" href="#"><img src="img/pic2.jpg" width="300" height="200"></a>
</p>
</p>
ログイン後にコピー

注: できるだけ簡単なので、適切にフォーマットされた HTML を作成する必要があります。主な要件は次のとおりです。色の部分に注意してください。

//A、id =containerIDNavCon と id =containerIDItemCon の接続 A 要素の数は等しい必要があります。 /B、ナビゲーション コンテナの ID 構成は、上記のとおり、メイン コンテナ ID に NavCon を加えたものである必要があります。
//C、ナビゲーション コンテナ内の各要素 A 要素の ID は、itemNav と開始する増加する番号シーケンスで構成されます。上の緑色の部分に示すように 0 の場合、
//D、表示項目コンテナーの各 A 要素の ID は、itemNav と 0 で始まる増加する数値シーケンス (上の紫色の部分など) で構成されます。

3. WEB ページの読み込みイベント onload で、カルーセル関数を初期化して有効にします。例:

window.onload=function(){
itemPlayerApp.init('containerID' ,3000,300,200);
itemPlayerApp。 active();
}

3. 次の例

すべてのファイルがフォルダーに配置されている場合、

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>TEST</title> 
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> 
<script language="javascript" type="text/javascript" src="itemPlayerApp.js"></script> 
<style type="text/css"> 
*{font-family:"宋体",verdana,arial; font-size:12px;color:#000000;} 
#playerBox{font-family:"宋体",verdana,arial; font-size:12px;color:#000000;} 
</style> 
</head> 
<body> 
<p id="playerBox" class="columnLeft01 box02"> 
<p id="playerBoxNavCon"> 
<a id="itemNav0" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1</a> 
<a id="itemNav1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >2</a> 
<a id="itemNav2" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >3</a> 
</p> 
<p id="playerBoxItemCon"> 
<a id="item0" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif" width="100%" height="200" border="0"></a> 
<a id="item1" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="http://csdnimg.cn/www/images/csdnindex_logo.gif" width="100%" height="200" border="0"></a> 
<a id="item2" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="http://avatar.csdn.net/5/1/9/1_shenzhennba.jpg" width="100%" height="200" border="0"></a> 
</p> 
</p> 
<p> </p> 
<p>项目循环轮播功能</p> 
<script language="javascript" type="text/javascript"> 
window.onload=function(){ 
itemPlayerApp.init('playerBox',3000,300,200); 
itemPlayerApp.appActive(); 
} 
</script> 
</body> 
</html>
ログイン後にコピー
ヒント: jQuery.js ファイルをオンラインでダウンロードしてください。

必要なときにだけ使用してください。

関連する推奨事項:


H5 を使用してカルーセルを実装するサンプル チュートリアル (タッチ スクリーン バージョン)

H5 を使用してタッチ スクリーン カルーセルを実装する方法を詳しく紹介します

ステップごとに説明しますjQyery はカルーセルを実装します

以上がjQueryのシンプルで実践的なカルーセル実装方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!