jQuery如何實(shí)現(xiàn)自動(dòng)或手動(dòng)圖片切換

這篇文章主要介紹了jQuery如何實(shí)現(xiàn)自動(dòng)或手動(dòng)圖片切換,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)公司是網(wǎng)站建設(shè)專家,致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,專業(yè)領(lǐng)域包括網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、電商網(wǎng)站制作開發(fā)、重慶小程序開發(fā)公司、微信營銷、系統(tǒng)平臺(tái)開發(fā),與其他網(wǎng)站設(shè)計(jì)及系統(tǒng)開發(fā)公司不同,我們的整合解決方案結(jié)合了恒基網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,且不斷評估并優(yōu)化我們的方案,為客戶提供全方位的互聯(lián)網(wǎng)品牌整合方案!

    

主頁核心代碼(Default.aspx):

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>jQuery實(shí)現(xiàn)圖片切換</title>  
    <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> 
    <script type="text/javascript" src="js/tupianqiehuan.js"></script> 
    <link rel="stylesheet" type="text/css" href="css/tupianqiehuan.css"> 
</head> 
<body> 
    <div class="wrapper"> 
        <h2>jquer實(shí)現(xiàn)圖片切換</h2> 
        <div id="focus"> 
            <ul> 
                <li><a href="#" target="_blank"><img src="images/01.jpg" alt="" /></a></li> 
                <li><a href="#" target="_blank"><img src="images/02.jpg" alt="" /></a></li> 
                <li><a href="#" target="_blank"><img src="images/03.jpg" alt="" /></a></li> 
                <li><a href="#" target="_blank"><img src="images/04.jpg" alt="" /></a></li><span >                 </ul> 
        </div> 
    </div> 
</body> 
</html>

CSS代碼(tupianqiehuan.css):  

*{margin:0;padding:0;} 
body{font-size:12px;color:#222;font-family:Verdana,Arial,Helvetica,sans-serif;background:#f0f0f0;} 
.clearfix:after{content: ".";display: block;height: 0;clear: both;visibility: hidden;} 
.clearfix{zoom:1;} 
ul,li{list-style:none;} 
img{border:0;} 
.wrapper{width:800px;margin:0 auto;padding-bottom:50px;} 
h2{height:50px;line-height:50px;font-size:22px;font-weight:normal;font-family:"Microsoft YaHei",SimHei;margin-bottom:20px;} 
/* focus */ 
#focus{width:800px;height:280px;overflow:hidden;position:relative;} 
#focus ul{height:380px;position:absolute;} 
#focus ul li{float:left;width:800px;height:280px;overflow:hidden;position:relative;background:#000;} 
#focus ul li div{position:absolute;overflow:hidden;} 
#focus .btnBg{position:absolute;width:800px;height:20px;left:0;bottom:0;background:#000;} 
#focus .btn{position:absolute;width:780px;height:10px;padding:5px 10px;right:0;bottom:0;text-align:right;} 
#focus .btn span{display:inline-block;_display:inline;_zoom:1;width:25px;height:10px;_font-size:0;margin-left:5px;cursor:pointer;background:#fff;} 
#focus .btn span.on{background:#fff;} 
#focus .preNext{width:45px;height:100px;position:absolute;top:90px;background:url(/upload/otherpic59/63007.png) no-repeat 0 0;cursor:pointer;} 
#focus .pre{left:0;} 
#focus .next{right:0;background-position:right top;}

JS代碼(tupianqiehuan.js):

$(function() { 
  var sWidth = $("#focus").width(); //獲取焦點(diǎn)圖的寬度(顯示面積) 
  var len = $("#focus ul li").length; //獲取焦點(diǎn)圖個(gè)數(shù) 
  var index = 0; 
  var picTimer; 
   
  //以下代碼添加數(shù)字按鈕和按鈕后的半透明條,還有上一頁、下一頁兩個(gè)按鈕 
  var btn = "<div class='btnBg'></div><div class='btn'>"; 
  for(var i=0; i < len; i++) { 
    btn += "<span></span>"; 
  } 
  btn += "</div><div class='preNext pre'></div><div class='preNext next'></div>"; 
  $("#focus").append(btn); 
  $("#focus .btnBg").css("opacity",0.5); 
 
  //為小按鈕添加鼠標(biāo)滑入事件,以顯示相應(yīng)的內(nèi)容 
  $("#focus .btn span").css("opacity",0.4).mouseover(function() { 
    index = $("#focus .btn span").index(this); 
    showPics(index); 
  }).eq(0).trigger("mouseover"); 
 
  //上一頁、下一頁按鈕透明度處理 
  $("#focus .preNext").css("opacity",0.2).hover(function() { 
    $(this).stop(true,false).animate({"opacity":"0.5"},300); 
  },function() { 
    $(this).stop(true,false).animate({"opacity":"0.2"},300); 
  }); 
 
  //上一頁按鈕 
  $("#focus .pre").click(function() { 
    index -= 1; 
    if(index == -1) {index = len - 1;} 
    showPics(index); 
  }); 
 
  //下一頁按鈕 
  $("#focus .next").click(function() { 
    index += 1; 
    if(index == len) {index = 0;} 
    showPics(index); 
  }); 
 
  //本例為左右滾動(dòng),即所有l(wèi)i元素都是在同一排向左浮動(dòng),所以這里需要計(jì)算出外圍ul元素的寬度 
  $("#focus ul").css("width",sWidth * (len)); 
   
  //鼠標(biāo)滑上焦點(diǎn)圖時(shí)停止自動(dòng)播放,滑出時(shí)開始自動(dòng)播放 
  $("#focus").hover(function() { 
    clearInterval(picTimer); 
  },function() { 
    picTimer = setInterval(function() { 
      showPics(index); 
      index++; 
      if(index == len) {index = 0;} 
    },4000); //此4000代表自動(dòng)播放的間隔,單位:毫秒 
  }).trigger("mouseleave"); 
   
  //顯示圖片函數(shù),根據(jù)接收的index值顯示相應(yīng)的內(nèi)容 
  function showPics(index) { //普通切換 
    var nowLeft = -index*sWidth; //根據(jù)index值計(jì)算ul元素的left值 
    $("#focus ul").stop(true,false).animate({"left":nowLeft},300); //通過animate()調(diào)整ul元素滾動(dòng)到計(jì)算出的position 
    $("#focus .btn span").stop(true,false).animate({"opacity":"0.4"},300).eq(index).stop(true,false).animate({"opacity":"1"},300);//為當(dāng)前的按鈕切換到選中的效果  
  } 
});

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“jQuery如何實(shí)現(xiàn)自動(dòng)或手動(dòng)圖片切換”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

當(dāng)前文章:jQuery如何實(shí)現(xiàn)自動(dòng)或手動(dòng)圖片切換
文章網(wǎng)址:http://bm7419.com/article18/jdccgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、虛擬主機(jī)微信公眾號靜態(tài)網(wǎng)站、服務(wù)器托管全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司