js特效:图片翻转效果

求使然,需要实现图片翻转特效 ,类似诸多大型网站上滚动的图片新闻。大凡特效,JS一般都可实现,而且用JS,却酷似Flash,非常绚。记得以前做过的小东西,直接拿来用了。

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script language=JavaScript>

var imgUrl=new Array();
var imgLink=new Array();
var adNum=0;
imgUrl[1]=http://photo14.yupoo.com/20071212/143930_1826427539_yakfhejo.jpg;
imgLink[1]="http://www.thinkjam.org";

imgUrl[2]=http://photo14.yupoo.com/20071212/143930_1826427539_yakfhejo.jpg;
imgLink[2]="http://www.thinkjam.org";

imgUrl[3]=http://photo14.yupoo.com/20071212/143930_1826427539_yakfhejo.jpg;
imgLink[3]="http://www.thinkjam.org";


var imgPre=new Array();
var j=0;
for (i=1;i<=3;i++) {
if( (imgUrl[i]!="") && (imgLink[i]!="") ) {
j++;
} else {
break;
}
}

function playTran(){
if (document.all)
imgInit.filters.revealTrans.play();
}

var key=0;
function nextAd(){
if(adNum<j)adNum++ ;
else adNum=1;

if( key==0 ){
key=1;
} else if (document.all){
imgInit.filters.revealTrans.Transition=100;
imgInit.filters.revealTrans.apply();
playTran();

}
document.images.imgInit.src=imgUrl[adNum];
theTimer=setTimeout("nextAd()", 3000); <!--设置图片更换的时间间隔-->
}

function goUrl(){
jumpUrl=imgLink[adNum];
jumpTarget='_blank';
if (jumpUrl != ''){
if (jumpTarget != '')
window.open(jumpUrl,jumpTarget);
else
location.href=jumpUrl;
}
}
</script>

</head>
<body>

<div><!-- 动态图开始 -->
<a href="javascript:goUrl()"><img style="FILTER: revealTrans(duration=2,transition=20);border-color:#000000;color:#000000" src="javascript:nextAd()" border=1 class=img01 name=imgInit></a><!

-- 动态图结束 --></div>

</body></html>

JS代码可放入页面代码中任何位置,最好放在head中。这种方法当然是最简单的了,因为我对代码如此白痴都会用,呵呵。此外还有许多其他方法,如下:

其他一:渐变轮换效果

<SCRIPT>
var NowFrame = 1;
var MaxFrame = 4;
var bStart = 0;
function fnToggle() {
    var next = NowFrame + 1;

    if(next == MaxFrame+1)
    {
        NowFrame = MaxFrame;
        next = 1;
    }

    if(bStart == 0)
    {
        bStart = 1;
       
        setTimeout('fnToggle()', 3500);

        return;
    }
    else
    {
        oTransContainer.filters[0].Apply();

        document.images['oDIV'+next].style.display = "";
        document.images['oDIV'+NowFrame].style.display = "none";

        oTransContainer.filters[0].Play(duration=2);

        if(NowFrame == MaxFrame)
            NowFrame = 1;
        else
            NowFrame++;
    }    setTimeout('fnToggle()', 3500);
}

fnToggle();
</SCRIPT>
<div align="center">
<DIV id=oTransContainer style="FILTER: progid:DXImageTransform.Microsoft.Wipe(GradientSize=1.0,wipeStyle=0, motion='forward'); WIDTH: 165px; HEIGHT: 83px">
<A href="/default.asp" target=_blank><IMG id=oDIV1 style="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid" src=http://photo15.yupoo.com/20080102/181403_14520634_zthbrrih.jpg border=0 width=241 height=121></A>
<A href="/default.asp" target=_blank><IMG id=oDIV2 style="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-LEFT: black 1px solid; DISPLAY: none; BORDER-BOTTOM: black 1px solid" width=241 height=121 src=http://blog.cnknow.com/attachments/I8G2007122974436_0603/85wa_135787.jpg border=0></A>
<A href="/default.asp" target=_blank><IMG id=oDIV3 style="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-LEFT: black 1px solid; DISPLAY: none; BORDER-BOTTOM: black 1px solid" src=http://photo15.yupoo.com/20080102/181403_14520634_zthbrrih.jpg border=0 width=241 height=121></A>
<A href="/default.asp" target=_blank><IMG id=oDIV4 style="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-LEFT: black 1px solid; DISPLAY: none; BORDER-BOTTOM: black 1px solid" src=http://photo15.yupoo.com/20080102/181403_14520634_zthbrrih.jpg width=241 height=121 border=0></A>

