js+css写的图片自动切换
网上有很多的这样的js代码,今天没什么事,看了一段,自己模仿了一下,记录下来,什么时候再仔细钻研钻研……
1、html代码
View Code
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5 <title>图片自动切换</title> 6 <link rel="stylesheet" type="text/css" href="css/picChange.css"/> 7 <script language="javascript" src="js/picChange.js"></script> 8 </head> 9 <body> 10 <div id="main"> 11 <div id="picWhole"> 12 <div id="pic"> 13 <div id="picList"> 14 <ul> 15 <li><a href="#" target="_blank"><img src="images/01.jpg" alt="性感美女1"/></a></li> 16 <li><a href="#" target="_blank"><img src="images/02.jpg" alt="高挑美女2"/></a></li> 17 <li><a href="#" target="_blank"><img src="images/03.jpg" alt="制服美女3"/></a></li> 18 <li><a href="#" target="_blank"><img src="images/04.jpg" alt="漂亮美女4"/></a></li> 19 </ul> 20 </div> 21 <div id="blank"></div> 22 <div id="picText"> 23 <ul> 24 <li class="current">2008年度全球第一美女。。。</li> 25 <li class="normal">2002年以来时尚界的美女哦。。</li> 26 <li class="normal">2005年的精致美女哦。。</li> 27 <li class="normal">2012年的美女哦美女啊美女女。。</li> 28 </ul> 29 </div> 30 </div> 31 <div id="picFoucus"> 32 <ul> 33 <li class="current"><img src="images/01.jpg" alt="性感美女1"/></li> 34 <li class="normal"><img src="images/02.jpg" alt="性感美女1"/></li> 35 <li class="normal"><img src="images/03.jpg" alt="性感美女1"/></li> 36 <li class="normal"><img src="images/04.jpg" alt="性感美女1"/></li> 37 </ul> 38 </div> 39 </div> 40 </div> 41 </body> 42 </html>
2、css 代码
View Code
/* 重置样式 */ *{ margin: 0px; padding: 0px; word-break: break-all; } /* 整体的基础样式 */ body{ background-color: #fff; color: #000; font: 12px/1.6em Helvetica, Arial, sans-serif; margin: 0px; text-align: center; } h1,h2,h3,h4,h5,h6{ font-size: 1em; } a{ color:#0287CA; text-decoration:none; } a:hover{ text-decoration:underline;} ul,li{ list-style:none;} fieldset, img { border:none; } legend { display:none; } em, strong, cite, th { font-style:normal; font-weight:normal; } input, textarea, select, button { font:12px Helvetica, Arial, sans-serif; } table { border-collapse:collapse; } html { overflow:-moz-scrollbars-vertical; } #main{ width: 655px; margin: 0 auto; text-align: left; } /* 图片的全局样式 */ #picWhole{ width:650px; height:245px; margin:0px; border:1px solid #DEDEDE; background:#F8F8F8; } #pic{ display:inline; position:relative; float:left; width:540px; height:225px; overflow:hidden; margin:10px 0 0 10px; } #picList{ position:absolute; left: 0; top: 0; } #picList li{ width:550px; height:225px; overflow:hidden; } #picList li img{ width:550px; height:225px; } #picFoucus { display:inline; float:right; width:91px; margin:9px 9px 0 0; } #picFoucus li { width:91px; height:57px; cursor:pointer; opacity:0.5; -moz-opacity:0.5; filter:alpha(opacity=50); } #picFoucus img { width:75px; height:45px; margin:7px 0 0 11px; } #picFoucus .current { background: url(images/01.jpg) no-repeat; opacity:1; -moz-opacity:1; filter:alpha(opacity=100); } /* 模拟文本后面的文本框 */ #blank { position:absolute; left:0; bottom:0; width:545px; height:35px; background:#000; opacity:0.5; -moz-opacity:0.5; filter:alpha(opacity=50); } /* 设置图片文本的css样式 */ #picText { position:absolute; left:8px; bottom:8px; color:#FFF; font-size: 1.1em; } #picText .normal { display:none; }
3、js代码
View Code
/** * author: 谯杨 * published: 2012.08.07 */ /** * @param id 获取元素的id * @return id所对应的结点 */ var atuokey = false; function $(id) { return document.getElementById(id); } function loadFunction(func){ var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function(){ oldonload(); func(); } } } //移动元素的函数 function moveElement(elementId, final_x, final_y, interval){ node = $(elementId); if(node.movement){ clearTimeout(node.movement); } if(!node.style.left){ node.style.left = "0px"; } if(!node.style.top){ node.style.top = "0px"; } var xpos = parseInt(node.style.left); var ypos = parseInt(node.style.top); if(xpos == final_x && ypos == final_y){ return true; } if(xpos < final_x){ var dis = Math.ceil((final_x - xpos)/10) xpos += dis; } if(xpos > final_x){ var dis = Math.ceil((xpos-final_x)/10); xpos -= dis; } if(ypos < final_y){ var dis = Math.ceil((final_y - ypos)/10); ypos += dis; } if(ypos > final_y){ var dis = Math.ceil((ypos - final_y) /10); ypos -= dis; } node.style.left = xpos + "px"; node.style.top = ypos + "px"; var repeat = "moveElement('"+elementId+"',"+final_x+","+final_y+","+interval+")"; node.movement = setInterval(repeat,interval); } function classNormal(picFoucusId, picTextId){ var focusNodes = $(picFoucusId).getElementsByTagName("li"); var textNodes = $(picTextId).getElementsByTagName("li"); for(var i = 0; i < 4; i++ ){ focusNodes[i].className = "normal"; textNodes[i].className = "normal"; } } function classCurrent(picFoucusId, picTextId, n){ var focusNodes = $(picFoucusId).getElementsByTagName("li"); var textNodes = $(picTextId).getElementsByTagName("li"); focusNodes[n].className = "current"; textNodes[n].className = "current"; } function picSwitch(){ if(!$("picWhole")) return false; $("picWhole").onmouseover = function(){atuokey = true}; $("picWhole").onmouseout = function(){atuokey = false}; var picFocus = $("picFoucus").getElementsByTagName('li'); var listLength = picFocus.length; if(listLength == 4){ picFocus[0].onmouseover = function() { moveElement('picList',0,0,10); classNormal('picFoucus','picText'); classCurrent('picFoucus','picText',0); } picFocus[1].onmouseover = function() { moveElement('picList',0,-225,10); classNormal('picFoucus','picText'); classCurrent('picFoucus','picText',1); } picFocus[2].onmouseover = function() { moveElement("picList", 0, -450, 10); classNormal('picFoucus','picText'); classCurrent('picFoucus','picText',2); } picFocus[3].onmouseover = function() { moveElement('picList',0,-675,10); classNormal('picFoucus','picText'); classCurrent('picFoucus','picText',3); } } } function operation(n){ var x = [0, -225, -450, -675]; if(n == 3){ moveElement('picList',0,x[n-3],10); classNormal('picFoucus','picText'); classCurrent('picFoucus','picText',0); }else{ moveElement('picList',0,x[n+1],10); classNormal('picFoucus','picText'); classCurrent('picFoucus','picText',n+1); } } function autoSwitch() { if(!$('picWhole')) return false; if(atuokey) return false; var picFocus = $('picFoucus').getElementsByTagName('li'); var listLength = picFocus.length; var currentNum = -1; for(var i=0; i<listLength; i++) { if (picFocus[i].className == 'current') { currentNum = i; break; } } operation(currentNum); } setInterval("autoSwitch()", 3000); loadFunction(picSwitch);
人生最可贵的事情是sb似的坚持于追求……