javascript實現輪顯新聞標題連結

來源:互聯網
上載者:User

用兩個div嵌套連結文字,最外面的div設定 overflow:hidden,並用js動態控制它的width,實現伸展與收縮。

<!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>新聞輪顯的demo</title>
<style type="text/css">
a { font-size:14px}
#box { width:400px; height:20px; overflow:hidden;}
#title { color:#000;width:400px; height:20px;font-size:14px;line-height:18px; /*background-color:#FF0000*/}
</style>
<script type="text/javascript">
var handle;
var handle2;
var boxW=0;
var titleNum=0;
function init(){
var newsTitles = "快樂笛子的部落格|中央統戰部:新社會階層納入中國統一戰線|中俄明年將在俄遠東地區舉行聯合反恐演|廣州賓館3小時內須將旅客資訊傳給公安機關|國親將推動彈劾陳水扁案";
var newsLinks = "http://www.happyshow.org|http://news.sina.com.cn/c/2006-09-02/093410901015.shtml|http://news.sina.com.cn/c/2006-09-02/02069914065s.shtml|http://news.sina.com.cn/c/2006-09-02/043210898986.shtml|http://news.sina.com.cn/c/2006-09-02/06069915455s.shtml";
document.getElementById("box").style.width = "400px";
document.getElementById("title").innerHTML = "<a target='_blank' href='"+newsLinks.split("|")[titleNum]+"'>"+newsTitles.split("|")[titleNum]+"</a>";
//setTimeout("decreaseBoxW();",1500);
titleNum++;
setInterval("innerTitle('"+newsTitles+"','"+newsLinks+"')",3100);
}
function innerTitle(titles,links){
var t = titles.split("|");
var a = links.split("|");
if (!t[titleNum])
titleNum=0;
document.getElementById("box").style.width = "0px";
document.getElementById("title").innerHTML = "<a target='_blank' href='"+a[titleNum]+"'>"+t[titleNum]+"</a>";
handle2 = setInterval("increaseBoxW()",1);//展開
titleNum++;
}
function increaseBoxW(){
boxW = parseInt(document.getElementById("box").style.width);
boxW = boxW + 4;
if (boxW>400){
clearInterval(handle2);
document.getElementById("box").style.width = "400px";
setTimeout("decreaseBoxW()",1500)
}else{
document.getElementById("box").style.width = boxW+"px";
}
}
function decreaseBoxW(){
handle = setInterval("decreaseBoxW2()",4)
}
function decreaseBoxW2(){
boxW = parseInt(document.getElementById("box").style.width);
boxW = boxW - 16;
if(boxW<0){
clearInterval(handle);
document.getElementById("box").style.width = "0px";
}else{
document.getElementById("box").style.width = boxW + "px";
}
}
</script>
</head>
<body>
<div id="box">
<div id="title"></div>
</div>
<script type="text/javascript">
init();
</script>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.