JS代码实现根据时间变换页面背景效果
1.概述
有些时侯为了丰富页面的显示效果,将页面制作成根据时间变换页面背景的样式,这样会使浏览者对此网站不会感觉厌倦,同时也会觉得网站制作的非常新颖。本实例通过Date对象的getHours()方法获得当前系统时间的小时,然后根据不同的时间段来改变页面的背景图片。
2.技术要点
主要使用JavaScript中的Date对象的getHours()方法得到当前系统时间的小时,然后在一定的时间段内判断当前小时是否符合指定的时间段,如果符合则使用document对象的write()方法在页面中显示一幅图片并在图片下方输出指定的提示信息。
3.具体实现
(1)使用now.getHours()函数获取当前系统时间的小时,根据此时间变换不同的背景,主要JavaScript脚本关键代码如下:
<scriptlanguage="javascript"> varnow=newDate(); varhour=now.getHours(); if(hour>=0&&hour<5){ document.write("<center><imgsrc='1.jpg'width='600'height='399'><center>"); document.write("<p>"); document.write("<fontsize=6face=黑体color=#ff9900>现在是凌晨时间"+hour+"点,祝您好梦</font>"); } if(hour>=5&&hour<8){ document.write("<center><imgsrc='2.jpg'width='600'height='399'><center>"); document.write("<p>"); document.write("<fontsize=6face=黑体color=#ff9900>现在是早上时间"+hour+"点,祝您一天心情愉快</font>"); } if(hour>=8&&hour<11){ document.write("<center><imgsrc='3.jpg'width='600'height='399'><center>"); ocument.write("<p>"); document.write("<fontsize=6face=黑体color=#ff9900>现在是上午时间"+hour+"点,您别忘了小憩一会喝口水</font>"); } if(hour>=11&&hour<13){ document.write("<center><imgsrc='4.jpg'width='600'height='399'><center>"); document.write("<p>"); document.write("<fontsize=6face=黑体color=#ff9900>现在是中午时间"+hour+"点,记得要多吃饭</font>"); } if(hour>=13&&hour<17){ document.write("<center><imgsrc='5.jpg'width='600'height='399'><center>"); document.write("<p>"); document.write("<fontsize=6face=黑体color=#ff9900>现在是下午时间"+hour+"点,久坐办公室要适当起身运动一下</font>"); } if(hour>=17&&hour<24){ document.write("<center><imgsrc='6.jpg'width='600'height='399'><center>"); document.write("<p>"); document.write("<fontsize=6face=黑体color=#ff9900>现在是晚上时间"+hour+"点,要注意早点入睡</font>"); } </script>
(2)添加一段css样式代码如下:
<styletype="text/css"> body{ background-color:#FFFFFF; } </style>
(3)添加一段css样式代码如下:
<styletype="text/css"> body{ background-color:#FFFFFF; } </style>
JavaScript中的Date对象的getHours()方法返回的是小时,返回值是一个数字,在0到23之间,表示包含或开始于此Date对象表示的瞬间的一天中的小时(用本地时区进行解释)。
以上所述是小编给大家介绍的JS代码实现根据时间变换页面背景效果的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!