在弹出的DIV层上显示走动的倒计时,就是使倒计时在弹出的DIV层上动起来。源码如下:
HTML:
----------------------------------------------------------------------------------------<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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 runat="server">
    <title>无标题页</title>
    <style type="text/css">
    * {margin:0;}
    body {font-size:12px;}
    #container div{margin-top:5px;}
    #select_city h3{float:left;}
    #main_city,#all_province {clear:both;}
    #main_city div,#all_province div{width:600px;clear:left;}
    #main_city h4,#all_province h4{float:left;display:inline;font-size:16px;}
    #main_city div span,#all_province div span{margin:0 10px;cursor:pointer;font-size:12px;}
    #float_lay{width:200px;height:120px;border:1px #d1d1d1 solid;position:absolute;background:#fff;z-index:999;display:none;}
    </style>
<script type="text/javascript">
//弹出层
function openLayer(objId,conId){
var arrayPageSize   = getPageSize();//调用getPageSize()函数
var arrayPageScroll = getPageScroll();//调用getPageScroll()函数
if (!document.getElementById("popupAddr")){
//创建弹出内容层
var popupDiv = document.createElement("div");
//给这个元素设置属性与样式
popupDiv.setAttribute("id","popupAddr")
popupDiv.style.position = "absolute";
popupDiv.style.border = "1px solid #ccc";
popupDiv.style.background = "#fff";
popupDiv.style.zIndex = 99;
//创建弹出背景层
var bodyBack = document.createElement("div");
bodyBack.setAttribute("id","bodybg")
bodyBack.style.position = "absolute";
bodyBack.style.width = "100%";
bodyBack.style.height = (arrayPageSize[1] + 35 + 'px');
bodyBack.style.zIndex = 98;
bodyBack.style.top = 0;
bodyBack.style.left = 0;
bodyBack.style.filter = "alpha(opacity=50)";
bodyBack.style.opacity = 0.5;
bodyBack.style.background = "black";
//实现弹出(插入到目标元素之后)
var mybody = document.getElementById(objId);
insertAfter(popupDiv,mybody);//执行函数insertAfter()
insertAfter(bodyBack,mybody);//执行函数insertAfter()
}
//显示背景层
document.getElementById("bodybg").style.display = "";
//显示内容层
var popObj=document.getElementById("popupAddr")
popObj.innerHTML = document.getElementById(conId).innerHTML;
popObj.style.display = "";
//让弹出层在页面中垂直左右居中(统一)
// popObj.style.width  = "600px";
// popObj.style.height = "400px";
// popObj.style.top  = arrayPageScroll[1] + (arrayPageSize[3] - 35 - 400) / 2 + 'px';
// popObj.style.left = (arrayPageSize[0] - 20 - 600) / 2 + 'px';
//让弹出层在页面中垂直左右居中(个性)
var arrayConSize=getConSize(conId)
popObj.style.top  = arrayPageScroll[1] + (arrayPageSize[3] - arrayConSize[1]) / 2-50 + 'px';
popObj.style.left = (arrayPageSize[0] - arrayConSize[0]) / 2 -30 + 'px';}
//获取内容层内容原始尺寸
function getConSize(conId){
var conObj=document.getElementById(conId)
conObj.style.position = "absolute";
conObj.style.left=-1000+"px";
conObj.style.display="";
var arrayConSize=[conObj.offsetWidth,conObj.offsetHeight]
conObj.style.display="none";
return arrayConSize;
}
function insertAfter(newElement,targetElement){//插入
var parent = targetElement.parentNode;
if(parent.lastChild == targetElement){
parent.appendChild(newElement);
}
else{
parent.insertBefore(newElement,targetElement.nextSibling);
}
}
//获取滚动条的高度
function getPageScroll(){
var yScroll;
if (self.pageYOffset) {
yScroll = self.pageYOffset;
} else if (document.documentElement && document.documentElement.scrollTop){
yScroll = document.documentElement.scrollTop;
} else if (document.body) {
yScroll = document.body.scrollTop;
}
arrayPageScroll = new Array('',yScroll)
return arrayPageScroll;
}
//获取页面实际大小
function getPageSize(){
var xScroll,yScroll;
if (window.innerHeight && window.scrollMaxY){
xScroll = document.body.scrollWidth;
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){
sScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else {
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}
var windowWidth,windowHeight;
//var pageHeight,pageWidth;
if (self.innerHeight) {
windowWidth = self.innerWidth;
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) {
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) {
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}
var pageWidth,pageHeight
if(yScroll < windowHeight){
pageHeight = windowHeight;
} else {
pageHeight = yScroll;
}
if(xScroll < windowWidth) {
pageWidth = windowWidth;
} else {
pageWidth = xScroll;
}
arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
return arrayPageSize;
}
//关闭弹出层
function closeLayer(){
document.getElementById("popupAddr").style.display = "none";
document.getElementById("bodybg").style.display = "none";
return false;
}
</script>
<script type="text/javascript">
//拖拽
//对“拖动点”定义:onMousedown="StartDrag(this)" onMouseup="StopDrag(this)" onMousemove="Drag(this)"即可
var move=false,oldcolor,_X,_Y;
function StartDrag(obj){  //定义准备拖拽的函数
obj.setCapture(); //对当前对象的鼠标动作进行跟踪
oldcolor=obj.style.backgroundColor;
obj.style.background="#999";
move=true;
//获取鼠标相对内容层坐标
var parentwin=document.getElementById("popupAddr");
_X=parentwin.offsetLeft-event.clientX
_Y=parentwin.offsetTop-event.clientY
}
function Drag(obj){        //定义拖拽函数
if(move){
var parentwin=document.getElementById("popupAddr");
parentwin.style.left=event.clientX+_X;
parentwin.style.top=event.clientY+_Y;
}
}
function StopDrag(obj){   //定义停止拖拽函数
obj.style.background=oldcolor;
obj.releaseCapture(); //停止对当前对象的鼠标跟踪
move=false;
}
</script><script language="JavaScript"> function clock() 

var DifferenceHour = -1 
var DifferenceMinute = -1 
var DifferenceSecond = -1 
var now=document.getElementById("HiddenField1").value;
var Tday = new Date(now) 
var daysms = 24 * 60 * 60 * 1000 
var hoursms = 60 * 60 * 1000 
var Secondms = 60 * 1000 
var microsecond = 1000 
var time = new Date() 
var hour = time.getHours() 
var minute = time.getMinutes() 
var second = time.getSeconds() 
var timevalue = ""+((hour > 12) ? hour-12:hour) 
timevalue +=((minute < 10) ? ":0":":")+minute 
timevalue +=((second < 10) ? ":0":":")+second 
timevalue +=((hour >12 ) ? " PM":" AM") 
var convertHour = DifferenceHour 
var convertMinute = DifferenceMinute 
var convertSecond = DifferenceSecond 
var Diffms = Tday.getTime() - time.getTime() 
DifferenceHour = Math.floor(Diffms / daysms) 
Diffms -= DifferenceHour * daysms 
DifferenceMinute = Math.floor(Diffms / hoursms) 
Diffms -= DifferenceMinute * hoursms 
DifferenceSecond = Math.floor(Diffms / Secondms) 
Diffms -= DifferenceSecond * Secondms 
var dSecs = Math.floor(Diffms / microsecond) 
str_time="<font style='font-size:14pt;color:#28C84A'>"+DifferenceHour+"</font><font face='黑体' style='font-size:13pt;'>天</font><br>"+DifferenceMinute+"小时"+DifferenceSecond+"分"+dSecs+"秒";
timer.innerHTML=str_time; 
setTimeout("clock()",1000) 
} </script> 
</head>
<body>
    <form id="form1" runat="server">
        <asp:Button ID="btn" runat="server" Text="竞拍" OnClick="btn_Click" /><br/>截止日期:<asp:Label ID="Label2" runat="server" Text="2009-09-08 11:25:50"></asp:Label><br />
        <input id="ss" type="button" onclick="clock();return true,openLayer('btn','test_con3'); return false;" />
       
        
        <!--弹出层的内容-->
        <div id="test_con3" style="display:none" title="包含弹出层的内容的容器">
        <div id="tab3" style="width:300px;height:180px;background:#fee">
        <div id="tabtop3">
        <div id="tabtop-L3"  style="text-align:center;height:25px; padding-top:10px;" onmousedown="StartDrag(this)" onmouseup="StopDrag(this)" onmousemove="Drag(this)"><strong style="color:red;font-size:13px;text-align:center">温馨提示</strong></div>
        </div>
        <div id="tabcontent3" style="text-align:center;">
        <br />
        <div id="timer" style="font-size:9pt;color:red"></div>
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <br />
        </div>
        <br/><br/><br/><br/>
        <div id="tabtop-R3" style="text-align:center;" onclick="closeLayer()"><strong>关闭</strong></div>
        </div>
        </div>
    </form>
