演示地址:
幻灯片新闻设计的思路,看过我Ajax选项卡效果的朋友都知道,通过点击选项卡标签,就可以异步的加载信息,我们在很多网站上都看到过这样的FLASH的幻灯片效果--就是过一段时间就替换一个张图片,点图片右下角的数字标签也可以换图片,其实这个原理跟选项卡效果很相识,为什么我们不能够用ajax写一个幻灯片的图片新闻效果呢?
想到这里我就动手写了一个,大家会看到,这个跟选项卡效果没有什么区别,就是加了个settimeout方法来定时自动加载页面,不多说了,代码如下!!
<!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=utf-8" />
<title>新闻切换技术</title>
<style type="text/css">
<!--
body { text-align: center; margin:0; padding:0; background: #FFF; font-size:12px; color:#000;}
div,form,img,ul,ol,li,dl,dt,dd {margin: 0; padding: 0; border: 0;}
h1,h2,h3,h4,h5,h6 { margin:0; padding:0;}
table,td,tr,th{font-size:12px;}
.blank2{margin:0 auto; width:180px; height:3px; font-size:1px;}
a:link {color: #000; text-decoration:none;}
a:visited {color: #83006f;text-decoration:none;}
a:hover {color: #c00; text-decoration:underline;}
a:active {color: #000;}
.clear{clear:both; font-size:1px; visibility:hidden}
.focusPic{margin:0 auto; width:250px; border:1px solid #000;}
#focusPic{margin:0 auto; width:250px;}
.focusPic .pic{margin:0 auto; width:240px; height:180px; padding:1px; border:1px solid #000;}
.focusPic h2{ float:left; width:232px;padding:4px 0 3px 12px; font-size:14px; text-align:left;}
.focusPic p{float:left; width:226px;line-height:160%; margin:0; text-align:left;padding:0 0 10px 12px;}
.focusPic p img {margin:0px 0 2px;}
.focusPic .more{ margin:0 auto; width:240px; }
.focusPic .more .textNum{float:right; width:226px; margin:0;padding:0 0 4px;}
.focusPic .more .textNum .text{float:left; font-weight:bold; padding:7px 6px 0 0; color:#666;}
.focusPic .more .textNum .num{float:left; width:113px; height:19px;}
.focusPic .more .textNum .bg1{ background:url(images/num1.gif);}
.focusPic .more .textNum .bg2{ background:url(images/num2.gif);}
.focusPic .more .textNum .bg3{ background:url(images/num3.gif);}
.focusPic .more .textNum .bg4{ background:url(images/num4.gif);}
.focusPic .more .textNum .num ul{ float:left; width:113px;}
.focusPic .more .textNum .num li{float:left; width:28px; font-weight:bold; cursor:pointer; display:block; color:#fff; list-style-type:none; padding:6px 0 0;}
.focusPic .more .textNum .num li a{color:#fff; padding:0 5px; }
.focusPic .more .textNum .num li a:visited{color:#fff;}
.focusPic .more .textNum .num li a:hover{color:#ff0;}
.loading{
margin:0 auto;
padding-top:77px;
padding-bottom:77px;
width:220px; /*修改了,现在可以适应*/
height:18px;
text-align:center;
}
-->
</style>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
function getObject(objectId) {
if(document.getElementById && document.getElementById(objectId)) {
// W3C DOM
return document.getElementById(objectId);
}
else if (document.all && document.all(objectId)) {
// MSIE 4 DOM
return document.all(objectId);
}
else if (document.layers && document.layers[objectId]) {
// NN 4 DOM.. note: this won't find nested layers
return document.layers[objectId];
}
else {
return false;
}
}
var responsecont;
var xmlHttp;
var requestType;
var newsstring;
function CreateXMLHttpRequest(){
// Initialize Mozilla XMLHttpRequest object
if (window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
// Initialize for IE/Windows ActiveX version
else if (window.ActiveXObject) {
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
}
catch (e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){newsstring = "<div class='loading'>服务器忙,请稍后重新连接...</div>";}
}
}
}
function getnews(x){
var url = 'news'+x+'.htm';
var loadstatustext="<div class='loading'><img src='images/loading.gif' />正在加载信息,请稍候...</div>";
requestType = 'focusPic';
CreateXMLHttpRequest();
getObject(requestType).innerHTML = loadstatustext;
xmlHttp.onreadystatechange = processRequestChange;
xmlHttp.open("GET", url, true);
//xmlHttp.setRequestHeader("If-Modified-Since","0"); // 注释掉后,就可以起用缓存,不用每次都重新加载,但是加载页面如果有更新,这里却还是缓存,看不到更新
xmlHttp.send(null);
}
function processRequestChange(){
// only if xmlHttp shows "complete"
if (xmlHttp.readyState == 4){
// only http 200 to process
if (window.location.href.indexOf("http")==-1 || xmlHttp.status == 200){
newsstring = xmlHttp.responseText;
//inject centent to tab-pane
shownews(requestType,newsstring);
}
}
}
function shownews(requestType,newsstring){
//<![CDATA[
responsecont = getObject(requestType);
responsecont.innerHTML = newsstring;
//]]>
}
var num;
function changeBg(x){
getObject('focusPic1nav').style.backgroundImage='url(images/num'+x+'.gif)';
num = x;
try{
getnews(x);
}
catch(err){
alert(err);
}
}
var nn;
setTimeout('change_news()',3000);
function change_news(){
if(num){
nn=num;
nn++;
if(nn>4){
nn=1;
}
}
setTimeout('changeBg('+nn+')',6000);
nn++;
tt=setTimeout('change_news()',6000);
}
//-->
</script>
<div class="focusPic">
<div class="blank2"></div>
<div id="focusPic">
<div class="pic"><a href="http://tech.163.com/special/000915SN/soft2005.html"><img src="images/2006011810122068706.jpg" alt="网易学院05年软件评选结果" width="240" height="180" border="0" /></a></div>
<h2><a href="http://tech.163.com/special/000915SN/soft2005.html">网易学院05年软件评选结果</a></h2>
<p>经过大家的热情投票和我们的辛劳整理,网易学院2005年年度软件评选结果终于出炉啦。点击进入查看……<img src="images/i2.gif" alt="详细" width="3" height="5" /> [<a href="http://tech.163.com/special/000915SN/soft2005.html" class="cDRed">详细</a>]</p>
</div>
<div class="more">
<div class="textNum">
<div class="text">> 更多头图新闻</div>
<div class="num bg1" id="focusPic1nav">
<ul>
<li><a href="javascript:void(0);" onclick="changeBg(1)" target="_self">1</a></li>
<li><a href="javascript:void(0);" onclick="changeBg(2)" target="_self">2</a></li>
<li><a href="javascript:void(0);" onclick="changeBg(3)" target="_self">3</a></li>
<li><a href="javascript:void(0);" onclick="changeBg(4)" target="_self">4</a></li>
</ul><div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
</div>
</body>
</html>
怎么样,原来这么简单就可以实现了!!