簡單三步建立超炫jQuery滑動代碼

簡單三步建立超炫jQuery滑動代碼

網上有不少不少這樣的代碼,這個只是比較通用的一種。javascript

  • 自動獲取圖片數量,數字顯示
  • 支持鼠標懸停時中止滾動
  • 滾動圖片能夠設置跳轉連接

 

效果圖:css

示例圖片

第一步 —— 建立要顯示的HTML代碼以下:html

  
  
  
  
  1. <div class="folio_block"> 
  2.          
  3.         <div class="main_view"> 
  4.             <div class="window">     
  5.                 <div class="p_w_picpath_reel"> 
  6.                     <a href="http://www.google.com/ncr"><img border="0" height="286" width="790"  src="reel_1.jpg" alt="111111111111111" /></a> 
  7.                     <a href="http://www.google.com/ncr"><img border="0" height="286" width="790"  src="reel_2.jpg" alt="2222222222222" /></a> 
  8.                     <a href="http://www.google.com/ncr"><img border="0" height="286" width="790"  src="reel_3.jpg" alt="aaaaaaaaaaaa" /></a> 
  9.                     <a href="http://www.google.com/ncr"><img border="0" height="286" width="790"  src="reel_4.jpg" alt="zzzzzzzzz" /></a> 
  10.                 </div> 
  11.             </div> 
  12.             <div class="paging"> 
  13.                 <a href="#" rel="1">1</a> 
  14.                 <a href="#" rel="2">2</a> 
  15.                 <a href="#" rel="3">3</a> 
  16.                 <a href="#" rel="4">4</a> 
  17.             </div> 
  18.         </div> 
  19.     </div> 

------------------------------------------------------------------------------------java

第二步 —— 設置樣式以下:jquery

   
   
   
   
  1. /*--Main Container--*/ 
  2. .main_view { 
  3.     floatleft
  4.     positionrelative
  5. /*--Window/Masking Styles--*/ 
  6. .window { 
  7.     height:286px;   width790px
  8.     overflowhidden/*--Hides anything outside of the set width/height--*/ 
  9.     positionrelative
  10. .p_w_picpath_reel { 
  11.     positionabsolute
  12.     top: 0; left: 0
  13. .p_w_picpath_reel img {floatleft;} 
  14.  
  15. /*--Paging Styles--*/ 
  16. .paging { 
  17.     positionabsolute
  18.     bottom: 40px; right: -7px
  19.     width178pxheight:47px
  20.     z-index100/*--Assures the paging stays on the top layer--*/ 
  21.     text-aligncenter
  22.     line-height40px
  23.     backgroundurl(http://www.sohtanaka.com/web-design/examples/p_w_picpath-slider/paging_bg2.png) no-repeat
  24.     displaynone/*--Hidden by default, will be later shown with jQuery--*/ 
  25. .paging a { 
  26.     padding5px
  27.     text-decorationnone
  28.     color#cccccc
  29. .paging a.active { 
  30.     color:green
  31.     font-weightbold
  32.     border1px solid #610000
  33.     -moz-border-radius: 3px
  34.     -khtml-border-radius: 3px
  35.     -webkit-border-radius: 3px
  36. .paging a:hover {font-weightbold;} 


====================================================================================web

第三步 —— 將如下腳本放入頁面:ajax

   
   
   
   
  1. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> 
  2. <script type="text/javascript"
  3.  
  4. $(document).ready(function() { 
  5.  
  6.     //Set Default State of each portfolio piece 
  7.     $(".paging").show(); 
  8.     $(".paging a:first").addClass("active"); 
  9.          
  10.     //Get size of p_w_picpaths, how many there are, then determin the size of the p_w_picpath reel. 
  11.     var p_w_picpathWidth = $(".window").width(); 
  12.     var p_w_picpathSum = $(".p_w_picpath_reel img").size(); 
  13.     var p_w_picpathReelWidth = p_w_picpathWidth * p_w_picpathSum; 
  14.      
  15.     //Adjust the p_w_picpath reel to its new size 
  16.     $(".p_w_picpath_reel").css({'width' : p_w_picpathReelWidth}); 
  17.      
  18.     //Paging + Slider Function 
  19.     rotate = function(){     
  20.         var triggerID = $active.attr("rel") - 1; //Get number of times to slide 
  21.         var p_w_picpath_reelPosition = triggerID * p_w_picpathWidth; //Determines the distance the p_w_picpath reel needs to slide 
  22.  
  23.         $(".paging a").removeClass('active'); //Remove all active class 
  24.         $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function) 
  25.          
  26.         //Slider Animation 
  27.         $(".p_w_picpath_reel").animate({  
  28.             left: -p_w_picpath_reelPosition 
  29.         }, 500 ); 
  30.          
  31.     };  
  32.      
  33.     //Rotation + Timing Event 
  34.     rotateSwitch = function(){       
  35.         play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds 
  36.             $active = $('.paging a.active').next(); 
  37.             if ( $active.length === 0) { //If paging reaches the end... 
  38.                 $active = $('.paging a:first'); //go back to first 
  39.             } 
  40.             rotate(); //Trigger the paging and slider function 
  41.         }, 1000); //Timer speed in milliseconds (3 seconds) 
  42.     }; 
  43.      
  44.     rotateSwitch(); //Run function on launch 
  45.      
  46.     //On Hover 
  47.     $(".p_w_picpath_reel a").hover(function() { 
  48.         clearInterval(play); //Stop the rotation 
  49.     }, function() { 
  50.         rotateSwitch(); //Resume rotation 
  51.     });  
  52.      
  53.     //On Click 
  54.     $(".paging a").click(function() {    
  55.         $active = $(this); //Activate the clicked paging 
  56.         //Reset Timer 
  57.         clearInterval(play); //Stop the rotation 
  58.         rotate(); //Trigger rotation immediately 
  59.         rotateSwitch(); // Resume rotation 
  60.         return false//Prevent browser jump to link anchor 
  61.     });  
  62.      
  63. }); 
  64. </script> 

示例地址:http://www.sohtanaka.com/web-design/examples/p_w_picpath-slider/
相關文章
相關標籤/搜索