Jquery焦點圖執行個體

來源:互聯網
上載者:User

標籤:style   http   io   ar   color   os   sp   java   for   

對於很多建站的朋友來講,焦點圖並不陌生,一般的企業站,門戶站都會用到焦點圖。我們平時在寫html代碼的時候,很多人為了省時省力,對於焦點圖都是在網上下載一些人家寫好的代碼,直接套上去即可,很多時候我自己也是這樣來的。因為一個焦點圖有html布局,css樣式調試和最關鍵的 javascript語句的編寫。這些都是一個很麻煩的過程。

今天我就以執行個體講解,手把手教你如何寫Jquery焦點圖。Jquery是js封裝的架構,讓js的文法變得簡單,很多知名網站都在用這個架構。這裡星仔為了文法的簡單,讓大部分網友能看懂,也引用Jquery架構。

長話短說,我直接貼出原始碼,和對應的注釋。如果想要體驗效果可以點擊文章後而給的樣本串連。

代碼開始------

 <!DOCTYPE html>

<html>

<head>

<title>focus code powered by boyxing.com</title>

<script type="text/javascript" src="http://www.boyxing.com/skin/default/js/jquery.js"></script& gt;<!--引用Jquery架構-->

<!--css定義,可以根據需要自己定義-->

<style type="text/css">

body,ul,li{ margin:0; padding:0}

ul,li{ list-style:none;}

.focus{ margin:0 auto; height:320px; width:980px; overflow:hidden; position:relative; padding:0;}

.focus .bigpic{ position:absolute;padding:0;}

.focus .bigpic li{ width:980px;float:left;padding:0;}

.focus .btn{ overflow:hidden; height:30px;position:absolute; bottom:3px; left:50%; margin-left:-100px;}

.focus .btn li{ float:left; margin:0 10px; padding:5px; cursor:pointer; background: #fff;border:1px #900 solid;border-radius:12px; height:12px; width:12px; overflow:hidden; text-align:center; line-height:12px;opacity:0.6; float:left;}

.focus .btn li.on{ background: #990000; color:#FFFFFF;}

</style>

</head>

<body>

 

<!--焦點圖div部分開始-->

<div class="focus">

  <div class="focuscon">

    <ul class="bigpic">

      <li><a href="http://www.boyxing.com/"><img src="http://www.boyxing.com/d/file/fbc60c3991284b6ffdb0aa11a1708c41.jpg" border="0"></a></li>

      <li><a href="http://www.boyxing.com/"><img src="http://www.boyxing.com/d/file/3d41450340fe053008f8437011b7bcf5.jpg" border="0"></a></li>

      <li><a href="http://www.boyxing.com/"><img src="http://www.boyxing.com/d/file/c9a787ef199d47bdb3529bbdecfc989c.jpg" border="0"></a></li>

      <li><a href="http://www.boyxing.com/"><img src="http://www.boyxing.com/d/file/2471eddde718c56879171814c8a52153.jpg" border="0"></a></li><!--焦點圖個數不限-->

    </ul>

  </div>

</div>

<!--焦點圖div部分結束-->

 

</body>

<script type="text/javascript">

$(document).ready(function(){

 

var num=$(".bigpic li").length;//擷取焦點圖的個數

var fwidth=$(".bigpic li").width();//擷取每個焦點圖的寬度

var sec=4000;//時間切換間隔

 

var btn = ‘<ul class="btn"><li class="on">1</li>‘;

var btnend = ‘</ul>‘;

for(i=2;i<=num;i++){btn += ‘<li>‘+i+‘</li>‘;};

btn += btnend;

if(num == 1){btn = null};

$(".focus").append(btn);//自動根據焦點圖個數,添加切換按鈕,如果只有一張圖片則不顯示切換按鈕。

 

$(".bigpic").css("width",fwidth*num);//設定大圖集合的寬度,也就是所有焦點圖寬度的和。

 

$(".btn li").bind("mouseover",function(){

  $(this).addClass("on").siblings().removeClass("on");

  var i=$(".btn li").index(this);var marginL=fwidth*i;

  $(".bigpic").animate({"left":-marginL},500);}

);//滑鼠指向按鈕,焦點圖切換到對應位置,按鈕樣式改變。mouseover是滑鼠經過時,這裡也可以改成click,通過點擊切換焦點圖。

 

picTimer = setInterval(timeset,sec); //指定sec毫秒後執行一次timeset函數。

function timeset(){

  var j = $(".btn li").index($(".on"));//取得 當前焦點圖的位置,即class為on的序號。

  var timew = fwidth*(j+1);

  if(j == num-1){$(".btn li").eq(0).addClass("on").siblings().removeClass("on");$(".bigpic").animate({"left":0},500);}

            else{$(".btn li").eq(j+1).addClass("on").siblings().removeClass("on");$(".bigpic").animate({"left":-timew},500);};

                  };

 

$(".focus").mouseover(function(){clearInterval(picTimer);});

$(".focus").bind("mouseout",function(){picTimer = setInterval(timeset,sec);}

);//當滑鼠指向焦點圖或者是切換按鈕時,定時器清除,即不在執行自動切換,滑鼠離開則恢複自動切換。

 

})

</script>

</html>

------代碼結束-------

 

本文出自:http://www.boyxing.com/article/136.html

Jquery焦點圖執行個體

聯繫我們

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