</body>
</html>

解决方案 »

  1.   

    CS:
    ----------------------------------------------------------------------------------------using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btn.Attributes.Add("onclick", "javascript:openLayer('btn','test_con3'); return false;");
                string m1 = changeMouth(Convert.ToInt32(Convert.ToDateTime(this.Label2.Text).Month));
                string d1 = Convert.ToString(Convert.ToDateTime(this.Label2.Text).Day);
                string y1 = Convert.ToString(Convert.ToDateTime(this.Label2.Text).Year);
                string h1 = Convert.ToString(Convert.ToDateTime(this.Label2.Text).Hour);
                string mi1 = Convert.ToString(Convert.ToDateTime(this.Label2.Text).Minute);
                string s1 = Convert.ToString(Convert.ToDateTime(this.Label2.Text).Second);
                this.HiddenField1.Value = m1 + " " + d1 + ", " + y1 + " " + h1 + ":" + mi1 + ":" + s1;
            }
        }
        public string changeMouth(int mouth)
        {
            string str;
            switch (mouth)
            {
                case 1:
                    str = "Jan";
                    break;
                case 2:
                    str = "Feb";
                    break;
                case 3:
                    str = "Mar";
                    break;
                case 4:
                    str = "Apr";
                    break;
                case 5:
                    str = "May";
                    break;
                case 6:
                    str = "Jun";
                    break;
                case 7:
                    str = "Jul";
                    break;
                case 8:
                    str = "Aug";
                    break;
                case 9:
                    str = "Sep";
                    break;
                case 10:
                    str = "Oct";
                    break;
                case 11:
                    str = "Nov";
                    break;
                case 12:
                    str = "Dec";
                    break;
                default: str = "NaN";
                    break;
            }
            return str;
        }
        protected void btn_Click(object sender, EventArgs e)
        {    }
    }
      

  2.   

    老大,var now=document.getElementById("HiddenField1").value;这句获得的是null吧。服务器控件可以这样吗?
    var now=document.getElementById(<%=this.HiddenField1.ClientID%>).value;
      

  3.   

    这个是可以这样写的,你的第二种写法是保险的写法。就是要看下页面运行生成的源代码中的id是不是跟你在html中定义的一样
      

  4.   


    I 服了 U给你一段简单代码你看看吧//getDateTime and show it on the view
    function GetDate(ob)
    {
        if(ob == "" || ob == null)
        {
            ob = "labDateTime";
        }
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth()+1>9?date.getMonth()+1:"0"+(date.getMonth()+1);
        var day = date.getDate()>9?date.getDate():"0"+date.getDate();
        var da = year + "-" + month + "-" + day + "  ";
        var hours = date.getHours()>9?date.getHours():"0"+date.getHours();
        var min = date.getMinutes()>9?date.getMinutes():"0"+date.getMinutes();
        var sec = date.getSeconds()>9?date.getSeconds():"0"+date.getSeconds();
        da += hours + ":" + min + ":" + sec;
        document.getElementById(ob).innerText = da;
        setTimeout("GetDate('"+ob+"')",1000);
    }你的代码我没看,我估计是没设置定时器
      

  5.   

    6楼,不是不走动,在页面上正常走动,我要的是在点击按钮弹出DIV,在DIV上走动,明白吗
    这个按钮事件执行了弹出DIV和执行倒计时的方法,有问题没?
    <input id="ss" type="button" onclick="clock();return true,openLayer('btn','test_con3'); return false;" />