jQuery實現(xiàn)首頁圖片淡入淡出效果的方法
本文實例講述了jQuery實現(xiàn)首頁圖片淡入淡出效果的方法。分享給大家供大家參考。具體分析如下:
這里演示當(dāng)當(dāng)網(wǎng)的品牌店鋪首頁效果,演示地址為:http://static.dangdang.com/gm/topic/2270_181320.shtml
效果圖如下所示:

需求:
1. 綠色區(qū)域要求在圖片上方,半透明顯示
2. 當(dāng)鼠標(biāo)移動到紅色區(qū)域,切換相應(yīng)的圖片
3. 首頁的三張大圖輪轉(zhuǎn)
HTML:
<div id="carousel">
<div id="carouselimg" class="content_top">
<div id="imgcontainer" class="slide_panel">
<a target="_blank" mce_><img src="images/brandshop/band_lx_717x280.gif" mce_src="images/brandshop/band_lx_717x280.gif"/></a>
<a target="_blank" mce_><img src="images/brandshop/band_agz_717x280.gif" mce_src="images/brandshop/band_agz_717x280.gif"/></a>
<a target="_blank" mce_><img src="images/brandshop/band_qb_717x280.gif" mce_src="images/brandshop/band_qb_717x280.gif"/></a>
</div>
</div>
<div id="carouseltitle">
<ul>
<li>
<a href="#" mce_href="#">聯(lián)想 <span>lenovo</span></a>
</li>
<li>
<a href="#" mce_href="#">愛國者 <span>aigo</span></a>
</li>
<li>
<a href="#" mce_href="#">倩碧 <span>CLINIQUE</span></a>
</li>
</ul>
</div>
CSS:
<mce:style type="text/css"><!--
*{
padding:0;
margin:0;
}
#carousel{
border-color:#DFDFDF;border-style:solid;border-width:0 1px 1px;
position:relative;/*DO NOT delete this line*/
}
#carousel ul{
list-style:none;
}
#carousel #carouselimg{
position:relative;/*fix ie7 overflow bug*/
overflow:hidden;
}
#carousel #carouselimg #imgcontainer{
position:absolute;
left:0px;
top:0px;
}
#carousel #carouselimg img{
float:left;/*fix ie6 auto-margin bug*/
border:0;
/*display:none;*/
}
#carousel #carouseltitle{
position:absolute;
bottom:0px;
}
#carousel #carouseltitle ul{
}
#carousel #carouseltitle li{
width:239px;
height:30px;
line-height:30px;
font-size:14px;
text-align:center;
background:#000;
color:#FFF;
float:left;
cursor:pointer;
opacity:.6;
filter:alpha(opacity=60);
}
#carousel #carouseltitle .active{
background:#cfaf73;
color:#FFF;
opacity:.9;
filter:alpha(opacity=90);
}
#carousel #carouseltitle .active a{
color:#000;
}
#carousel #carouseltitle li a{
text-decoration:none;
color:#fff;
}
#carousel #carouseltitle li a span{
font-family:Arial;
}
--></mce:style><style type="text/css" mce_bogus="1"> *{
padding:0;
margin:0;
}
#carousel{
border-color:#DFDFDF;border-style:solid;border-width:0 1px 1px;
position:relative;/*DO NOT delete this line*/
}
#carousel ul{
list-style:none;
}
#carousel #carouselimg{
position:relative;/*fix ie7 overflow bug*/
overflow:hidden;
}
#carousel #carouselimg #imgcontainer{
position:absolute;
left:0px;
top:0px;
}
#carousel #carouselimg img{
float:left;/*fix ie6 auto-margin bug*/
border:0;
/*display:none;*/
}
#carousel #carouseltitle{
position:absolute;
bottom:0px;
}
#carousel #carouseltitle ul{
}
#carousel #carouseltitle li{
width:239px;
height:30px;
line-height:30px;
font-size:14px;
text-align:center;
background:#000;
color:#FFF;
float:left;
cursor:pointer;
opacity:.6;
filter:alpha(opacity=60);
}
#carousel #carouseltitle .active{
background:#cfaf73;
color:#FFF;
opacity:.9;
filter:alpha(opacity=90);
}
#carousel #carouseltitle .active a{
color:#000;
}
#carousel #carouseltitle li a{
text-decoration:none;
color:#fff;
}
#carousel #carouseltitle li a span{
font-family:Arial;
}
</style>
引入River Zhang 的fr.carousel.js
FR={
Version:'1.0.0',
Author:'River Zhang(zhang_hechuan@hotmail.com)',
Lisence:'MIT Lisence'
};
FR.Util={
//Replace document.getElementById.
$:function(id){
return document.getElementById(id);
},
//Replace getElementsByTagName.
$$:function(node, tag){
return node.getElementsByTagName(tag);
},
creat:function(node,name){
var element=document.createElement(name);
node.appendChild(element);
return element;
},
//Event Binding functions.
addEvent:function(eventType,eventFunc,eventObj){
eventObj = eventObj || document;
if(window.attachEvent)eventObj.attachEvent("on"+eventType,eventFunc);
if(window.addEventListener) eventObj.addEventListener(eventType,eventFunc,false);
},
setOpacity:function(obj, value) {
if (document.all) obj.style.filter = "alpha(opacity=" + value + ")";
else obj.style.opacity = value / 100;
},
setPosition:function(obj, x, y){
var curx=parseInt(obj.style.left);
var cury=parseInt(obj.style.top);
if(isNaN(curx)) curx=cury=0;
var newx=curx+x;
var newy=cury+y;
obj.style.left=newx+'px';
obj.style.top=newy+'px';
}
};
FR.Carousel={
version:'1.1',
mode:1,
steps:20,
period:25,
width:300,
height:200,
bgColor:'#000000',
autoSwitch:true,
delay:5000,
_semaphore:0,/* DO NOT try to modify this value */
start:function(args){
if(typeof(args)!='undefined'){
FR.Carousel.mode=args.mode||FR.Carousel.mode;
FR.Carousel.steps=args.steps||FR.Carousel.steps;
FR.Carousel.period=args.period||FR.Carousel.period;
FR.Carousel.width=args.width||FR.Carousel.width;
FR.Carousel.height=args.height||FR.Carousel.height;
FR.Carousel.bgColor=args.bgColor||FR.Carousel.bgColor;
FR.Carousel.autoSwitch=args.autoSwitch||FR.Carousel.autoSwitch;
FR.Carousel.delay=args.delay||FR.Carousel.delay;
}
FR.Util.addEvent("load",FR.Carousel.run,window);
},
run:function(){
FR.Carousel.initialCSS();
FR.Carousel.counter='frimg0';
var carouselimg=FR.Util.$('carouselimg');
var img=FR.Util.$$(carouselimg, 'img');
for(var i=0;i!=img.length;++i){
img[i].id='frimg'+i;
if(FR.Carousel.mode==4 || FR.Carousel.mode==5) continue;
img[i].style.position="absolute";
img[i].style.left="0 px";
img[i].style.top="0 px";
FR.Util.setOpacity(img[i], 0);
}
if(FR.Carousel.mode!=4) FR.Util.setOpacity(img[0], 100);
if(FR.Carousel.mode==1) bindFunction=function(name){FR.Carousel.fade(FR.Util.$(name), FR.Carousel.steps, FR.Carousel.period);};
else if(FR.Carousel.mode==2) bindFunction=function(name){FR.Carousel.flash(FR.Util.$(name), FR.Carousel.steps, FR.Carousel.period);};
else if(FR.Carousel.mode==3) bindFunction=function(name){FR.Carousel.fadeIntoColor(FR.Util.$(name), FR.Carousel.steps, FR.Carousel.period);};
else if(FR.Carousel.mode==4) bindFunction=function(name){FR.Carousel.scroll(name, FR.Carousel.steps, FR.Carousel.period);};
else if(FR.Carousel.mode==5) bindFunction=function(name){FR.Carousel.crawl(name, FR.Carousel.steps, FR.Carousel.period);};
var carouseltitle=FR.Util.$('carouseltitle');
var li=FR.Util.$$(carouseltitle, 'li');
li[0].className='#carousel #carouseltitle active';
FR.Carousel.autoCarousel(img.length);
for(var i=0;i!=li.length;++i){
(function(){
var name='frimg'+i;
li[i].onmouseover=function(){
clearInterval(FR.Carousel.s);
if(!FR.Carousel._semaphore){
li[FR.Carousel.counter.split('')[FR.Carousel.counter.length-1]].className='';
this.className='#carousel #carouseltitle active';
bindFunction(name);
}
};
li[i].onmouseout=function(){
FR.Carousel.autoCarousel(img.length);
}
})();
}
},
autoCarousel:function(length){
if(FR.Carousel.autoSwitch){
FR.Carousel.s=setInterval(function(){
var carouseltitle=FR.Util.$('carouseltitle');
var li=FR.Util.$$(carouseltitle, 'li');
li[FR.Carousel.counter.split('')[FR.Carousel.counter.length-1]].className='';
var next=(parseInt(FR.Carousel.counter.split('')[FR.Carousel.counter.length-1])+1)%length;
li[next].className='#carousel #carouseltitle active';
name='frimg'+next;
bindFunction(name);
},FR.Carousel.delay);
}
},
initialCSS:function(){
var carouselimg=FR.Util.$('carouselimg');
var carousel=FR.Util.$('carousel');
carouselimg.style.width=FR.Carousel.width+"px";
carouselimg.style.height=FR.Carousel.height+"px";
carousel.style.width=FR.Carousel.width+"px";
carousel.style.height=FR.Carousel.height+"px";
if(FR.Carousel.mode==5){
var imgcontainer=FR.Util.$('imgcontainer');
var img=FR.Util.$$(carouselimg, 'img');
var size=img.length*FR.Carousel.width;
imgcontainer.style.width=size+"px";
}
},
fade:function(obj, steps, speed) {
FR.Carousel._semaphore=1;
var value1=0;
var value2=100;
if(obj.id!=FR.Carousel.counter){
var carouselimg=FR.Util.$('carouselimg');
var img=FR.Util.$$(carouselimg, 'img');
for(var i=0;i!=img.length;++i){
if(i!=FR.Carousel.counter.split('')[FR.Carousel.counter.length-1])
FR.Util.setOpacity(img[i], 0);
}
temp=FR.Carousel.counter;
FR.Carousel.counter=obj.id;
tempobj=FR.Util.$(temp);
var increment=100/steps;
FR.Carousel.i=setInterval(function(){
if(value1<=100){
FR.Util.setOpacity(obj,value1);
FR.Util.setOpacity(tempobj,value2);
value1+=increment;
value2-=increment;
} else {
clearInterval(FR.Carousel.i);
FR.Carousel._semaphore=0;
}
},speed);
}else {
FR.Carousel._semaphore=0;
return;
}
},
flash:function(obj, steps, speed) {
FR.Carousel._semaphore=1;
var value1=0;
if(obj.id!=FR.Carousel.counter){
var carouselimg=FR.Util.$('carouselimg');
var img=FR.Util.$$(carouselimg, 'img');
for(var i=0;i!=img.length;++i){
FR.Util.setOpacity(img[i], 0);
}
FR.Carousel.counter=obj.id;
var increment=100/steps;
FR.Carousel.i=setInterval(function(){
if(value1<=100){
FR.Util.setOpacity(obj,value1);
value1+=increment;
} else {
clearInterval(FR.Carousel.i);
FR.Carousel._semaphore=0;
}
},speed);
}else {
FR.Carousel._semaphore=0;
return;
}
},
fadeIntoColor:function(obj, steps, speed){
FR.Carousel._semaphore=1;
var value1=100;
var value2=0;
if(obj.id!=FR.Carousel.counter){
var carouselimg=FR.Util.$('carouselimg');
carouselimg.style.backgroundColor=FR.Carousel.bgColor;
var img=FR.Util.$$(carouselimg, 'img');
for(var i=0;i!=img.length;++i){
if(i!=FR.Carousel.counter.split('')[FR.Carousel.counter.length-1])
FR.Util.setOpacity(img[i], 0);
}
temp=FR.Carousel.counter;
FR.Carousel.counter=obj.id;
tempobj=FR.Util.$(temp);
var increment=100/steps;
FR.Carousel.i=setInterval(function(){
if(value1>=0){
FR.Util.setOpacity(tempobj,value1);
value1-=increment;
}
else if(value1<0 && value2<=100){
FR.Util.setOpacity(obj,value2);
value2+=increment;
} else {
clearInterval(FR.Carousel.i);
FR.Carousel._semaphore=0;
}
},speed);
} else {
FR.Carousel._semaphore=0;
return;
}
},
scroll:function(curno, steps, speed){
FR.Carousel._semaphore=1;
var ic=FR.Util.$('imgcontainer');
var count=(curno.split('')[curno.length-1]-FR.Carousel.counter.split('')[FR.Carousel.counter.length-1])*FR.Carousel.height;
FR.Carousel.counter=curno;
var value1=0;
var increment=count/steps;
FR.Carousel.i=setInterval(function(){
if(Math.abs(value1)<Math.abs(count)){
if(count>0){
FR.Util.setPosition(ic,0,-increment);
value1-=increment;
}
else{
FR.Util.setPosition(ic,0,-increment);
value1+=increment;
}
} else {
clearInterval(FR.Carousel.i);
FR.Carousel._semaphore=0;
}
},speed);
},
crawl:function(curno, steps, speed){
FR.Carousel._semaphore=1;
var ic=FR.Util.$('imgcontainer');
var count=(curno.split('')[curno.length-1]-FR.Carousel.counter.split('')[FR.Carousel.counter.length-1])*FR.Carousel.width;
FR.Carousel.counter=curno;
var value1=0;
var increment=count/steps;
FR.Carousel.i=setInterval(function(){
if(Math.abs(value1)<Math.abs(count)){
if(count>0){
FR.Util.setPosition(ic,-increment,0);
value1-=increment;
}
else{
FR.Util.setPosition(ic,-increment,0);
value1+=increment;
}
} else {
clearInterval(FR.Carousel.i);
FR.Carousel._semaphore=0;
}
},speed);
}
};
可以實現(xiàn)圖片的輪轉(zhuǎn)效果,但是點擊圖片后,每次鏈接跳轉(zhuǎn)都是最后一張圖的a href
查看fr.carousel.js ,原來,它實現(xiàn)的思路是將三張圖一張疊在一張上邊,使用setInterval() ,將一張圖的opacity 值置為1,其余兩張為0。但是,這將導(dǎo)致你點擊圖片后,每次的鏈接都是最上邊的那張圖(即最后倩碧那一張)。
解決方法:
設(shè)置輪轉(zhuǎn)圖的 z-index,當(dāng)圖片顯示的時候,其父節(jié)點<a> 的z-index 比其它元素高。
jQuery實現(xiàn):
// 判斷img輪轉(zhuǎn),實現(xiàn)a跳轉(zhuǎn)
setInterval(function(){
$("#imgcontainer a").each(function(i){
var img = $(this).children("img");
var op = img.css("opacity");
if(op>0){
img.css("z-index","100");
$(this).css("z-index","100");
}else{
img.css("z-index","0");
$(this).css("z-index","0");
}
})
},100);
希望本文所述對大家的jQuery程序設(shè)計有所幫助。
- jquery 淡入淡出效果的簡單實現(xiàn)
- jQuery實現(xiàn)的淡入淡出與滑入滑出效果示例
- jQuery 淡入淡出、展開收縮菜單實現(xiàn)代碼
- 基于jquery實現(xiàn)的文字淡入淡出效果
- 基于Jquery的淡入淡出的特效基礎(chǔ)練習(xí)
- jQuery淡入淡出元素讓其效果更為生動
- jQuery實現(xiàn)圖片文字淡入淡出效果
- 基于jquery的淡入淡出選項卡效果代碼
- jQuery實現(xiàn)DIV層淡入淡出拖動特效的方法
- jquery淡入淡出效果簡單實例
- 非常實用的jQuery代碼段集錦【檢測瀏覽器、滾動、復(fù)制、淡入淡出等】
- jQuery 淡入/淡出效果函數(shù)用法分析
相關(guān)文章
jquery預(yù)覽圖片實現(xiàn)鼠標(biāo)放上去顯示實際大小
本文為大家介紹的這個示例為jquery實現(xiàn)的預(yù)覽圖片,當(dāng)鼠標(biāo)放上去顯示實際大小,感興趣的朋友可以學(xué)習(xí)下2014-01-01
自己動手實現(xiàn)jQuery Callbacks完整功能代碼詳解
最近大量的用到j(luò)Query Callbacks 對象,jQuery庫中的$.ajax()和$.Deferred() 對象也是基于這個對象實現(xiàn),下面我們也模擬實現(xiàn)jQuery Callbacks 對象的部分功能2013-11-11
jQuery移除tr無效的解決方法(tr是動態(tài)添加)
移除掉某些tr(tr是動態(tài)添加的)嘗試了很多方法,都不見效,后來發(fā)現(xiàn)個不錯的方法,于是與大家分享下2014-09-09
jQuery實現(xiàn)簡易的輸入框字?jǐn)?shù)計數(shù)功能示例
這篇文章主要介紹了jQuery實現(xiàn)簡易的輸入框字?jǐn)?shù)計數(shù)功能,結(jié)合實例形式分析了jQuery針對頁面元素屬性的獲取與計算相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
jQuery 獲取/設(shè)置/刪除DOM元素的屬性以a元素為例
這篇文章主要介紹了jQuery如何獲取/設(shè)置/刪除DOM元素的屬性,需要的朋友可以參考下2014-05-05

