分析javascript縱向的相冊效果

字號:


    本代碼來自于懶人圖庫,本站只是對其代碼進行分析并解釋,并標注了源代碼的來源,本次是對縱向的相冊效果進行分析.
    注明:源文件來源于懶人圖庫(便于代碼簡潔,去除了部分懶人的ad)
    <!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 http-equiv=content-type content=text/html; charset=gb2312 />
    <title>縱向的js相冊效果</title>
    <style>
    body {background:#000000;margin:20px 0;font:12px verdana, arial, tahoma;text-align:center;vertical-align:middle;color:#ffffff}
    img {border:none}
    .txt_1 {font:bold 24px verdana, tahoma;color:#fff}
    img.thumb_img {cursor:pointer;display:block;margin-bottom:10px}
    img#main_img {cursor:pointer;display:block;}
    #gotop {cursor:pointer;display:block;}
    #gobottom {cursor:pointer;display:block;}
    #showarea {height:355px;margin:10px;overflow:hidden}
    .info {color:#666;font:normal 9px verdana;margin-top:20px}
    .info a:link, .info a:visited {color:#666;text-decoration:none}
    .info a:hover {color:#fff;text-decoration:none}
    </style>
    </head>
    <body>
    <table width=760 border=0 cellpadding=0 cellspacing=5>
    <tr>
    <td height=75 colspan=2 align=left class=txt_1>縱向的js相冊效果</td><!--<網站制作學習網foasp.cn>-->
    </tr>
    <tr>
    <td width=640><img src=images/03.jpg url width=640 height=400 border=0 id=main_img rel=images/03.jpg link=url /><!--這是展示整個大圖--></td>
    <td width=110 valign=top>
    <img src=images/gotop.gif width=100 height=14 id=gotop /><!--上邊的按鈕-->
    <div id=showarea>
     <img src=images/01.jpg  width=80 height=50 border=0 class=thumb_img rel=images/01.jpg link=url />
     ......省略n個圖片
     <img src=images/02.jpg  width=80 height=50 border=0 class=thumb_img rel=images/02.jpg link=url /><!--設置rel后圖片可以有l(wèi)ink的連接,點擊直接跳轉-->
    </div>
    <img src=images/gobottom.gif width=100 height=14 id=gobottom /><!--下邊的按鈕--></td>
    </tr>
    </table>
    </body>
    </html>
    <script language=javascript type=text/javascript>
    function $(e) {return document.getelementbyid(e);}//定義獲取通過對象
    document.getelementsbyclassname = function(cl) {//定義document的通過classname來獲得對象數(shù)組
    var retnode = [];//定義空數(shù)組
    var myclass = new regexp(cl);//通過正則上面?zhèn)鱽淼腸l值來判斷對象
    var elem = this.getelementsbytagname('*');
    for (var i = 0; i < elem.length; i++) {
     var classes = elem[i].classname;//如果classname符合正則則添加到定義的空數(shù)組
     if (myclass.test(classes)) retnode.push(elem[i]);
    }
    return retnode;//最后返回數(shù)組
    }
    var mymar;
    var speed = 1; //速度,越大越慢
    var spec = 1; //每次滾動的間距, 越大滾動越快
    var ipath = 'images/'; //圖片路徑
    var thumbs = document.getelementsbyclassname('thumb_img');//獲取以thumb_img的數(shù)組
    for (var i=0; i<thumbs.length; i++) {
    thumbs[i].onmouseover = function () {$('main_img').src=this.rel; $('main_img').link=this.link;};//循環(huán)對對象進行附加事件
    thumbs[i].onclick = function () {location = this.link}
    }
    $('main_img').onclick = function () {window.open(this.link);}//對中間的大圖進行附加事件
    $('gotop').onmouseover = function() {this.src = ipath + 'gotop2.gif'; mymar=setinterval(gotop,speed);}
    $('gotop').onmouseout = function() {this.src = ipath + 'gotop.gif'; clearinterval(mymar);}
    $('gobottom').onmouseover = function() {this.src = ipath + 'gobottom2.gif'; mymar=setinterval(gobottom,speed);}
    $('gobottom').onmouseout = function() {this.src = ipath + 'gobottom.gif'; clearinterval(mymar);}
    function gotop() {$('showarea').scrolltop-=spec;}//設定鼠標移動到上下按鈕時,圖片的整體滾動<網站制作學習網foasp.cn>
    function gobottom() {$('showarea').scrolltop+=spec;}
    </script>