div可实现类似滚动公告栏的效果。本例学习如何使用JavaScript,实现这种文本的滚动效果。
软件开发网 www.mscto.com
【实现代码】
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>如何实现div内自动滚动?</title>
<style type="text/css">
#scrollMsg{width:500px;height:235px;background-color:#feeff7;overflow:scroll; overflow-x:hidden;text-overflow:ellipsis;word-break:break-all;}
#scrollMsg span{margin:6px;display:block;}
#scrollMsg span a{color:#f60;text-decoration:underline;margin:0 4px;}
#scrollMsg span a:hover{color:#f20;}
#scrollMsg span label{color:#c70060;margin:0 4px;}
</style>
软件开发网 www.mscto.com
<script type="text/javascript">
function getEid(id){
return document.getElementById(id); //获取指定的div元素
}
软件开发网 www.mscto.com
function newNode(param){
return document.createElement(param); //创建元素
}
function newTextNode(param){
return document.createTextNode(param); //创建元素内容
}
function scrollDiv(){ 软件开发网 www.mscto.com
var dest=getEid("scrollMsg"); //获取要显示滚动内容的div
var newStr=newTextNode(new Date().toLocaleString()+":知识改变命运,科技推动发展!"); //显示的滚动信息
var span=newNode("span"); //创建span元素
span.appendChild(newStr); //在sapn中添加显示信息
dest.appendChild(span); //将span添加到div中
scrollMsg.scrollTop+=10000; //滚动
setTimeout("scrollDiv()",2000); //设置定时器定时滚动
}
window.onload=scrollDiv;
</script> 软件开发网 www.mscto.com
</head>
<body>
<div id="scrollMsg"></div>
</body>
</html> 软件开发网 www.mscto.com
【运行效果】
div的自动滚动效果如图5-3所示。
【难点剖析】
本例的重点是动态创建元素。动态创建元素需要使用JavaScript的DOM对象,其可以实现元素的添加、删除、修改等功能。本例中,使用“createElement”方法创建了一个span元素,然后使用“createTextNode”方法为span元素指定文本内容,最后将span元素添加到要滚动的div中。
图5-3 div自动滚动的效果
<script language="JavaScript">
function click() {
if (event.button==2) {
if(document.all.auto.status==true){document.all.auto.status=false;alert("自动滚屏已经停止了!")}
scroller();
}
}
document.onmousedown=click
var position = 0;
function scroller() {
if (document.all.auto.status==true){
position++;
scroll(0,position);
clearTimeout(timer);
var timer = setTimeout("scroller()",50);
timer;
}
else{
clearTimeout(timer);
}
}
</script>
<script language="JavaScript">
<!--
function MM_callJS(jsStr) { //v2.0
return eval(jsStr)
}
//-->
</script>
<div align="right">
<input type="checkbox" name="auto" value="on" onClick="MM_callJS('scroller() ; ')" >
自动滚屏(右键暂停)</div>
http://demo.nextapp.com/echo3csjs/
http://www.7wind.net/wytx/wytx.htm 网页特效
string strUserId = txtUser.Text;
ArrayList list = Application.Get("GLOBAL_USER_LIST") as ArrayList;
if (list == null)
{
list = new ArrayList();
}
for (int i = 0; i < list.Count; i++)
{
if (strUserId == (list[i] as string))
{
//已经登录了,提示错误信息
lblError.Text = "此用户已经登录";
return;
}
}
list.Add(strUserId);
Application.Add("GLOBAL_USER_LIST", list);
void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
string strUserId = Session["SESSION_USER"] as string;
ArrayList list = Application.Get("GLOBAL_USER_LIST") as ArrayList;
if (strUserId != null && list != null)
{
list.Remove(strUserId);
Application.Add("GLOBAL_USER_LIST", list);
}
}
function window.onbeforeunload()
{
if (event.clientX>document.body.clientWidth && event.clientY<0||event.altKey){
window.open("logout.aspx");
}
}由于onbeforeunload方法在浏览器关闭、刷新、页面调转等情况下都会被执行,所以需要判断是点击了关闭按钮或是按下Alt+F4时才执行真正的关闭操作。
然后在logout.aspx的Page_Load中写和Session_End相同的方法,同时在logout.aspx中加入事件:onload="javascript:window.close()"
但是这样还是有问题,javascript在不同的浏览器中可能有不同的行为,还有就是当通过文件->关闭时没有判断到。
2、使用xmlhttp方法(这种方法测试下来没有问题)
在每个页面中加入如下的javascript(这些javascript也可以写在共通里,每个页面引入就可以了)
var x=0;
function myRefresh()
{
var httpRequest = new ActiveXObject("microsoft.xmlhttp");
httpRequest.open("GET", "test.aspx", false);
httpRequest.send(null);
x++;
if(x<60) //60次,也就是Session真正的过期时间是30分钟
{
setTimeout("myRefresh()",30*1000); //30秒
}
}
myRefresh();
在web.config中设置
<sessionState mode="InProc" timeout="1"></sessionState>
test.aspx页面就是一个空页面,只不过需要在Page_Load中加入:
Response.Expires = -1;
保证不使用缓存,每次都能调用到这个页面。
原理就是:设置Session的过期时间是一分钟,然后在每个页面上定时每30秒连接一次测试页面,保持Session有效,总共连60次,也就是30分钟。如果30分钟后用户还没有操作,Session就会过期。当然,如果用户直接关闭浏览器,那么一分钟后Session也会过期。这样就可以满足要求了。