淡入淡出js輪播廣告

來源:互聯網
上載者:User

標籤:clear   cursor   縮圖   ret   廣告   pointer   osi   toolbar   int   

淡入淡出js輪播廣告,寫法類似自動播放的tab選項卡,加入了淡入淡出效果。

js代碼:

function lunbo(contClass, numArr, normalClass, hoverClass){
function getElementsByClass(searchClass,node,tag) {
var classElements = new Array();
if ( node == null )
node = document;
if ( tag == null )
tag = ‘*‘;
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}
var numArray = getElementsByClass(numArr),
contArray = getElementsByClass(contClass),
now = 0,
len = numArray.length;
//預設第一個顯示
numArray[0].className = hoverClass + ‘1‘;
contArray[0].style.display = ‘block‘;
//漸現
function fadeIn(el){
el.style.opacity = 0;
el.style.filter = ‘alpha(opacity = 0)‘;
el.style.display = ‘block‘;
var n = 0;
function fadeShow(){
if (n < 1){
n += 0.1;
el.style.opacity = n;
el.style.filter = ‘alpha(opacity = ‘+n*100+‘)‘;
var setTimeId = setTimeout(fadeShow, 50);
}else{
el.style.opacity = 1;
el.style.filter = ‘alpha(opacity = 100)‘;
clearTimeout(setTimeId);
}
}
fadeShow();
}
//漸隱
function fadeOut(el){
el.style.opacity = 1;
el.style.filter = ‘alpha(opacity = 100)‘;
var n = 1;
function fadeHide(){
if (n > 0){
n -= 0.1;
el.style.opacity = n;
el.style.filter = ‘alpha(opacity = ‘+n*100+‘)‘;
var setTimeId = setTimeout(fadeHide, 50);
}else{
el.style.opacity = 0;
el.style.filter = ‘alpha(opacity = 0)‘;
clearTimeout(setTimeId);
el.style.display = ‘none‘;
}
}
fadeHide();
}
//所有數字預設狀態
function normalNum(){
for (var i = 0; i < len; i++){
numArray[i].className = normalClass + (i+1);
}
}
//當前數字hover狀態
function showNum(now){
numArray[now].className = hoverClass + (now+1);
}
//自動播放
function autoPlay(){
if (now < (len - 1)){
normalNum();
fadeOut(contArray[now]);
showNum(now + 1);
fadeIn(contArray[now + 1]);
now += 1;
}else{
normalNum();
fadeOut(contArray[now]);
showNum(0);
fadeIn(contArray[0]);
now = 0;
}
lunboSettimeId = setTimeout(autoPlay, 3000);
}
lunboSettimeId = setTimeout(autoPlay, 3000);
//點擊事件
for (var i = 0; i < len; i++){(function(i){
numArray[i].onclick = function(){
clearTimeout(lunboSettimeId);
normalNum();
fadeOut(contArray[now]);
showNum(i);
fadeIn(contArray[i]);
now = i;
lunboSettimeId = setTimeout(autoPlay, 3000);
}
})(i)
}
}
lunbo(‘cont‘, ‘num‘, ‘num_normal‘, ‘num_hover‘);

參數說明:
contClass:輪播內容的class名;
numArr:所有數字樣式名(附加用來擷取數組的,無樣式內容),也可改成小縮圖;
normalClass:數字預設樣式名的前部分,如此例中數字預設樣式為.num_normal1到.num_normal5,此參數傳num_normal;
hoverClass:數字當前樣式名的前部分,使用類似上邊預設樣式

html代碼:

<div class="lunbo_box">
<div class="cont"><img src="images/pic1.jpg" /></div>
<div class="cont"><img src="images/pic2.jpg" /></div>
<div class="cont"><img src="images/pic3.jpg" /></div>
<div class="cont"><img src="images/pic4.jpg" /></div>
<div class="cont"><img src="images/pic5.jpg" /></div>
<ul class="num_ul">
<li class="num num_normal1"></li>
<li class="num num_normal2"></li>
<li class="num num_normal3"></li>
<li class="num num_normal4"></li>
<li class="num num_normal5"></li>
</ul>
</div>

css代碼:

div,ul,li{ margin:0; padding:0; font-size:12px;}
.lunbo_box{ position:relative; width:280px; height:210px; border:1px solid #666;}
.cont{ display:none; position:absolute; width:280px; height:210px; left:0; top:0;}
.cont img{ width:280px; height:210px;}
.num_ul{ position:absolute; width:120px; height:20px; right:10px; bottom:10px; list-style:none;}
.num_ul li{float:left; display:inline; width:21px; height:20px;margin-left:2px; cursor:pointer; overflow:hidden;}
.num_normal1,.num_normal2,.num_normal3,.num_normal4,.num_normal5,.num_hover1,.num_hover2,.num_hover3,.num_hover4,.num_hover5{background-image:url(../images/num.gif);}
.num_normal1{ background-position:0 0;}
.num_normal2{background-position:-21px 0;}
.num_normal3{background-position:-42px 0;}
.num_normal4{background-position:-63px 0;}
.num_normal5{background-position:-84px 0;}
.num_hover1{ background-position:0 -20px;}
.num_hover2{background-position:-21px -20px;}
.num_hover3{background-position:-42px -20px;}
.num_hover4{background-position:-63px -20px;}
.num_hover5{background-position:-84px -20px;}

淡入淡出js輪播廣告

聯繫我們

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