javascript制作滚动公告栏

标签:
it |
分类: js |
javascript 向上滚动公告、水平滚动公告(翻页效果)
在页面中添加滚动公告栏,不但要向上滚动,而且可以向左滚动。滚动方向可以由用户选择。
以下是在Ext平台下运行的,UICtrl.StatusBarNoNotice 返回个TextItem。可以直接添加到Toolbar的Items中,进行测试。
Javascript代码
- UICtrl.StatusBarNoNotice
= function() { -
-
var noticeArray = []; -
// [id,title] -
noticeArray.push(["100", " -
noticeArray.push(["200", " -
noticeArray.push(["300", " -
noticeArray.push(["400", " -
noticeArray.push(["500", " -
-
var total = noticeArray.length;// -
-
// -
var noticeTxt = new Ext.Toolbar.TextItem({ -
id : 'noticeTxt', -
text : noticeArray[0][1] -
}); -
-
if (noticeArray.length == 0) -
noticeArray.push(["-1", " -
-
-
var marqueeInterval = new Array(); -
var marqueeId = 0; -
var marqueeDelay = 3000;// (ms) -
var marqueeHeight = 16; // -
var marqueeWidth = 300;// -
var dir = Sys.agentInfo.noticeScrollDir;// up 为向上滚动,left为向左滚动 -
var separator = " ● "; -
-
if (dir == "left") -
leafscroll(); -
else -
upscroll(); -
-
-
function leafscroll() { -
var str = ""; -
if (noticeArray[0][0] == "-1") -
return; -
for (var i = 0; i < noticeArray.length; i++) { -
str += separator; -
var item = '<a onclick="new UICtrl.ShowNoNotice(' -
+ String(noticeArray[i][0]) + ')" style="cursor:pointer;">' -
+ noticeArray[i][1] + '</a>'; -
str += item; -
} -
-
noticeDiv = '<div id="marqueeBox" style="overflow:hidden;height:' -
+ marqueeHeight -
+ 'px;width:' -
+ marqueeWidth -
+ 'px;"><div style="float:left;width: 800%;"><div id="notice1" style="float:left;">' -
+ str + '</div><div id="notice2" style="float:left;">' + str -
+ '</div></div></div>'; -
if (document.getElementByIdx_x("noticeTxt")) { -
var noticeText = document.getElementByIdx_x("noticeTxt"); -
noticeText.innerHTML = noticeDiv; -
} else -
noticeTxt.text = noticeDiv; -
-
var mytask = new Ext.TaskMgr.start({ -
run : function() { -
var noticeText = document.getElementByIdx_x("noticeTxt"); -
// noticeText.innerHTML = noticeDiv; -
-
var dir1 = Sys.agentInfo.noticeScrollDir; -
if (dir1 == "up") { -
noticeText.innerHTML = ""; -
Ext.TaskMgr.stop(mytask); -
upscroll(); -
return; -
} -
var marqueeBox = document.getElementByIdx_x("marqueeBox"); -
var notice1 = document.getElementByIdx_x("notice1"); -
var notice2 = document.getElementByIdx_x("notice2"); -
-
if (notice2.offsetWidth - marqueeBox.scrollLeft <= 0) -
marqueeBox.scrollLeft -= notice1.offsetWidth -
else { -
marqueeBox.scrollLeft++; -
} -
marqueeBox.onmouseover = function() { -
Ext.TaskMgr.stop(mytask); -
}; -
marqueeBox.onmouseout = function() { -
Ext.TaskMgr.start(mytask); -
}; -
}, -
interval : 10 -
}); -
} -
-
-
function upscroll() { -
var str = "<a onclick='javascript:new UICtrl.ShowNoNotice(" -
+ noticeArray[0][0] + ");' style='cursor:pointer;'>" -
+ noticeArray[0][1] + "</a>"; -
if (noticeArray[0][0] == "-1") -
str = noticeArray[0][1]; -
else -
marqueeId++; -
-
noticeDiv = '<div id="marqueeBox" style="overflow:hidden;height:' -
+ marqueeHeight + 'px;width:' + marqueeWidth + 'px;"><div>' -
+ str + '</div></div>'; -
-
if (document.getElementByIdx_x("noticeTxt")) { -
var noticeText = document.getElementByIdx_x("noticeTxt"); -
noticeText.innerHTML = noticeDiv; -
} else -
noticeTxt.text = noticeDiv; -
-
marqueeInterval[0] = new Ext.TaskMgr.start({ -
run : function() { -
var dir1 = Sys.agentInfo.noticeScrollDir; -
-
var noticeText = document.getElementByIdx_x("noticeTxt"); -
-
if (dir1 == "left") { -
noticeText.innerHTML = ""; -
Ext.TaskMgr.stop(marqueeInterval[0]); -
Ext.TaskMgr.stop(marqueeInterval[1]); -
leafscroll(); -
return; -
} -
var marqueeBox = document.getElementByIdx_x("marqueeBox"); -
-
if (noticeArray[marqueeId]) -
var str = "<a onclick='javascript:new UICtrl.ShowNoNotice(" -
+ noticeArray[marqueeId][0] -
+ ");' style='cursor:pointer;'>" -
+ noticeArray[marqueeId][1] + "</a>"; -
-
if (noticeArray[0][0] == "-1") -
str = noticeArray[0][1]; -
-
marqueeId++; -
if (marqueeId >= noticeArray.length) -
marqueeId = 0; -
-
if (marqueeBox.childNodes.length == 1) { -
var nextLine = document.createElement_x('DIV'); -
nextLine.innerHTML = str; -
nextLine.style.width = marqueeWidth; -
marqueeBox.appendChild(nextLine); -
} else { -
marqueeBox.childNodes[0].innerHTML = str; -
marqueeBox.appendChild(marqueeBox.childNodes[0]); -
marqueeBox.scrollTop = 0; -
} -
marqueeBox.onmouseover = function() { -
Ext.TaskMgr.stop(marqueeInterval[0]) -
} -
marqueeBox.onmouseout = function() { -
Ext.TaskMgr.start(marqueeInterval[0]); -
} -
// /marqueeBox.onclick = function() { -
// /new UICtrl.ShowNoNotice(); -
// /} -
if (marqueeInterval[1]) -
Ext.TaskMgr.stop(marqueeInterval[1]); -
marqueeInterval[1] = new Ext.TaskMgr.start({ -
run : function() { -
var marqueeBox = document.getElementByIdx_x("marqueeBox"); -
marqueeBox.scrollTop++; -
if (marqueeBox.scrollTop % marqueeHeight == (marqueeHeight - 1)) { -
Ext.TaskMgr.stop(marqueeInterval[1]); -
} -
}, -
interval : 20 -
}); -
}, -
interval : 3000 -
}); -
} -
return [noticeTxt]; - }
以下是纯JS版本的代码:
Html代码
- <!DOCTYPE
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> - <html>
- <head>
- <meta
http-equiv="Content-Type" content="text/html; charset=UTF-8"> - <title>滚动公告(横向/纵向)</title>
-
- </head>
- <body
style="margin: 5px;"> - <div>
-
<button onclick="left()"> -
<button onclick="up()"> - </div>
- <div
id="noticeBar">notice</div> - <hr>
- <script
type="text/javascript" language="javascript"> - var
noticeArray = []; - //[id,title]
- noticeArray.push(
[ 部门周例会会议通知1""100", " ]); - noticeArray.push(
[ 部门周例会会议通知2""200", " ]); - noticeArray.push(
[ 部门周例会会议通知3""300", " ]); - noticeArray.push(
[ 部门周例会会议通知4""400", " ]); - noticeArray.push(
[ 部门周例会会议通知5""500", " ]); -
- var
total 公告数量= noticeArray.length;// -
- if
(noticeArray.length == 0) -
noticeArray.push( [ "-1", " ]); -
-
- var
marqueeInterval = new Array(); - var
marqueeId = 0; - var
marqueeDelay 停顿时间= 3000;// (ms) - var
marqueeHeight 公告栏高度= 16; // - var
marqueeWidth 公告栏宽度= 300;// - var
dir 滚动方向up/left= 'left';// up 为向上滚动,left为向左滚动 - var
separator = " <font color='#999999'>●</font> "; -
- if
(dir == "left") -
leafscroll(); - else
-
upscroll(); -
-
- function
leafscroll() { -
var str = ""; -
if (noticeArray[0][0] == "-1") -
return; -
for ( var i = 0; i < noticeArray.length; i++) { -
str += separator; -
var item = '<a onclick="alert(' + String(noticeArray[i][0]) -
+ ')" style="cursor:pointer;">' + noticeArray[i][1] + '</a>'; -
str += item; -
} -
-
noticeDiv = '<div id="marqueeBox" style="overflow:hidden;height:' -
+ marqueeHeight -
+ 'px;width:' -
+ marqueeWidth -
+ 'px;margin:5px;border:2px solid #1d953f;font-size:12px;color:red;"><div style="float:left;width: 800%;"><div id="notice1" style="float:left;">' -
+ str + '</div><div id="notice2" style="float:left;">' + str -
+ '</div></div></div>'; -
-
var noticeBar = document.getElementByIdx_x("noticeBar"); -
noticeBar.innerHTML = noticeDiv -
-
marqueeInterval[3] = window.setInterval(LeftStartFn, 10); -
-
function LeftStartFn() { -
if (dir == "up") { -
noticeBar.innerHTML = ""; -
window.clearInterval(marqueeInterval[3]); -
upscroll(); -
return; -
} -
var marqueeBox = document.getElementByIdx_x("marqueeBox"); -
var notice1 = document.getElementByIdx_x("notice1"); -
var notice2 = document.getElementByIdx_x("notice2"); -
-
if (notice2.offsetWidth - marqueeBox.scrollLeft <= 0) -
marqueeBox.scrollLeft -= notice1.offsetWidth -
else { -
marqueeBox.scrollLeft++; -
} -
marqueeBox.onmouseover = function() { -
window.clearInterval(marqueeInterval[3]) -
}; -
marqueeBox.onmouseout = function() { -
marqueeInterval[3] = window.setInterval(LeftStartFn, 10) -
}; -
} - }
-
- function
upscroll() { -
var str = "<a onclick='javascript:new UICtrl.ShowNoNotice(" -
+ noticeArray[0][0] + ");' style='cursor:pointer;'>" -
+ noticeArray[0][1] + "</a>"; -
if (noticeArray[0][0] == "-1") -
str = noticeArray[0][1]; -
else -
marqueeId++; -
-
noticeDiv = '<div id="marqueeBox" style="overflow:hidden;height:' -
+ marqueeHeight -
+ 'px;width:' -
+ marqueeWidth -
+ 'px;margin:5px;border:2px solid #1d953f;font-size:12px;color:red;"><div>' -
+ str + '</div></div>'; -
-
var noticeBar = document.getElementByIdx_x("noticeBar"); -
noticeBar.innerHTML = noticeDiv -
-
marqueeInterval[0] = window.setInterval(upStartFn, 3000); -
-
function upStartFn() { -
-
if (dir == "left") { -
noticeBar.innerHTML = ""; -
window.clearInterval(marqueeInterval[0]); -
window.clearInterval(marqueeInterval[1]); -
leafscroll(); -
return; -
} -
var marqueeBox = document.getElementByIdx_x("marqueeBox"); -
-
if (noticeArray[marqueeId]) -
var str = "<a onclick='javascript:alert(" -
+ noticeArray[marqueeId][0] -
+ ");' style='cursor:pointer;'>" -
+ noticeArray[marqueeId][1] + "</a>"; -
-
if (noticeArray[0][0] == "-1") -
str = noticeArray[0][1]; -
-
marqueeId++; -
if (marqueeId >= noticeArray.length) -
marqueeId = 0; -
-
if (marqueeBox.childNodes.length == 1) { -
var nextLine = document.createElement_x('DIV'); -
nextLine.innerHTML = str; -
nextLine.style.width = marqueeWidth; -
marqueeBox.appendChild(nextLine); -
} else { -
marqueeBox.childNodes[0].innerHTML = str; -
marqueeBox.appendChild(marqueeBox.childNodes[0]); -
marqueeBox.scrollTop = 0; -
} -
marqueeBox.onmouseover = function() { -
window.clearInterval(marqueeInterval[0]) -
} -
marqueeBox.onmouseout = function() { -
marqueeInterval[0] = window.setInterval(upStartFn, 3000); -
} -
-
if (marqueeInterval[1]) -
window.clearInterval(marqueeInterval[1]); -
marqueeInterval[1] = window.setInterval(scrollFn, 10); -
} -
-
function scrollFn() { -
var marqueeBox = document.getElementByIdx_x("marqueeBox"); -
marqueeBox.scrollTop++; -
if (marqueeBox.scrollTop % marqueeHeight == (marqueeHeight - 1)) { -
window.clearInterval(marqueeInterval[1]); -
} -
} - }
- function
left() { -
dir = 'left'; - }
-
- function
up() { -
dir = 'up'; - }
- </script>
- </body>
- </html>