</div>

其他二:JS生成的Flash效果

<SCRIPT type=text/javascript>
    <!--
   
    var focus_width=260
    var focus_height=182
    var text_height=25
    var swf_height = focus_height+text_height
   
    var pics=http://photo15.yupoo.com/20080102/181403_14520634_zthbrrih.jpg|http://photo14.yupoo.com/20080102/181403_1536117876_xqnapaxy.jpg|http://photo14.yupoo.com/20080102/181403_1536117876_xqnapaxy.jpg'
    var links='http://thinkjam.org/|http://thinkjam.org/|http://thinkjam.org/'
    var texts='唐正东与莫科之争|江苏掀翻八一|巴特尔独木难支'
   
    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+ focus_width +'" height="'+ swf_height +'">');
    document.write('<param name="allowScriptAccess" value="sameDomain"><param name="movie" value="/images/media/pixviewer.swf"><param name="quality" value="high"><param name="bgcolor" value="#DADADA">');
    document.write('<param name="menu" value="false"><param name=wmode value="opaque">');
    document.write('<param name="FlashVars" value="pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'">');
    document.write('</object>');
   
    //-->
</SCRIPT>

其他三:

<script language="Javascript" type="text/javascript">
function ImgArray(len){
    this.length=len;
}
ImgName = new ImgArray(10);
ImgName[0]="http://photo15.yupoo.com/20080102/181403_14520634_zthbrrih.jpg";
ImgName[1]=http://photo14.yupoo.com/20080102/181403_1536117876_xqnapaxy.jpg;
var t=0;
function playImg(){
    if (t==1){t=0;}
    else{t++;}
    img.style.filter="blendTrans(Duration=3)";
    img.filters[0].apply();
    img.src=ImgName[t];
    img.filters[0].play();
    mytimeout=setTimeout("playImg()",5000);
}
</script>
</head>
<img src="http://photo14.yupoo.com/20080102/181403_1536117876_xqnapaxy.jpg" name="img" />
<script language="Javascript">playImg();</script>

其他四:

<!--begin:图片轮换开始-->
<script language=JavaScript>
var imgUrl=new Array();
var imgLink=new Array();
var imgTz=new Array();
var buttonShow=1;
var buttonPos=1;
var buttonX;
var buttonY;
var bakularCode=""
var adNum=0;
var dakularCode="";
imgUrl[1]="http://photo14.yupoo.com/20080102/181403_1536117876_xqnapaxy.jpg";
imgLink[1]="http://www.thinkjam.org/mercury";
imgTz[1]="墨神的凡龛";
imgUrl[2]="http://photo15.yupoo.com/20080102/181403_14520634_zthbrrih.jpg";
imgLink[2]="http://www.thinkjam.org";
imgTz[2]="原生脑浆";
var imgPre=new Array();
for (i=1;i<4;i++)
{
imgPre[i]=new Image();
    imgPre[i].src=imgUrl[i];
}
function setTransition()
{
if (document.all)
{
   imgUrlrotator.filters.revealTrans.Transition=23;//Math.floor(Math.random()*23)
     imgUrlrotator.filters.revealTrans.apply();
}
}
function playTransition()
{
if (document.all)
imgUrlrotator.filters.revealTrans.play()
}
function nextAd(toNum)
{
if(adNum<imgUrl.length-1)
adNum++ ;
else
adNum=1;
   if(toNum!=null) adNum=toNum;
   setTransition();
document.images.imgUrlrotator.src=imgUrl[adNum];
document.getElementById('linktext').innerHTML=imgTz[adNum];
document.images.imgUrlrotator.alt='今日头条:'+imgTz[adNum];
document.getElementById('linktext').innerHTML="<a href="+imgLink[adNum]+" target=_blank class=px14-lh20>"+imgTz[adNum]+"</a>";
playTransition();
checkButton();
if(toNum!=null)
   {toNum=null;theTimer=setTimeout("nextAd()", 7000);}
else
{theTimer=setTimeout("nextAd()", 7000);}
}
function jump2url()
{
jumpUrl=imgLink[adNum];jumpTarget='_blank';
if (jumpUrl != '')
{   if (jumpTarget != '')
     window.open(jumpUrl,jumpTarget);
   else location.href=jumpUrl;
   }
}
function displayStatusMsg()
{
status=imgLink[adNum];
    document.returnValue = true;
}
function checkButton()
{
for(i=1;i<=imgUrl.length-1;i++)
   {if(i==adNum)
    //document.getElementById('bt'+i).style.backgroundColor='#00FF00';
    eval("bt"+i+".style.backgroundColor='#9B0000';bt"+i+".style.color='#FFFFFF';");
   else
    eval("bt"+i+".style.backgroundColor='#CCCCCC';bt"+i+".style.color='#000000';");
   }
}
function dakularButtons()
{
if(buttonShow==1)
{
   switch(buttonPos)
   {
    case 1:{buttonX=160+20-imgUrl.length*20;buttonY=16;break;}
    case 2:{buttonX=160+20-imgUrl.length*20;buttonY=125;break;}
    default:{buttonX=160+20-imgUrl.length*20;buttonY=16;break;}
   }
   for(i=1;i<=imgUrl.length-1;i++)
   {
    dakularCode=dakularCode+"<div id='bt"+i+"' style='display:inline; height=14px; width:20px; border:1px solid #000000; background-color:#f7f7f7; color:#000000; font-size:12px; line-height:14px; filter:Alpha(Opacity=50, FinishOpacity=50,Style=2);' align='center' onMouseOver=this.style.backgroundColor='#FFFFFF';this.style.color='#000000';this.style.cursor='hand' onMouseOut=checkButton(); onClick='clearTimeout(theTimer);nextAd("+i+");'>"+i+"</div>";
   }
   dakularCode="<div style='display:inline; position:relative;left:"+buttonX+"px; top:"+buttonY+"px; z-index:1;'>"+dakularCode+"</div>";
   document.write(dakularCode);
}
}
</script>
<table border="0" cellpadding="0" cellspacing="0" id=newsTable style="position:relative; top:-10px;right:0px; font-size:12px;">
<script language="javascript">dakularButtons();</script><tr><td><a onMouseOver="displayStatusMsg();return document.returnValue;" onMouseOut="status='';" href="javascript:jump2url()"><img style="FILTER: revealTrans(duration=1,transition=18); border:1px solid #000000" src="javascript:nextAd()" width=160 height=150 border=0 name=imgUrlrotator alt="今日头条"></a></td></tr>
<tr><td bgcolor="#f0f0f0" align="center" style="border-bottom:1px solid #000000; border-left:1px solid #000000;border-right:1px solid #000000;" height="20"><label id="linktext"></label></td></tr>
</table>
<!--begin:图片轮换结束-->

其他五:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Language" content="zh-CN">
<title>DronFw Example</title>
<style type="text/css"> td,body{font-size:12px;}</style>
<script type="text/javascript" src="http://ucren.com/dronfw/DronFw/boot.js" loadClass="PicturePlayer"></script>
</head>
<body>
<h2>游戏人图片轮播器类</h2>
<hr>

<div id="box">
</div>

<script type="text/javascript">
//Name, Title, Src, Url
var pdata =
[
["名称一", "标题一", "PicturePlayer/1.jpg", "http://www.baidu.com"],
["名称二", "标题二", "PicturePlayer/2.jpg", "http://www.baidu.com"],
["名称三", "标题三", "PicturePlayer/3.jpg", "http://www.baidu.com"],
["名称四", "标题四", "PicturePlayer/4.jpg", "http://www.baidu.com"]
];
var pp = new DronFw.Class.PicturePlayer;
pp.createPlayer("box", 365, 120);
pp.timeout = 3000;
pp.loadData(pdata);
/*
PicturePlayer 类的属性方法如下:
属性
   timeout : 等待 timeout 毫秒后切换帧
方法
   createPlayer(tid, width, height)   :   在 id 为 tid 的层里创建大小为 width * height 的轮播器
   loadData(f) : 加载图片信息数据

其它说明:由于 Firefox 不支持滤镜,所以那些变来变去的效果都没有,只是简单的切换
*/
</script>
</body>
</html>

其他六:

<script>
var links = new Array();
links[1] = "http://tongji.baidu.com";
links[2] = "http://hi.baidu.com/bdadd/blog/item/6f9f80b12e50cf57092302e3.html";
links[3] = "http://union.baidu.com/promo/dataunion/index.html";
links[4] = "http://www.umaz.cn";
var imgs = new Array();
for(var n = 1; n <= 5; n++) imgs[n] = new Image();
imgs[1].src = "http://union.baidu.com/img/tongji550.gif";
imgs[2].src = "http://union.baidu.com/img/banner071031.jpg";
imgs[3].src = "http://union.baidu.com/img/dataunion0711.jpg";
imgs[4].src = "http://union.baidu.com/img/umaz13_550.jpg";
var tits = new Array();
tits[1] ="百度统计";
tits[2] = "联盟杯摄影师大赛";
tits[3] = "百度行业报告";
tits[4] = "联盟志";

var imgwidth = 550;//图片宽度
var imgheight = 134;//图片宽度

var str = "<style type='text/css'>";
str += "#imgnv{display:none;position:absolute;bottom:-1px;right:0;height:16px;}#imgnv div{float:left;margin-right:1px;}";
str += "#imgnv div.on,#imgnv div.off{margin-bottom:1px;width:30px;height:15px;line-height:18px!important;line-height:15px;font-size:9px;text-align:center;cursor:pointer;cursor:hand}";
str += "#imgnv div.on{background:#CE0609;color:#FFF;font-weight:bold}";
str += "#imgnv div.off{background:#323232;color:#FFF;text-decoration:none}";
str += "#titnv{margin-top:3px;color:#000;text-align:center;display:none;}";
str += "</style>";
str += "<div style='position:relative'>";
str += "<div><a id='dlink' href='" + links[1] + "' target='_blank'><img id='dimg' src='" + imgs[1].src + "' border='0' width='" + imgwidth + "' height='"+imgheight+"' style='filter:Alpha(opacity=100)' onmouseover='Pause(true)' onmouseout='Pause(false)'></a></div>";

//修改点1:循环添加内层div内容以增加个数
str += "<div id='imgnv'><div id='it1' class='on' onmouseover='ImgSwitch(1, true)' onmouseout='Pause(false)'>1</div><div id='it2' class='off' onmouseover='ImgSwitch(2, true)' onmouseout='Pause(false)'>2</div><div id='it3' class='on' onmouseover='ImgSwitch(3, true)' onmouseout='Pause(false)'>3</div><div id='it4' class='off' onmouseover='ImgSwitch(4, true)' onmouseout='Pause(false)'>4</div></div>";
str += "<div id='titnv'><b>" + tits[1] + "</b></div>";
str += "</div>";
document.write(str);

var oi = document.getElementById("dimg");
var pause = false;
var curid = 1;
var lastid = 1;
var sw = 1;
var opacity = 100;
var speed = 15;
var delay = (document.all)? 400:700;

function SetAlpha(){
if(document.all){
if(oi.filters && oi.filters.Alpha) oi.filters.Alpha.opacity = opacity;
}else{
oi.style.MozOpacity = ((opacity >= 100)? 99:opacity) / 100;
}
}

function ImgSwitch(id, p){
if(p){
pause = true;
opacity = 100;
SetAlpha();
}
oi.src = imgs[id].src;
document.getElementById("dlink").href = links[id];
document.getElementById("it" + lastid).className = "off";
document.getElementById("it" + id).className = "on";
document.getElementById("titnv").innerHTML = "<b>" + tits[id] + "</b>";
curid = lastid = id;
}

function ScrollImg(){
if(pause && opacity >= 100) return;
if(sw == 0){
opacity += 2;
if(opacity > delay){ opacity = 100; sw = 1; }
}
if(sw == 1){
opacity -= 3;
if(opacity < 10){ opacity = 10; sw = 3; }
}
SetAlpha();
if(sw != 3) return;
sw = 0;
curid++;
//修改点2:这里的4也是个数
if(curid > 4) curid = 1;
ImgSwitch(curid, false);
}

function Pause(s){
pause = s;
}

function StartScroll(){
setInterval(ScrollImg, speed);
}

function CheckLoad(){
if (imgs[1].complete == true && imgs[2].complete == true) {
clearInterval(checkid);
setTimeout(StartScroll, 2000);
}
}

var checkid = setInterval(CheckLoad, 10);
</script>

JS特效方法多多,但是具体实现时,不得不考虑其在各浏览器的兼容性问题,详见JS特效的兼容性:重叠层和遮挡问题的解决